简体   繁体   English

将多个列名传递给 data.table 函数中的“by”

[英]Passing multiple column names to "by" in a data.table function

I've read many posts on passing column names to a data.table function, but I did not see a post dealing with passing multiple variables to "by".我已经阅读了很多关于将列名传递给 data.table 函数的帖子,但我没有看到处理将多个变量传递给“by”的帖子。 I commonly use code like this to calculate summary statistics by group.我通常使用这样的代码来按组计算汇总统计数据。

# Data
library(data.table)
dt=mtcars
setDT(dt)

# Summary Stats Example
dt[cyl==4,.(Count=.N,
    Mean=mean(hp),
    Median=median(hp)),
    by=.(am,vs)]

#    am vs Count   Mean Median
# 1:  1  1     7 80.571     66
# 2:  0  1     3 84.667     95
# 3:  1  0     1 91.000     91

I can't get the following function to work:我无法使用以下功能:

# Function
myFun <- function(df,i,j,by){
    df[i==4,.(Count=.N,
      Mean=mean(j),
      Median=median(j)),
      by=.(am,by)]
}
myFun(dt,i='cyl',j='hp',by='vs')

Note that I hard-coded "4" and "am" into the function for this example.请注意,我将“4”和“am”硬编码到此示例的函数中。 get() worked when only using 1 by grouping variable, but failed when multiple grouping variables are used. get()在仅通过分组变量使用 1 时有效,但在使用多个分组变量时失败。 Guidance on how to properly use get/quote/eval/substitute/parse/as.name/etc when writing data.table functions is appreciated.感谢有关在编写 data.table 函数时如何正确使用 get/quote/eval/substitute/parse/as.name/etc 的指导。

Just create a character vector for by part of data.table , it will work:只需为data.table by一部分创建一个字符向量,它就会起作用:

myFun <- function(df, i, j, by){

 df[get(i) == 4, .(Count = .N, 
           Mean = mean(get(j)),
           Median = median(get(j))),
  by = c(by, 'am')]
}



myFun(dt, i = 'cyl', j = 'hp', by = 'vs')

#vs am Count     Mean Median
#1:  1  1     7 80.57143     66
#2:  1  0     3 84.66667     95
#3:  0  1     1 91.00000     91

I've accepted sm95's answer.我已经接受了 sm95 的回答。 Below is a more complex example/solution that sends a list to the by argument:下面是一个更复杂的示例/解决方案,它将列表发送到by参数:

# Libraries
library(data.table)

# Data
dt = mtcars
setDT(dt)

# Function to calculate summary statistics
myFun <- function(df, i1var, i1val, i2var, i2val,            # i arguments
                                    j,                       # j arguments
                                    by1var, by2var, by2val){ # by arguments
    df[get(i1var) == i1val & get(i2var) %in% i2val,
         .(Count = .N,
            Mean = mean(get(j)),
            Median = median(get(j))),
        by = .(get(by1var), get(by2var) == by2val)]
} # END Function

# Run function
myFun(dt,i1var = 'cyl', i1val = 4, i2var = 'gear', i2val = c(3,4),
            j = 'hp',
            by1var = 'vs', by2var = 'am', by2val = 1)
#    vs am Count     Mean Median
# 1:  1  1     6 75.16667     66
# 2:  1  0     3 84.66667     95

# Should match
dt[cyl == 4 & gear %in% c(3,4),
     .(Count = .N,
        Mean = mean(hp),
        Median = median(hp)),
     by = .(vs, am == 1)]
#    vs am Count     Mean Median
# 1:  1  1     6 75.16667     66
# 2:  1  0     3 84.66667     95

Here is my Cheat Sheet:这是我的备忘单:

  • Pass i , j , and by variables using get(var)使用get(var)传递ijby变量
  • Pass i or by criteria directly通过i或直接by标准

The above may not apply to more complex functions, and may not be optimal.以上可能不适用于更复杂的功能,并且可能不是最优的。

If by is a vector and NOT a list (eg, by=c() vs by=.() ), then by arguments can be passed directly.如果by是向量而不是列表(例如, by=c()by=.() ),则可以直接传递by参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM