6

I am trying to get a better grasp on how some of the special variables in the data.table package work. One of these is the .BY statement. I have not seen a lot of examples of people using it, but the documentation implies that is is useful in plotting.

For example, the following code seems to work quite well (showing a plot for each of the species and assigning the right title to each plot) in data.table 1.9.3:

iris <- data.table(iris)
iris[,plot(Sepal.Length ~ Sepal.Width, main = unlist(.BY)), by = Species]

While this code does not work as intended by me:

iris[ , plot(Sepal.Length ~ Sepal.Width, main = .BY), by = Species]

Why are these two different? From the comments, it does not seem to be an issue in data.table 1.9.2. In what other ways might it be useful to use the .BY statement? How is this different compared to the .EACHI statement?

Henrik
  • 65,555
  • 14
  • 143
  • 159
Mike.Gahan
  • 4,565
  • 23
  • 39
  • I get identical results using both of your statements. What is supposed to be different? – thelatemail Jun 11 '14 at 01:26
  • 1
    Interesting...I just tried this with ```data.table``` 1.9.2 and I got identical results (but with the titles being 1,2, and 3). But in 1.9.3, I get different results for the two pieces of code (one with the Species names and one returns an error). – Mike.Gahan Jun 11 '14 at 01:37
  • 2
    Yep, I'm using 1.9.2 - it's probably worth editing your post to include that this is an issue present in 1.9.3. – thelatemail Jun 11 '14 at 01:42

1 Answers1

4

.BY is a named list containing the values of the by variables.

Passing an unnamed list to main will work, however a named list will fail (wholly unrelated to data.table

plot(1, main = list(1))
# works....
plot(1, main = list(s=1))
# Error in title(...) : invalid graphics parameter

There is a recent commit to data.table 1.9.3 which fixed a bug to do with naming in `.BY Closes bug #5415. .BY gets names attribute set properly in april this year.

If you had more than 1 "by" variable, you would want to be able to concatenate some how

perhaps

iris[,plot(Sepal.Length~Sepal.Width,main=do.call(paste,.BY)),by=Species]

will work (unless you have a column called collapse!)

EACHI is completely unrelated to this. Please read the NEWS for data.table 1.9.3 for an understanding of this.

sebastian-c
  • 15,057
  • 3
  • 47
  • 93
mnel
  • 113,303
  • 27
  • 265
  • 254