title: Replace value in column with corresponding value from another column in same dataframe
body: <p>Im trying to match a specific value in one column and replace it with the corresponding value from another column (same row). This is probably very easy... I have been trying to find a solution with <code>for loop</code>, <code>sub</code>, <code>subset</code>, <code>data.table</code> but I have not succeeded. There must be a neat way of doing this.</p>
  
  <p>Example data, where we aim at swapping <code>a</code> in the first column with the corresponding value in the second column and outputting the column again.</p>
  
  <pre><code>df &lt;- data.frame(rbind(c('a','D'),c('H','W'),c('Q','E'),c('a','F'),c('U','P'),c('a','B')))
  
  df$X1 &lt;- as.character(df$X1)
  df$X2 &lt;- as.character(df$X2)
  
  # not working
  for (i in seq_along(df$X1)){
    a &lt;- df$X1[i]
    b &lt;- df$X2[i]
    x &lt;- ifelse(a[i=='a'], a[i]&lt;-b[i], do.nothing )
    print(x)
  }
  </code></pre>
  
  <p>The output would be like this;</p>
  
  <pre><code>   X1 X2
  1  D  a
  2  H  W
  3  Q  E
  4  F  a
  5  U  P
  6  B  a
  </code></pre>
  
  <p>(The switch isn't necessary). It's the first column Im interested in.</p>
  
  <p>Any pointer would be appreciated, thanks!</p>