[R] using dcast with a function of multiple arguments

Bos, Roger roger.bos at rothschild.com
Mon Jul 20 20:52:02 CEST 2015


I am trying to figure out how to use dcast.data.table with a function with multiple arguments.  Here is my reproducible example for a simple function of one argument:

require(data.table)
dt <- as.data.table(mtcars)
dcast.data.table(dt, carb ~ cyl, value.var='mpg', fun=mean)

If I instead want to use, say, weighted.mean(x, w), how do I do so?

The docs say
...
Any other arguments that maybe passed to the aggregating function.

So I tried:

> dcast.data.table(dt, carb ~ cyl, value.var='mpg', fun=weighted.mean, w="wt")
Error in weighted.mean.default(data[[value.var]][0], ...) :
  'x' and 'w' must have the same length

The docs also say that value.var can be a list, so I tried that:

 In cases where value.var is a list, the function should be able to handle a list input and provide a single value or list of length one as output.

> dcast.data.table(dt, carb ~ cyl, value.var=list('mpg','wt'), fun=weighted.mean)
Error in dcast.data.table(dt, carb ~ cyl, value.var = list("mpg", "wt"),  :
  'value.var' must be a character vector of length 1.

I didn't actually expect that to work, but without an example I don't know what else to try.  Any hints would be greatly appreciated.

Thanks,

Roger



***************************************************************
This message and any attachments are for the intended recipient's use only.
This message may contain confidential, proprietary or legally privileged
information. No right to confidential or privileged treatment
of this message is waived or lost by an error in transmission.
If you have received this message in error, please immediately
notify the sender by e-mail, delete the message, any attachments and all
copies from your system and destroy any hard copies.  You must
not, directly or indirectly, use, disclose, distribute,
print or copy any part of this message or any attachments if you are not
the intended recipient.



More information about the R-help mailing list