So I have a function flip.allele(a, b, c, d)
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.
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.
This is my current attempt at doing this:
flip.data.frame = function(df) {
for (i in nrow(df)) {
df$flip = flip.allele(df[2], df[3], df[4], df[5])
}
}
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 flip.allele
function and then returns the value for that row into every single row in the data frame.
I know that this is because of the way I'm using the df$flip =
bit, but I'm also not sure what a solution would be. I saw another thread which seemed to suggest using apply()
but I'm not completely certain how to go about using that.
Any help appreciated.