[R] using dcast with a function of multiple arguments

Thierry Onkelinx thierry.onkelinx at inbo.be
Tue Jul 21 09:52:58 CEST 2015


Here is a solution using dplyr

require(data.table)
dt <- as.data.table(mtcars)
library(dplyr)
library(tidyr)
dt %>%
  group_by(carb, cyl) %>%
  summarise(WM = weighted.mean(x = mpg, w = wt)) %>%
  spread(cyl, WM)

Best regards,

ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek / Research Institute for Nature and
Forest
team Biometrie & Kwaliteitszorg / team Biometrics & Quality Assurance
Kliniekstraat 25
1070 Anderlecht
Belgium

To call in the statistician after the experiment is done may be no more
than asking him to perform a post-mortem examination: he may be able to say
what the experiment died of. ~ Sir Ronald Aylmer Fisher
The plural of anecdote is not data. ~ Roger Brinner
The combination of some data and an aching desire for an answer does not
ensure that a reasonable answer can be extracted from a given body of data.
~ John Tukey

2015-07-20 20:52 GMT+02:00 Bos, Roger <roger.bos op rothschild.com>:

> 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.
>
> ______________________________________________
> R-help op r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list