title: R: creating a new column in a data.frame based on values out of a second data.frame
body: <p>I have two (example) data.frames (df1, df2) </p>
  
  <pre><code>#df1
  L &lt;- LETTERS[1:4]
  b &lt;- sample(L, 20, replace = TRUE)
  df1 &lt;- data.frame(stuff1 = 1, stuff2 = 1:10, b = b, c= NA, stringsAsFactors=FALSE)
  
  #df2
  a &lt;- c(10,20,30,40)
  df2 &lt;- data.frame(xx = L, yy = a, stringsAsFactors=FALSE )
  </code></pre>
  
  <p>i want to have a new column, let's say <code>c</code>, in <code>df1</code> based on the values out of <code>df2</code>. One example: <code>A</code> has the corresponding value of 10 (see <code>df2</code>) so for every A in column <code>b</code> of <code>df1</code> should be 10 written down in the (new) line <code>c</code>. And this for every row of <code>xx</code> in <code>df2</code>, so in this case A,B,C and D. My code is not really working and is based only for a single value, here <code>A</code>:</p>
  
  <pre><code>##copy column b now it is c
  df1["c"] &lt;- df1$b
  
  
  # which value has A in df2?
  zz &lt;- df2[df2$xx == "A",]
  xy &lt;- zz$yy
  
  
  # fill in the new value in c (this is not working)
  df1[df1$c == "A", ] &lt;- xy 
  </code></pre>
  
  <p>i hope it is clear what i want to say... oh and i have some big data this is only an example to try out... </p>