通过引用列名称在数据表中聚合 [英] Aggregation in data.table by reference to column names
本文介绍了通过引用列名称在数据表中聚合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想通过数据表中的列列来聚合一些列。但是,我想避免使用引号外的列名称(在 by =。(desiredColumn1,desiredColumn2)
,即)。我很高兴使用列名或列索引。例如:
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?
推荐答案
我们可以使用 c
与名称
x[, sum(Sepal.Width), by = c(names(x)[c(1, 5)])]
这篇关于通过引用列名称在数据表中聚合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文