# Applying a function to a column of a dataset [closed]

Functions can easily be applied to columns of datasets, as in the documentation it is shown. Now I want to apply a function with parameters to a column, but I get a list rules. E.g.

dataset = Dataset[{
<|"a" -> 1, "b" -> "x", "c" -> {1}|>,
<|"a" -> 2, "b" -> "y", "c" -> {2, 3}|>,
<|"a" -> 3, "b" -> "z", "c" -> {3}|>,
<|"a" -> 4, "b" -> "x", "c" -> {4, 5}|>,
<|"a" -> 5, "b" -> "y", "c" -> {5, 6, 7}|>,
<|"a" -> 6, "b" -> "z", "c" -> {}|>}]


and

func[x_] := x^2


lmax[list_List, func] := Module[{temp =  list},  {Max @ temp, func @ (Length@temp)}]


If I now do:

dataset[All, {"c" -> lmax[#c, func] &}]


I get the following:

So two questions:

1. Why do I get this result.... where are the other columns?
2. How can I manage it to get the dataset with the changed column "c"?
• This has to do with precedence, "c" -> (lmax[#c, func] &) // FullForm will show you that your operator is a function. It will transform the entire row. You meant to use the syntax {"c" -> f} where only f is a function. In order to fix this, in other parts of Mathematica, it is common to write "c" -> (lmax[#c, func] &). This fixes the precedence issue but there is another problem with this which I believe is specific to this context, and it throws an error. You can get around it by defining lmax[func_][list_List] :=... and using dataset[All, {"c" -> lmax[func]}]. Commented May 16, 2015 at 12:04
• @Picket: Thanks for the precedence hint. The other remake on a workaround is exactly the one I´m using so far (but I´m still searching a "cleaner" way). Commented May 16, 2015 at 12:19
• @Picket: "...the other remark", not "...remake" ;-) funny typo.... Commented May 16, 2015 at 15:01
• I got it now. The problem isn't only precedence, it's precedence and the fact that you use "#c" instead of "#". It should be dataset[All, {"c" -> (lmax[#, func] &)}] Commented May 17, 2015 at 16:26
• Great! Times[1000, "Thanks"] @ Picket ;-) This is really a good -> answer! Commented May 17, 2015 at 21:03

The problem with

dataset[All, {"c" -> lmax[#c, func] &}]


is that it will be interpreted as

dataset[All, {("c" -> lmax[#c, func])&}]


so you have to explicitly write

dataset[All, {"c" -> (lmax[#, func]&)}]


to get the precedence right. In this last version I've also written # instead of #c because the function that you specify in this type of format {"c" -> f} will be given the values of the column c, and they will be known simply as #.