如何将新摘要列的名称传递给函数中的data.table?

时间:2016-02-11 18:35:34

标签: r data.table

假设我想创建一个从R中的data.table计算摘要数据集的函数,我希望能够以编程方式传递新计算变量的名称。

例如:

library(data.table)

# generate some fake data
set.seed(919)
dt <- data.table(x = rnorm(50), by.var = rep(c("a", "b"), 25))
dt[, list(group.means = mean(x)), by = "by.var"]  # This is what I want

# But I want to do in a function, so I can do it repeatedly:
groupMeans <- function(out.var, by.var, dat = dt) {
  return(dat[, list(out.var = mean(x)), by = by.var])  # doesn't work
}

groupMeans("group.means", "by.var")  # out.var should be "group.means"

我该怎么做?

3 个答案:

答案 0 :(得分:8)

docendo discimus提供,您可以使用setNames创建的命名列表,如下所示:

groupMeans <- function(out.var, by.var, dat = dt) {
  return(dat[, setNames(list(mean(x)), out.var), by = by.var])  
}

groupMeans("group.means", "by.var")
#    by.var group.means
# 1:      a  -0.1159832
# 2:      b   0.2910531

答案 1 :(得分:2)

您可以考虑更改函数中的列名:

groupMeans <- function(out.var, by.var, dat = dt) {
  res <- dat[, list(mean(x)), by=by.var]
  setnames(res, "V1", out.var)
  res
}

答案 2 :(得分:2)

我们可以使用setnames将汇总列命名为'out.var'向量。

groupMeans <- function(out.var, by.var, dat = dt) {
  setnames(dat[, list(mean(x)), by = by.var], 
                 length(by.var)+1L, out.var)
}

groupMeans("group.var","by.var", dt)[]
#    by.var  group.var
#1:      a -0.1159832
#2:      b  0.2910531

编辑:基于@Frank的建议。