最佳答案
dplyr is amazingly fast, but I wonder if I'm missing something: is it possible summarise over several variables. For example:
library(dplyr)
library(reshape2)
(df=dput(structure(list(sex = structure(c(1L, 1L, 2L, 2L), .Label = c("boy",
"girl"), class = "factor"), age = c(52L, 58L, 40L, 62L), bmi = c(25L,
23L, 30L, 26L), chol = c(187L, 220L, 190L, 204L)), .Names = c("sex",
"age", "bmi", "chol"), row.names = c(NA, -4L), class = "data.frame")))
sex age bmi chol
1 boy 52 25 187
2 boy 58 23 220
3 girl 40 30 190
4 girl 62 26 204
dg=group_by(df,sex)
With this small dataframe, it's easy to write
summarise(dg,mean(age),mean(bmi),mean(chol))
And I know that to get what I want, I could melt, get the means, and then dcast such as
dm=melt(df, id.var='sex')
dmg=group_by(dm, sex, variable);
x=summarise(dmg, means=mean(value))
dcast(x, sex~variable)
But what if I have >20 variables and a very large number of rows. Is there anything similar to .SD in data.table that would allow me to take the means of all variables in the grouped data frame? Or, is it possible to somehow use lapply on the grouped data frame?
Thanks for any help