8
$\begingroup$

I have a list of column names:

list = {"col1", "col2", "col3"}

and a dataset ds. I want to add a new column to ds that contains the sum of the columns listed in list. As suggested here, I could do:

ds[All, <| #, "colnew" -> #col1 + #col2 + #col3 |> &]

But I cannot list the column names by hand, because the list may be very large, or I might not know the names of the columns a priori.

$\endgroup$
0

3 Answers 3

7
$\begingroup$

If the you want to total all columns instead of just those on a select list, then the suggestion given by @Kuba is appropriate:

ds[All, <| #, "colnew" -> Total@# |> &]

dataset screenshot

If you wish to total only the listed columns, then:

ds[All, <| #, "colnew" -> Total[#[[list]]] |> &]

For example:

list = {"col1", "col2", "col3"};

ds = AssociationThread[list->#]& /@ RandomInteger[10, {4, 3}] // Dataset

ds[All, <| #, "colnew" -> Total[#[[list]]] |> &]

dataset screenshot

Or, asking for an actual subset of columns:

list2 = {"col1", "col2"};

ds[All, <| #, "colnew" -> Total[#[[list2]]] |> &]

dataset screenshot

$\endgroup$
4
  • $\begingroup$ I suppose I missed the point :) $\endgroup$
    – Kuba
    Commented Jun 1, 2017 at 16:02
  • $\begingroup$ @Kuba Actually, I just reworded my response to emphasize your suggestion -- to match the title of the post :) $\endgroup$
    – WReach
    Commented Jun 1, 2017 at 16:02
  • $\begingroup$ I don't sure you have read my answer here.But I have read yours many many times,which make me upset sometimes I have to say.I think your solution works just beacuse result by a unexpected behavior derive from MMA.Note this two f is in different place(one is in the inner of Association,but another out of it,and I think it is should in outside).Though they is a same operation.I think the solution is right,I mean that behavoir is expected. $\endgroup$
    – yode
    Commented Jul 2, 2017 at 23:09
  • $\begingroup$ @yode You are right -- the documentation makes no explicit mention of this syntax. But it does say that a list of rules will be treated as if the rules has been specified directly. Associations are treated like lists in many functions, so it would be surprising if Association itself did not do that. I think the syntax is a logical consequence of these principles. For the first example in your screenshot to work, I would expect f to evaluate to a rule, list or association. $\endgroup$
    – WReach
    Commented Jul 3, 2017 at 5:23
3
$\begingroup$

You can programmatically generate <| #, "colnew" -> #col1 + #col2 + #col3 |> & as follows.

list = {"col1", "col2", "col3"};

Tr[Slot /@ list] /. x_ :> (<|#, "colnew" -> x|> &)
Association[#1, "colnew" -> #col1 + #col2 + #col3] &
$\endgroup$
1
$\begingroup$
ds = Dataset[{<|"col1" -> 1, "col2" -> 2|>, <|"col1" -> 3, 
    "col2" -> 4|>, <|"col1" -> 5, "col2" -> 6|>}]

Mathematica graphics

ds[All, Append[#, "colnew" -> Total[#]] &]

Mathematica graphics

$\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.