如何从data.table中的`.BY`中受益? [英] How to benefit from `.BY` in data.table?
问题描述
这是 .BY
.BY
是一个列表,其中包含by
中每个项目的长度为1的向量。如果事先不知道by
,这将很有用。by
变量也可以按名称直接用于j
;例如,如果j
是绘图命令,则对于图形标题很有用,或者根据if()
.BY
is a list containing a length 1 vector for each item inby
. This can be useful whenby
is not known in advance. Theby
variables are also available toj
directly by name; useful for example for titles of graphs ifj
is a plot command, or to branch withif()
depending on the value of a group variable.
它说例如,对于 j
是绘图命令,或者根据组变量的值使用 if()
进行分支。
It says "useful for example for titles of graphs if j
is a plot command, or to branch with if()
depending on the value of a group variable."
但是,我仍然不确定何时使用。如何从此 .BY
中受益?
But still, I'm not sure when to use.how to benefit from this .BY
?
您能举一个例子吗?非常感谢!
Could you give one example? Thanks a lot!
推荐答案
这里是一个简单的示例。我们使用 .BY
变量来显示该图属于哪个组。请注意,您也可以使用 gear [1]
代替 .BY
来执行此操作。
Here is a simple example. We are using the .BY
variable to show which group the plot belongs to. Note that you can also do it without using .BY
, by replacing it with gear[1]
, which will be equivalent.
library(data.table)
mtcars_dt = data.table(mtcars)
mtcars_dt[,
plot(wt, mpg, main = paste('Gears: ', .BY)),
gear
]
这篇关于如何从data.table中的`.BY`中受益?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!