title: Match two data frames by two columns and extract values from third column
body: <p>I apologize if is a basic or duplicate question, but I am a beginner R user. </p>
  
  <p>I am attempting to match every row in Dataframe A by Sex and Age to the two corresponding columns in Dataframe B. I know there will be a match for sure, so I want to extract values from the matching rows of two different columns in Dataframe B and store them in Dataframe C.</p>
  
  <pre><code>Dataframe A                          Dataframe B
  
  ID   Sex   Age   Weight            Row  Sex  Age   X1    X2
  1     1    24    36                1    1    24   18.2  12.3
  2     1    34    56                2    2    87   15.4  16.5
  3     2    87    12                3    1    64   16.3  11.2 
  4     2    21    08                4    2    21   15.6  14.7
  5     1    64    33                5    1    34   17.7  18.9 
  ...
  
  
  Dataframe C
  ID   Age   Sex  Weight   Y1   Y2
  1     1    24    36     18.2  12.3
  2     1    34    56     17.7  18.9           
  3     2    87    12     15.4  16.5           
  4     2    21    08     15.6  14.7          
  5     1    64    33     16.3  11.2                 
  </code></pre>
  
  <p>There are 9000 IDs in my dataframe. I've looked at similar questions like this one </p>
  
  <p><a href="https://stackoverflow.com/questions/33176171/fill-column-values-by-matching-values-in-each-row-in-two-dataframe">Fill column values by matching values in each row in two dataframe</a></p>
  
  <p>But I don't think this I am applying this code correctly. Will a for loop be useful here?</p>
  
  <pre><code>for(i in 1:nrow(ID){
      dfC[i,Y1] &lt;-df2[match(paste(dfA$Sex,dfa$Age),paste(dfB$Sex,dfB$Age)),"X1"]
  
     dfC[i,Y2] &lt;-df2[match(paste(dfA$Sex,dfa$Age),paste(dfB$Sex,dfB$Age)),"X2"]
  }
  </code></pre>
  
  <p>I know the merge function was also suggested, but these two variables are not actually named the same way in my data set. </p>
  
  <p>Thanks!</p>