library(doBy)
library(data.table)
data <- warpbreaks
facs <- names(Filter(is.factor, data))
nums <- names(Filter(is.numeric, data))
Map(function(x) {
s <- summaryBy(list(nums, x), data, FUN = list("min", "mean", "median", "max"))
setNames(s, c("level", names(s)[-1]))
}, facs)
rbindlist(res, idcol = "factor")
## factor level breaks.min breaks.mean breaks.median breaks.max
## <char> <fctr> <num> <num> <num> <num>
## 1: wool A 10 31.03704 26.0 70
## 2: wool B 13 25.25926 24.0 44
## 3: tension L 14 36.38889 29.5 70
## 4: tension M 12 26.38889 27.0 42
## 5: tension H 10 21.66667 20.5 43
Consider this code from here except we have used
warpbreaksas the data. The problem is that the first column name is set to the factor name and here we wanted to call itlevelso that all the data frames generated below have the same column names because werbindlistall the data frames together. We had to overwrite the names -- see theSetNamesline. PerhapssummaryBycould provide some constrol over that through an argument or perhaps if we wrotesummaryBy(list(nums, level = x), data, FUN = fun)it could use the namelevelor whatever name was used there.