Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I am not sure if I understand the problem well, and I also just started to read the docs of <em>data.table</em> library, but I think if you would like to get the columns of <em>y</em> and also do something to those by the columns of <em>a</em>, you might try something like:</p> <pre><code>&gt; x[y,a*y] foo boo [1,] 5 50 [2,] 8 44 [3,] 9 36 [4,] 8 26 [5,] 5 14 </code></pre> <p>Here, you get back the columns of <em>y</em> multiplied by the <em>a</em> column of <em>x</em>. If you want to get <em>x</em>'s <em>foo</em> multiplied by <em>y</em>'s <em>boo</em>, try:</p> <pre><code>&gt; y[,x*boo] foo a [1,] 10 50 [2,] 22 44 [3,] 36 36 [4,] 52 26 [5,] 70 14 </code></pre> <hr> <p><strong>After editing:</strong> thank you @Prasad Chalasani making the question clearer for me.</p> <p>If simple merging is preferred, then the following should work. I made up a more complex data to see the actions deeper:</p> <pre><code>x &lt;- data.table( foo = 1:5, a=20:24, zoo = 5:1 ) y &lt;- data.table( foo = 1:5, b=30:34, boo = 10:14) setkey(x, foo) setkey(y, foo) </code></pre> <p>So only an extra column was added to each data.table. Let us see <code>merge</code> and doing it with <code>data.tables</code>:</p> <pre><code>&gt; system.time(merge(x,y)) user system elapsed 0.027 0.000 0.023 &gt; system.time(x[,list(y,x)]) user system elapsed 0.003 0.000 0.006 </code></pre> <p>From which the latter looks a lot faster. The results are not identical though, but can be used in the same way (with an extra column of the latter run):</p> <pre><code>&gt; merge(x,y) foo a zoo b boo [1,] 1 20 5 30 10 [2,] 2 21 4 31 11 [3,] 3 22 3 32 12 [4,] 4 23 2 33 13 [5,] 5 24 1 34 14 &gt; x[,list(x,y)] foo a zoo foo.1 b boo [1,] 1 20 5 1 30 10 [2,] 2 21 4 2 31 11 [3,] 3 22 3 3 32 12 [4,] 4 23 2 4 33 13 [5,] 5 24 1 5 34 14 </code></pre> <p>So to get <code>xy</code> we might use: <code>xy &lt;- x[,list(x,y)]</code>. To compute a one-column data.table from <code>xy$foo * xy$boo</code>, the following might work:</p> <pre><code>&gt; xy[,foo*boo] [1] 10 22 36 52 70 </code></pre> <p>Well, the result is not a data.table but a vector instead.</p> <hr> <p><strong>Update (29/03/2012):</strong> thanks for @David for pointing my attention to the fact that <code>merge.data.table</code> were used in the above examples.</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.
    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