5

the data.table vignette defines the special symbol .BY as

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

This description is confusing to me. How and when would I use .BY? Even for titles of graphs? Why was it added as a special symbol?

example("data.table") doesn't have any examples, and I haven't found any on the data.table wiki page. I don't know how to search on stack overflow for data.table and .BY usages either. I keep getting hits that use the by operator in data.table

  • 2
    Here's a slightly trickier use of `.BY` from an old answer of mine too: http://stackoverflow.com/questions/25129750/data-table-drop-key-rows-and-summarize/25130426 – thelatemail Oct 19 '16 at 05:48
  • 1
    ...and yes, it is tough to search for. Maybe dot-BY or something could be used to make it easier. – thelatemail Oct 19 '16 at 06:04
  • 1
    Related: [r - Use of .BY and .EACHI in the data.table package](http://stackoverflow.com/questions/24153174/use-of-by-and-eachi-in-the-data-table-package). Regarding your "_I don't know how to search on stack overflow for data.table and .BY_", it's generally more efficient to use google instead of searching from within SO (see e.g. [here](http://meta.stackoverflow.com/questions/286485/stack-overflow-search-returns-no-results-but-google-search-returns-2-000) and links therein). The link above was the second hit on my google search '`R data.table ".BY"`'. – Henrik Oct 19 '16 at 06:43
  • 1
    https://github.com/Rdatatable/data.table/issues/1363 – Frank Oct 19 '16 at 11:02
  • 2
    Also be aware that documentation about that has been recently improved, up-to-date manual can be found in [?"special-symbols"](http://jangorecki.gitlab.io/data.table/library/data.table/html/special-symbols.html). – jangorecki Oct 19 '16 at 14:29

1 Answers1

4

Here's a simple example showing how .BY can be passed through to further arguments like a plot title. Using the built-in mtcars data:

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

enter image description here

thelatemail
  • 91,185
  • 12
  • 128
  • 188
  • 3
    In this particular case it's simple to just replace `.BY` with `cyl`. `.BY` is actually more useful when we do not know in advanced what the grouping column will be, so we can refer to it from `j` expression. Your example could be ideally wrapped in a function that takes arbitrary column to group by. – jangorecki Oct 19 '16 at 14:33