Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Good question. Note the following (admittedly buried) in <code>?data.table</code> :</p> <blockquote> <p>When <code>i</code> is a <code>data.table</code>, <code>x</code> must have a key. <code>i</code> is joined to <code>x</code> using the key and the rows in <code>x</code> that match are returned. An equi-join is performed between each column in <code>i</code> to each column in <code>x</code>'s key. The match is a binary search in compiled C in O(log n) time. If <code>i</code> has less columns than <code>x</code>'s key then many rows of <code>x</code> may match to each row of <code>i</code>. If <code>i</code> has more columns than <code>x</code>'s key, the columns of <code>i</code> not involved in the join are included in the result. <strong>If <code>i</code> also has a key</strong>, it is <code>i</code>'s key columns that are used to match to <code>x</code>'s key columns and a binary merge of the two tables is carried out.</p> </blockquote> <p>So, the key here is that <code>i</code> doesn't have to be keyed. Only <code>x</code> must be keyed.</p> <pre><code>X2 &lt;- data.table(id = 11:15, y_id = c(14,14,11,12,12), key="id") id y_id [1,] 11 14 [2,] 12 14 [3,] 13 11 [4,] 14 12 [5,] 15 12 Y2 &lt;- data.table(id = 11:15, b = letters[1:5], key="id") id b [1,] 11 a [2,] 12 b [3,] 13 c [4,] 14 d [5,] 15 e Y2[J(X2$y_id)] # binary search for each item of (unsorted and unkeyed) i id b [1,] 14 d [2,] 14 d [3,] 11 a [4,] 12 b [5,] 12 b </code></pre> <p>or,</p> <pre><code>Y2[SJ(X2$y_id)] # binary merge of keyed i, see ?SJ id b [1,] 11 a [2,] 12 b [3,] 12 b [4,] 14 d [5,] 14 d identical(Y2[J(X2$y_id)], Y2[X2$y_id]) [1] FALSE </code></pre>
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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