I play around with Alternatives for the dplyr way of summarising data. I like the split and apply approach but need some help.
library(Hmisc)
library(data.table)
summary <- function(x) {
funs <- c(wtd.mean, wtd.var)
sapply(funs, function(f) f(x, na.rm = TRUE))
}
df <- split(mtcars, f = mtcars$cyl)
store <- list()
for(i in 1:length(df)) {
store[[i]] <- data.frame(sapply(df[[i]], summary))
}
finaldf <- data.table::rbindlist(store)
finaldf
Here is my code. With the split function i get three dataframes with summarised values. But after that my code gets a little bit messy with creating an empty list, converting the matrix to a data.frame inside the loop etc.
Is there a way to use multiple apply functions to avoid this loop? Something like lapply(sapply(...)) ?