We can do a couple of things to make it short.
1) By using group_by_
, we can group by many variables by passing an object that have the column names ('nm1')
2) Use summarise_each
for applying the same function (mean
) to multiple columns.
nm1 <- letters[3:12]
b %>%
group_by_(.dots = nm1) %>%
mutate(n = n()) %>%
group_by(n, add=TRUE) %>%
summarise_each(funs(mean), m:s)
data
set.seed(24)
m1 <- matrix(rnorm(10000*7), nrow=10000, dimnames = list(NULL, letters[13:19]))
set.seed(42)
m2 <- matrix(sample(letters[1:5], 10000*10, replace=TRUE), nrow=10000,
dimnames = list(NULL, letters[3:12]))
b <- cbind(as.data.frame(m2, stringsAsFactors=FALSE), m1)
b[3:5, 1:10] <- b[1, 1:10]