[datatable-help] Odd behavior with [.data.table and J

Matthew Dowle mdowle at mdowle.plus.com
Mon Aug 6 19:31:32 CEST 2012

> Hello,
> I'm running into an odd behavior with data.table. Given the following
> variables:
>> tbl = data.table(foo=c(1,2,3), bar=c(1.1, 2.2, 3.3))
>> setkey(tbl, foo)
>> i = data.frame(foo=1)

   i = data.table(foo=1)
gives the same results below, so it doesn't seem related to whether i is a
data.table -vs- a data.frame.

> ... I would expect the following three ways of indexing "tbl" using "i" to
> give the same result, but they don't:
>> tbl[i]
>    foo bar
> 1:   1 1.1
>> tbl[J(i)]
> Error in `[.data.table`(tbl, J(i)) :
>   typeof x.foo (double) != typeof i.V1 (list)
>> tbl[data.table(i)]
>    foo bar
> 1:   1 1.1
> Anything I'm missing on why tbl[J(i)] wouldn't work like the other two? Or
> have I hit a bug? I'm running R 2.15.1 64bit on Windows 7, with data.table
> 1.8.2.

J inside [] is an alias for list(), not data.table(). I don't think this
changed in 1.8.2 but might be wrong. data.table() is much heavier than
list() checking argument types up front and recycling vectors to ensure
each item of the data.table has the same length, for instance.
data.table() also unpacks data.frame and data.table arguments like a cbind
would. Whereas list() treats data.table and data.frame arguments as though
they are list columns.

So it's doing something sensible and is correct behaviour on first glance.
Please confirm this makes sense, and if so I'll add as as FR to improve
error message and documentation.

A simple case where a list column in i will in future validly match to an
atomic column in x's key is FR#203 "Allow 2 column to specify range in i
instead of %between%", which contains some links to other discussions :

Allowing list columns in a key is something different, and again not yet
implemented, but that might be a future feature request.


> Thanks,
>   Christian
> _______________________________________________
> datatable-help mailing list
> datatable-help at lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help

More information about the datatable-help mailing list