This is the explanation in manual of .BY
.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.
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."
But still, I'm not sure when to use.how to benefit from this .BY
?
Could you give one example? Thanks a lot!
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
]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With