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

时间:2017-09-01 23:07:50

标签: r group-by data.table user-defined-functions custom-function

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

我的代码:

# 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 我得到的是 get,mean_disp,mean_hp < / p>

修改

对于一个变量,修正似乎是罗马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)]
}

此处,仅使用= list(grouping1,grouping2)或其他变体似乎失败。

1 个答案:

答案 0 :(得分:1)

你不能只指定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