title: How to replace values in one df with values from another df using an if statement
body: <p>I have two data frames:</p>
  
  <pre><code>df1 &lt;-data.frame(ID=c(1,2,3,4,5), date=c(NA,NA,NA,NA,NA), outcome=c(NA,1,NA,NA,0))
  df1
    ID date outcome
  1  1   NA      NA
  2  2   NA       1
  3  3   NA      NA
  4  4   NA      NA
  5  5   NA       0
  
  df2 &lt;-data.frame(ID=c(3,25,222,415,700), date=c(010215,032412,040513,041015,120314), outcome=c(1,1,1,1,1))
  df2
     ID   date outcome
  1   3  10215       1
  2  25  32412       1
  3 222  40513       1
  4 415  41015       1
  5 700 120314       1
  </code></pre>
  
  <p>If the ID in df1 is in df2 then I want to replace df1$date with df2$date. Also, if ID in df1 is in df2 I want to set df1$outcome = 1. I can do this with this code:</p>
  
  <pre><code>df1$date &lt;-ifelse(df1$ID %in% df2$ID, df2$date[match(df1$ID,df2$ID)],df1$date)
  df1$outcome &lt;-ifelse(df1$ID %in% df2$ID, 1,df1$outcome)
  df1
    ID  date outcome
  1  1    NA      NA
  2  2    NA       1
  3  3 10215       1
  4  4    NA      NA
  5  5    NA       0
  </code></pre>
  
  <p>but I would like to understand how to do it with one if statement. I have come up with the following code:</p>
  
  <pre><code>for(i in 1:nrow(df1)){
    if(df1$ID[i] %in% df2$ID){
      df1$outcome[i]==1 &amp; df1$date[i]==df2$date[match(df1$ID,df2$ID)]
    }
  }
  
  df1
    ID date outcome
  1  1   NA      NA
  2  2   NA       1
  3  3   NA      NA
  4  4   NA      NA
  5  5   NA       0
  </code></pre>
  
  <p>which runs without errors, but does not complete the desired replacement. Can someone suggest how to modify what I have done to make it work like the first code chunk?</p>