在`by`参数中使用字符向量 [英] Use a character vector in the `by` argument
问题描述
在R的 data.table
包中,有没有一种方法可以使用字符向量在中由by分配
计算的参数?
Within the data.table
package in R, is there a way in order to use a character vector to be assigned within the by
argument of the calculation?
以下是使用mtcars从中得到的期望输出示例:
Here is an example of what would be the desired output from this using mtcars:
mtcars <- data.table(mtcars)
ColSelect <- 'cyl' # One Column Option
mtcars[,.( AveMpg = mean(mpg)), by = .(ColSelect)] # Doesn't work
# Desired Output
cyl AveMpg
1: 6 19.74286
2: 4 26.66364
3: 8 15.10000
我知道可以在 j $ c中使用分配列名
I know that this is possible to use assigning column names in j
by enclosing the vector around brackets.
ColSelect <- 'AveMpg' # Column to be assigned for average mpg value
mtcars[,(ColSelect):= mean(mpg), by = .(cyl)]
head(mtcars)
mpg cyl disp hp drat wt qsec vs am gear carb AveMpg
1: 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 19.74286
2: 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 19.74286
3: 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 26.66364
4: 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1 19.74286
5: 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2 15.10000
6: 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1 19.74286
是否有建议为了实现此目的而在 by
参数中添加什么?
Is there a suggestion as to what to put in the by
argument in order to achieve this?
推荐答案
来自中
部分,它表示?data.table
by
接受:
- 包含逗号分隔列名的单个字符串(空格很重要,因为列名即使在开头或结尾也可能包含空格
):例如DT [,sum(a) ,by = x,y,z]
- 列名称的字符向量:例如DT [,sum(a),by = c( x, y )]
是的,您可以在@cccmir的回复中使用答案。您也可以使用 c()
,如@akrun所述,但这似乎有点多余,除非您想要多个列。
So yes, you can use the answer in @cccmir's response. You can also use c()
as @akrun mentioned, but that seems slightly extraneous unless you want multiple columns.
不能使用。()
语法的原因是在 data.table
中。 )
是 list()
的别名。并且根据 by
的相同帮助, list()
语法要求使用列名的表达式-而不是字符串。
The reason you cannot use .()
syntax is that in data.table
.()
is an alias for list()
. And according to the same help for by
the list()
syntax requires an expression of column names - not a character string.
如果要使用多个变量并将名称作为字符传递,请参考 by
帮助中的示例。您可以这样做:
Going off the examples in the by
help if you wanted to use multiple variables and pass the names as characters you could do:
-
mtcars [,..(AveMpg = mean(mpg)),by = cyl, am]
-
mtcars [,..(AveMpg = mean(mpg)),by = c( cyl, am )]
mtcars[,.( AveMpg = mean(mpg)), by = "cyl,am"]
mtcars[,.( AveMpg = mean(mpg)), by = c("cyl","am")]
这篇关于在`by`参数中使用字符向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!