8
$\begingroup$

I would like to use a Dataset to track the process of a lengthy calculation over a large volume of material. Dataset appeals to me because of the power of the queries and it also looks like it may offer a simple way to serialize results to a data store.

I have not been able to figure out how to programmatically add rows to an existing Dataset.

If I initialize a Dataset named eMap:

          eMap = Dataset[{<|"ModuleId"->0,
                       "SegmentId"->0,
                       "x1"->0, "y1"->0, "z1"->0,
                       "x2"->0, "y2"->0, "z2"->0|>}]

How would I programmatically add an additional row with the same columns to eMap so that eMap may be queried?

I have tried Append and AppendTo without success.

  eMap = AppendTo[eMap, {<|"ModuleId"->0,
                       "SegmentId"->0,
                       "x1"->0, "y1"->0, "z1"->0,
                       "x2"->0, "y2"->0, "z2"->0|>}]];

After the call above, if I issue:

eMap[1]

I get an error: Cannot apply Append to an expression of the form {__Association}.

$\endgroup$
3
  • 2
    $\begingroup$ Have a look here. $\endgroup$ Commented Jan 14, 2015 at 12:59
  • $\begingroup$ b. Thanks most kindly - the referenced approach discusses adding additional columns. Is it possible to add additional rows (with the same columns)? $\endgroup$ Commented Jan 14, 2015 at 13:06
  • $\begingroup$ Any suggestions for adding a row and in case it exists already overwrite the existing row? $\endgroup$
    – Rho Phi
    Commented Aug 29, 2015 at 11:06

3 Answers 3

6
$\begingroup$

If the curly braces are left out, this seems to work.

 Remove[eMap];
eMap = Dataset[{<|"ModuleId" -> 0, "SegmentId" -> 0, "x1" -> 0, 
     "y1" -> 0, "z1" -> 0, "x2" -> 0, "y2" -> 0, "z2" -> 0|>}];
eMap = AppendTo[
   eMap, <|"ModuleId" -> 0, "SegmentId" -> 1, "x1" -> 1, "y1" -> 0, 
    "z1" -> 0, "x2" -> 1, "y2" -> 0, "z2" -> 0|>];

One quick comment - the article referenced by b.gatessucks is well worth a look. This could be a basis for a very useful set of metadata management functions that allow developers to do SQL-Like operations similar to Alter Structure commands.

$\endgroup$
3
$\begingroup$
Append[eMap // Normal, eMap[1] // Normal] // Dataset

{<|"ModuleId" -> 0, "SegmentId" -> 0, "x1" -> 0, "y1" -> 0, "z1" -> 0, "x2" -> 0, "y2" -> 0, "z2" -> 0|>, <|"ModuleId" -> 0, "SegmentId" -> 0, "x1" -> 0, "y1" -> 0, "z1" -> 0, "x2" -> 0, "y2" -> 0, "z2" -> 0|>}

Note: using eMap[1] as per your example.

At this point, Dataset is not fully integrated with the rest of the language, hence the Normal workaround.

$\endgroup$
4
  • 1
    $\begingroup$ Alan - Thank you. Would you know what additional items are being done to fully integrate Dataset? $\endgroup$ Commented Jan 15, 2015 at 13:26
  • 2
    $\begingroup$ There's an answer to that question: wolfram.com/data-science-platform, I don't see how WRI can launch and expect quick - or any - ramp up in use w/o good Dataset integration. $\endgroup$ Commented Jan 15, 2015 at 18:19
  • $\begingroup$ I agree that data management would need a brush-up ... $\endgroup$
    – Rho Phi
    Commented Aug 29, 2015 at 11:02
  • $\begingroup$ As of version 10.2 (of July 29th to be exact) something like AppendTo[ds, Association[{"point" -> pointtobeadded, "value" -> RandomInteger[10]}]] seems to work $\endgroup$
    – Rho Phi
    Commented Aug 29, 2015 at 15:43
1
$\begingroup$

You can just use Join (in version 12.1):

eMap = Dataset[{<|"ModuleId" -> 0, "SegmentId" -> 0, "x1" -> 0, "y1" -> 0, 
    "z1" -> 0, "x2" -> 0, "y2" -> 0, "z2" -> 0|>}]

(* Dataset[{<|"ModuleId" -> 0, "SegmentId" -> 0, "x1" -> 0, "y1" -> 0, "z1" -> 0, 
       "x2" -> 0, "y2" -> 0, "z2" -> 0|>}] *)

eMap2 = Join[eMap, 
  Dataset@{<|"ModuleId" -> 0, "SegmentId" -> 0, "x1" -> 0, "y1" -> 0, 
     "z1" -> 0, "x2" -> 0, "y2" -> 0, "z2" -> 0|>}]

(* Dataset[{<|"ModuleId" -> 0, "SegmentId" -> 0, "x1" -> 0, "y1" -> 0, "z1" -> 0, 
       "x2" -> 0, "y2" -> 0, "z2" -> 0|>, <|"ModuleId" -> 0, 
   "SegmentId" -> 0, "x1" -> 0, 
       "y1" -> 0, "z1" -> 0, "x2" -> 0, "y2" -> 0, "z2" -> 0|>}] *)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.