<div class="gmail_quote">On Mon, Aug 6, 2012 at 1:31 PM, Matthew Dowle <span dir="ltr"><<a href="mailto:mdowle@mdowle.plus.com" target="_blank">mdowle@mdowle.plus.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="im">> Hello,<br>
><br>
> I'm running into an odd behavior with data.table. Given the following<br>
> variables:<br>
><br>
>> tbl = data.table(foo=c(1,2,3), bar=c(1.1, 2.2, 3.3))<br>
>> setkey(tbl, foo)<br>
>> i = data.frame(foo=1)<br>
<br>
</div>And<br>
i = data.table(foo=1)<br>
gives the same results below, so it doesn't seem related to whether i is a<br>
data.table -vs- a data.frame.<br>
<div class="im"><br>
><br>
> ... I would expect the following three ways of indexing "tbl" using "i" to<br>
> give the same result, but they don't:<br>
><br>
>> tbl[i]<br>
> foo bar<br>
> 1: 1 1.1<br>
>> tbl[J(i)]<br>
> Error in `[.data.table`(tbl, J(i)) :<br>
> typeof x.foo (double) != typeof i.V1 (list)<br>
>> tbl[data.table(i)]<br>
> foo bar<br>
> 1: 1 1.1<br>
><br>
> Anything I'm missing on why tbl[J(i)] wouldn't work like the other two? Or<br>
> have I hit a bug? I'm running R 2.15.1 64bit on Windows 7, with data.table<br>
> 1.8.2.<br>
<br>
</div>J inside [] is an alias for list(), not data.table(). I don't think this<br>
changed in 1.8.2 but might be wrong. data.table() is much heavier than<br>
list() checking argument types up front and recycling vectors to ensure<br>
each item of the data.table has the same length, for instance.<br>
data.table() also unpacks data.frame and data.table arguments like a cbind<br>
would. Whereas list() treats data.table and data.frame arguments as though<br>
they are list columns.<br>
<br>
So it's doing something sensible and is correct behaviour on first glance.<br>
Please confirm this makes sense, and if so I'll add as as FR to improve<br>
error message and documentation.<br></blockquote></div><br>Okay, this makes sense. But pretty much the whole of the documentation on that topic is wrong, then. Hence my confusion.<br><br>As a single example (there are others), in the "Introduction to the data.table package in R" document, page 6:<br>
<br><blockquote style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex" class="gmail_quote">[...] Since we do this a lot, there is an alias for data.table called J(), short for join.<br></blockquote>
<div><br>Thanks for your help,<br><br> Christian<br><br></div>