title: Loop row wise over dataframe and apply a if-else
body: <p>I have a dataframe named "final" with five columns as shown below:</p>
  
  <pre><code>quest  opt1  opt2  opt3 opt4   answer
  asdsf    as   pe    dsf  qqqq    A
  asdsf    sa   pe    dsf  qqqq    B
  asdsf    ee   pe    dsf  qqqq    C
  .
  .
  .
  2000 rows
  </code></pre>
  
  <p>What I want is if </p>
  
  <pre><code>final$answer[1] has A then final$answer[1] &lt;- final$opt1[1]
  if final$answer[1] has B then final$answer[1] &lt;- final$opt2[1] 
  final$answer[1] has C then final$answer[1] &lt;- final$opt3[1]
  final$answer[1] has D then final$answer[1] &lt;- final$opt4[1]
  </code></pre>
  
  <p>...so on for all rows of the dataframe.</p>
  
  <p>So I wrote below loop and if-else and though it works.
  Please suggest a better option if available ?</p>
  
  <pre><code>for (i in 1:nrow(final)){
  
    if (final$answer[i] == "A"){
  
     final$answer[i] &lt;- final$opt1[i];
                             }
  else if (final$answer[i] == "B"){
  final$answer[i] &lt;- final$opt2[i];
  
  }
  else if(final$answer[i] == "C"){
  final$answer[i] &lt;- final$opt3[i];
  
  }
  else{
  final$answer[i] &lt;- final$opt4[i];
  }
      }
  </code></pre>