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

77 views Asked by At

I would like to be able to change a name of column which contains the grouping variable in the data table. I know how to do this when not wrapping into a function, but when I wrap the group by data table operation into a function, I am unable to figure out how to also set the name to truly reflect the grouping variable.

My code:

# 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))]
}

Now, if I run grouping_function(mtcars, "cyl") I would like to get column names cyl, mean_disp, mean_hp byt what I get is get, mean_disp, mean_hp

EDIT

For one variable the fix seems to be strightforward as suggested by the answer of Roman Lustrik. But when I have two grouping variables, that fix doesn't seem to work:

# 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)]
}

Here, using merely by = list(grouping1, grouping2) or other variants seem to fail.

1

There are 1 answers

2
Roman Luštrik On BEST ANSWER

Can't you just specify 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