如何将字符参数传递给data.table?我试过eval
,但没有帮助:
library(data.table)
dt <- data.table(iris)
groupedBy <- "Species"
dt[,.(m = mean(Sepal.Length)), by = .(groupedBy)] # error!
答案 0 :(得分:4)
我们可以在没有.()
dt[,.(m = mean(Sepal.Length)), by = groupedBy]
它也适用于多列
grpby <- c('cyl', 'gear')
as.data.table(mtcars)[, .(m_mpg = mean(mpg)), by = grpby]