I've been experimenting with dplyr, reading about NSE, and googling, but don't understand why this simple example doesn't work. I'd like to have summarise use an arbitrary function that might take extra parameters (e.g., weights). It works fine with aggregate, but not dplyr, and I'd appreciate if someone can explain why. Simple example:
library(dplyr)
d <- data.frame(x=c(1,1,2,2), y=1:4) # sample data
f_dplyr <- function(d, FUN, ...) {
group_by(d, x) %>% summarise(y=FUN(y, ...))
}
f_aggregate <- function(d, FUN, ...) {
aggregate(y~x, d, FUN=FUN, ...)
}
print(f_aggregate(d, weighted.mean, c(1,2))) # this works
print(f_dplyr(d, weighted.mean, c(1,2))) # this does not--no output
Thanks,
Ben
R version 3.1.2 (2014-10-31)
Platform: x86_64-apple-darwin10.8.0 (64-bit)
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] dplyr_0.3.0.9000
loaded via a namespace (and not attached):
[1] assertthat_0.1 DBI_0.3.1 lazyeval_0.1.9 magrittr_1.0.1 parallel_3.1.2 Rcpp_0.11.3 tools_3.1.2