when applying function multiple output variables (e.g., list) subset of data.table, lose variable names. there way retain them?
library(data.table) foo <- function(x){ list(mn = mean(x), sd = sd(x)) } bar <- data.table(x=1:8, y=c("d","e","f","g")) # column names "mn" , "sd" replaced "v1" , "v2" bar[, sapply(.sd, foo), = y, .sdcols="x"] # column names "mn" , "sd" retained bar_split <- split(bar$x, bar$y) t(sapply(bar_split, foo))
i go wit following, bit awkward, doesn't require writing names manually no matter how many functions there are
bar[, as.list(unlist(lapply(.sd, foo))), = y, .sdcols = "x"] # y x.mn x.sd # 1: d 3 2.828427 # 2: e 4 2.828427 # 3: f 5 2.828427 # 4: g 6 2.828427
the biggest advantage of approach binds functions column names. if, example, have additional column, still give informative result while using same code above
set.seed(1) bar[, z := sample(8)] bar[, as.list(unlist(lapply(.sd, foo))), = y, .sdcols = c("x", "z")] # y x.mn x.sd z.mn z.sd # 1: d 3 2.828427 2.0 1.4142136 # 2: e 4 2.828427 7.5 0.7071068 # 3: f 5 2.828427 3.0 1.4142136 # 4: g 6 2.828427 5.5 0.7071068