title: How to swap values between two columns
body: <p>I have a data frame with three variables and 250K records. As an example consider </p>
  
  <pre><code>df &lt;- data.frame(V1=c(1,2,4), V2=c("a","a","b"), V3=c(2,3,1))
  V1 V2 V3
  1  a  2
  2  a  3
  4  b  1
  </code></pre>
  
  <p>and want to swap values between V1 and V3 based on the value of V2 as follows: 
  if <code>V2 == 'b'</code> then <code>V1 &lt;- V3</code> and <code>V3 &lt;- V1</code>
  resulting in</p>
  
  <pre><code>V1 V2 V3
  1  a  2
  2  a  3
  1  b  4
  </code></pre>
  
  <p>I tried a do loop but it takes forever. If I use Perl, it takes seconds. I believe this task can be done efficiently in R as well. Any suggestions are appreciated.</p>