如何从data.table中的`.BY`中受益? [英] How to benefit from `.BY` in data.table?

查看:101
本文介绍了如何从data.table中的`.BY`中受益?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是 .BY


.BY 是一个列表,其中包含 by 中每个项目的长度为1的向量。如果事先不知道 by ,这将很有用。 by 变量也可以按名称直接用于 j ;例如,如果 j 是绘图命令,则对于图形标题很有用,或者根据 if()

.BY is a list containing a length 1 vector for each item in by. This can be useful when by is not known in advance. The by variables are also available to j directly by name; 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.

它说例如,对于 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆