I am working with data.table
. It is easy to select a column from a data.table
object:
> head(data.table(mtcars)[,2])
cyl
1: 6
2: 6
3: 4
4: 6
5: 8
6: 6
But trying to select a column using this syntax within a ecdf
call yields an error:
> ecdf(data.table(mtcars)[,2])(data.table(mtcars)[,2])
Error in
[.data.frame
(x, i) : undefined columns selected
Can someone explain me why please?
Pragmatically, one way around this is to do:
> ecdf(data.table(mtcars)[[2]])(data.table(mtcars)[[2]])
[1] 0.56250 0.56250 0.34375 0.56250 1.00000 0.56250 1.00000 0.34375 0.34375 0.56250 0.56250 1.00000 1.00000 1.00000 1.00000 1.00000
[17] 1.00000 0.34375 0.34375 0.34375 0.34375 1.00000 1.00000 1.00000 1.00000 0.34375 0.34375 0.34375 1.00000 0.56250 1.00000 0.34375
but I would like to understand the behavior above.
The reason is in the extraction. In the first case, it is still a a data.table, while in second case it is a
vector
data.table
anddata.frame
syntax are slightly different. INdata.table
, the[
, by default usesdrop = TRUE
. So, using,
and selecting only a single column will drop the dimensions to become avector
It is also mentioned in the data.table-faq