r - R data.table `.BY` 运算符如何使用?

标签 r data.table

data.table 小插图定义了特殊符号 .BY作为

.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.

这个描述让我感到困惑。我如何以及何时使用.BY ?即使是图表标题?为什么将其添加为特殊符号?

example("data.table")没有任何示例,我在 data.table wiki 页面上也没有找到任何示例。我也不知道如何在堆栈溢出上搜索 data.table 和 .BY 用法。我不断收到使用 by 的点击data.table 中的运算符

最佳答案

这是一个简单的示例,展示了如何将 .BY 传递给进一步的参数,例如绘图标题。使用内置 mtcars 数据:

mtcars <- as.data.table(mtcars)
layout(1:3)
mtcars[, plot(mpg, main=paste("Cylinders:", as.character(.BY))), by=cyl]

enter image description here

关于r - R data.table `.BY` 运算符如何使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40122918/

相关文章:

r - 如何更改雪花 SQL 的 dplyr show_query() 引用约定

r - 数据表 "key indices"或 "group counter"

r - data.table 与两个键值合并

r - 通过引用删除整个 data.table

r - 同时对数据框的特定列进行子集化和操作

r - 具有多个组的 3D 密度图(回归线)

r - 使用来自相同键索引的值填充空白字段

从 data.table groupby 模型中恢复 lm() 结果

r - 将单列传递给 j 和从 data.table 中选择同一列有什么区别?

在r中重新编码数据