The data.table
idiom is lapply(.SD, mean)
, which is
DT <- data.table(df)
DT[, lapply(.SD, mean), by = sex]
# sex age bmi chol
# 1: boy 55 24 203.5
# 2: girl 51 28 197.0
I'm not sure of a dplyr
idiom for the same thing, but you can do something like
dg <- group_by(df, sex)
# the names of the columns you want to summarize
cols <- names(dg)[-1]
# the dots component of your call to summarise
dots <- sapply(cols ,function(x) substitute(mean(x), list(x=as.name(x))))
do.call(summarise, c(list(.data=dg), dots))
# Source: local data frame [2 x 4]
# sex age bmi chol
# 1 boy 55 24 203.5
# 2 girl 51 28 197.0
Note that there is a github issue #178 to efficienctly implement the plyr
idiom colwise
in dplyr
.