Aggregation in data.table by reference to column names [duplicate]

ⅰ亾dé卋堺 提交于 2019-12-11 05:35:08

问题


I would like to aggregate some columns by a list of columns in a data.table. However, I would like to refrain from using the column names outside the quotation marks (in the by = .(desiredColumn1, desiredColumn2), that is). I am happy with using either the column names or the column indices. For example:

library(data.table)
x = as.data.table(iris)
x[, sum(Sepal.Width), by = .(Sepal.Length, Species)] # I want to avoid doing this
x[, sum("Sepal.Width"), by = .("Sepal.Length", "Species"), with = FALSE] # this does not work
x[, sum("Sepal.Width"), by = .(1, 5), with = FALSE]

Any ideas on how to do this?


回答1:


We can use c with names

x[, sum(Sepal.Width), by = c(names(x)[c(1, 5)])] 


来源:https://stackoverflow.com/questions/39954534/aggregation-in-data-table-by-reference-to-column-names

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!