Consider the following data.table
:
dt <- data.table(a = 1:5, b = 6:10, c = 11:15)
> dt
a b c
1 1 6 11
2 2 7 12
3 3 8 13
4 4 9 14
5 5 10 15
From the Frequently Asked Questions vignette:
DT[3]
refers to the 3rd row, butDF[3]
refers to the 3rd column.
DT[3, ] == DT[3]
and likewise, from the introduction to data.table vignette, we see that
A comma after the condition in i is not required
, i.e. that in absence of commas the default is that the index is referring to the i
index, the rows.
We want to access the b
column programatically, so we assign the string to a variable colname <- "b"
. However, if we want to get the column vector b
, we can use either of the following:
> dt[, ..colname][[1]]
[1] 6 7 8 9 10
> dt[,get(colname)]
[1] 6 7 8 9 10
> dt[[colname]]
[1] 6 7 8 9 10
The first two options make sense, as they are accessing a column by the j
index, so they include a comma (although in a slightly cumbersome manner). But the third option is accessing a column, with no commas whatsoever. I cannot make sense of this from the introductory data.table documentation, what is happening, is this desired?