将数据 table 包装到函数中时如何设置分组变量列的名称

How to set a name of grouping variable column when wrapping data table into function

我希望能够更改包含数据中分组变量的列的名称 table。我知道如何在不包装到函数中时执行此操作,但是当我将 group by data table 操作包装到函数中时,我无法弄清楚如何设置名称以真正反映分组变量。

我的代码:

# load the data table library
library(data.table)
# load sample dataset for reproducible example
mtcars <- data.table(mtcars)
# define a function which would group given
# data table (1st parameter) by given column (2nd parameter)
grouping_function <- function(x, grouping1)
{
  x[,
    list(mean_disp = mean(disp),
           mean_hp = mean(hp)),
    .(get(grouping1))]
}

现在,如果我 运行 grouping_function(mtcars, "cyl") 我想得到列名 cyl, mean_disp, mean_hp byt我得到的是 get, mean_disp, mean_hp

编辑

对于一个变量,修复似乎很简单,正如 Roman Lustrik 的回答所建议的那样。但是当我有两个分组变量时,该修复似乎不起作用:

# load the data table library
library(data.table)
# load sample dataset for reproducible example
mtcars <- data.table(mtcars)
# define a function which would group given
# data table (1st parameter) by given column (2nd parameter)
grouping_function <- function(x, grouping1, grouping2)
{
  x[,
    list(mean_disp = mean(disp),
           mean_hp = mean(hp)),
    .(get(grouping1), get(grouping2)]
}

在这里,仅使用 by = list(grouping1, grouping2) 或其他变体似乎会失败。

您不能只指定 by 吗?

grouping_function <- function(x, grouping1) {
  x[,
    list(mean_disp = mean(disp),
         mean_hp = mean(hp)),
    by = grouping1]
}

grouping_function(mtcars, "cyl")

   cyl mean_disp   mean_hp
1:   6  183.3143 122.28571
2:   4  105.1364  82.63636
3:   8  353.1000 209.21429