title: How to add a column to a dataframe with values of another based on multiple conditions
body: <p>I have two data frames of different length, and I want to add a new column to the first data frame with corresponding values of the second data frame.  </p>
  
  <p>The corresponding value is defined by the following condition <code>if (DF1[i,1] == DF2[,1] &amp; DF1[i,2] == DF2[i,2]) == TRUE</code>, then the value of this row should be taken from DF2 and written to <code>DF1$newColumn[i]</code>.</p>
  
  <p>The following data frames are used to illustrate the question:</p>
  
  <pre><code>DF1&lt;-data.frame(X = rep(c("A","B","C"),each=3),
                  Y = rep(c("a","b","c"),each=3))
  
  DF2&lt;-data.frame(X = c("A","B","C"),
                  Y = c("a","b","c"),
                  Z = c(1:3))
  </code></pre>
  
  <p>I tried to use <code>if()</code> statements as in the text above but the condition returns a vector of TRUE/FALSE and that doesn't seem to work.</p>
  
  <p>The code that works that I use now is</p>
  
  <pre><code>  for (i in 1 : length(DF1[,1])) {
    DF1$Z[i] &lt;- subset(DF2,DF2$X == DF1$X[i] &amp; DF2$Y == DF1$Y[i])$Z               
  }
  </code></pre>
  
  <p>However it is incredibly slow (user  system elapsed 115.498  12.341 127.799 for my full dataframe) and there must be a more efficient way to code this. Also, I have read repeatedly that vectorizing is more efficient then loops but I don't know how to do that. </p>
  
  <p>I do need to work with conditional statements though so something like </p>
  
  <p><code>DF1$Zz&lt;-rep(DF2$Z,each=3)</code></p>
  
  <p>wouldn't work for my real dataset.</p>