Sum all columns from a list

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


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] &