9
$\begingroup$

I have go through the forum but did not see this question. Hope I don't miss it.

I want to add a new colum "C" to a dataset with existing columns named "A" and "B". The value of "C" shall be the difference between the value of "A" at the same row and the previous value of "A". (A[[i]]-A[[i-1]]) I see how to do it with creating a new list for "C" and append it to the dataset but is it possible to do it using slots #. Thanks for your help.

$\endgroup$
9
$\begingroup$

Let's start with a dataset:

$dataset =
  RandomInteger[100, 5] //
  Query[All, <|"A" -> All, "B" -> RomanNumeral|>] //
  Dataset

dataset screenshot

We can compute the differences in the A column between rows by using a semi-imperative query operator:

Module[{prev = 0}, $dataset[All, {<| #, "C" -> #A - prev|>, prev = #A}[[1]] &]]

dataset with additional differences column

$\endgroup$
  • $\begingroup$ Wowwww great even if I don't understand perfecty the syntax. I see dataset in mathematica can be really powerful but the wolfram documentation is not very extended. Do you know some books or websites which deal with datasets in Mathematica? Thanks. $\endgroup$ – Porty Oct 3 '17 at 10:13
  • $\begingroup$ @Porty Stephen Wolfram writes about datasets in section 45 of An Elementary Introduction to the Wolfram Language. $\endgroup$ – WReach Oct 3 '17 at 14:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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