Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to pass a parameter by variable into data.table[J()]
    primarykey
    data
    text
    <p>I'm brand new to the (completely marvelous) <code>data.table</code> package, and seem to have gotten stuck on a very basic, somewhat bizarre problem. I can't post the exact data set I'm working with, for which I apologize -- but I think the problem is simple enough to articulate that hopefully this will still be very clear.</p> <p>Let's say I have a data.table like so, with key x: </p> <pre><code>set1 x y 1: 1 a 2: 1 b 3: 1 c 4: 2 a </code></pre> <p>I want to return a subset of <code>set1</code> containing all rows where <code>x == 1</code>. This is wonderfully simple in data.table: <code>set1[J(1)]</code>. Bam. Done. I can also assign <code>z &lt;- 1</code>, and call <code>set1[J(z)]</code>. Again: works great. </p> <p>...except when I try to scale it up to my actual data set, which contains ~6M rows. When I call <code>set1[J(1674)]</code>, I get back a 78-row return that's exactly what I'm looking for. But I need to be able to look up (literally) 4M of these subsets. When I assign the value I'm searching for to a variable, <code>id &lt;- 1674</code>, and call <code>set1[J(id)]</code>... R nearly takes down my desktop. </p> <p>Clearly <em>something</em> I don't understand is going on under the data.table hood, but I haven't been able to figure out what. Googling and slogging through Stack Overflow suggest that this should work. Out of pure whimsey, I've tried:</p> <pre><code>id &lt;- quote(1674) set1[J(eval(id))] </code></pre> <p>...but that is far, far worse. What... what's going on? </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload