title: Iterating function over every row in data frame in R
body: <p>So I have a function <code>flip.allele(a, b, c, d)</code> which takes in 4 arguments and then returns a numerical value based on comparing these different arguments. I am now trying to write an additional function which will take this function and then apply it to a whole data frame, so iterate my the original function across every row in the data frame.</p>
  
  <p>So the four values I want to compare are in columns 2, 3, 4, 5. And I want it to output the value of comparing those four columns into a new column 6.</p>
  
  <p>This is my current attempt at doing this:</p>
  
  <pre><code>flip.data.frame = function(df) {
  
    for (i in nrow(df)) {
  
      df$flip = flip.allele(df[2], df[3], df[4], df[5])
    }
  }
  </code></pre>
  
  <p>The issue is that all of my attempts so far (including this one) have meant that when trying to use it on my data, it takes the first 4 values, correctly applies the <code>flip.allele</code> function and then returns the value for that row into every single row in the data frame.</p>
  
  <p>I know that this is because of the way I'm using the <code>df$flip =</code> bit, but I'm also not sure what a solution would be. I saw <a href="https://stackoverflow.com/questions/1699046/for-each-row-in-an-r-dataframe">another thread</a> which seemed to suggest using <code>apply()</code> but I'm not completely certain how to go about using that.</p>
  
  <p>Any help appreciated.</p>