6
$\begingroup$

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

and additionally (just an example):

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:

enter image description here

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"?
$\endgroup$
5
  • 1
    $\begingroup$ 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]}]. $\endgroup$
    – C. E.
    Commented May 16, 2015 at 12:04
  • $\begingroup$ @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). $\endgroup$
    – mgamer
    Commented May 16, 2015 at 12:19
  • $\begingroup$ @Picket: "...the other remark", not "...remake" ;-) funny typo.... $\endgroup$
    – mgamer
    Commented May 16, 2015 at 15:01
  • 1
    $\begingroup$ 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] &)}] $\endgroup$
    – C. E.
    Commented May 17, 2015 at 16:26
  • 1
    $\begingroup$ Great! Times[1000, "Thanks"] @ Picket ;-) This is really a good -> answer! $\endgroup$
    – mgamer
    Commented May 17, 2015 at 21:03

1 Answer 1

8
$\begingroup$

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 #.

$\endgroup$

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