title: R: memory efficient way to replace column values by index
body: <p>I have two very large dataframes, df1 and df2 with two columns &quot;ID&quot; and &quot;Value&quot;.<br />
  The first dataframe is larger.<br />
  All values in the column &quot;ID&quot; from df2 also occur in df1, but not vice versa.</p>
  <p>I want to replace all values of column &quot;Value&quot; in df1 with the values of df2, for the ID's which df1 and df2 share.</p>
  <p>Although with smaller dataframes this is quite simple (see exaple below), I'm running into memory issues with my larger dataframes (<em>&quot;Error: cannot allocate vector of size X&quot;</em>).<br />
  <strong>I wonder is there is a more memory-efficient way to do the following:</strong></p>
  <pre><code>mydf  &lt;- data.frame(ID = c(1, 3, 4, 2, 5),
                      A  = c(1, 2, 3, 4, 5))
  #&gt;   ID A
  #&gt; 1  1 1
  #&gt; 2  3 2
  #&gt; 3  4 3
  #&gt; 4  2 4
  #&gt; 5  5 5
  
  mydf2 &lt;- data.frame(ID = c(2, 3, 1), 
                      A  = c(&quot;a&quot;, &quot;b&quot;, &quot;d&quot;),
                      stringsAsFactors = FALSE)
  #&gt;   ID A
  #&gt; 1  2 a
  #&gt; 2  3 b
  #&gt; 3  1 d
  
  # Result
  keys  &lt;- match(mydf2$ID, mydf$ID)
  mydf$A[keys] &lt;- mydf2$A
  #&gt;   ID A
  #&gt; 1  1 d
  #&gt; 2  3 b
  #&gt; 3  4 3
  #&gt; 4  2 a
  #&gt; 5  5 5
  </code></pre>
  <p>The error occurs at the last line, (mydf$A[keys] &lt;- mydf2$A), which cannot be completed because of it.</p>