通过不同变量的不同函数创建多个列

时间:2015-11-08 16:36:14

标签: r data.table

我无法转换到data.table。我试图通过一些分类变量进行分组,并应用每个目标不同变量的函数列表以创建新列。对于mapplyMap来说这似乎应该很容易,但是我无法想出在传递给函数时汇集适当的子集。

这是它的样子,

set.seed(2015)
dat <- data.table(cat1 = factor('Total'),
                  cat2 = factor(rep(letters[1:4], 5)),
                  cat3 = factor(rep(1:4, each=5)),
                  var1 = sample(20),
                  var2 = sample(20),
                  var3 = sample(20))

## I have list of factor columns to group by
groups <- c(paste0("cat", 1:3))
setkeyv(dat, groups)

## List of functions, and corresponding list of column names that
## they are to be applied to.  So, in this example I should get
## two new columns: V1=sum(var1) and V2=mean(var2, var3)
thing <- function(...) mean(c(...), na.rm=TRUE)  # arbitrary function
funs <- list("sum", "thing")                     # named functions
targets <- list("var1", c("var2", "var3"))       # variables
outnames <- funs                                 # names or result columns

## Can't get this part
f <- function(fn, vars) do.call(fn, vars)
dat[, outnames := Map(f, funs, targets), by=groups]

此示例的结果应该是这样的

dat[, `:=`(sum=sum(var1), thing=thing(var2, var3)), by=groups]

1 个答案:

答案 0 :(得分:3)

我们需要根据“目标”list中的列名称对数据集列进行子集化。一种方法是遍历'targets'的list元素并将data.table(.SD[, x, with=FALSE])子集化,然后应用该函数。

dat[, unlist(outnames) := Map(f, funs, lapply(targets, function(x)
                          .SD[, x, with=FALSE])), by = groups]