I have a dataframe:
df <- data.frame(\'a\'=c(1,2,3,4,5), \'b\'=c(1,20,3,4,50)) df a b 1 1 1 2 2 20 3 3 3 4 4 4 5 5 50
A solution with apply
apply
myFunction <- function(x){ a <- x[1] b <- x[2] #further values ignored (if there are more than 2 columns) value <- if(a==b) a + b else b - a #or more complicated stuff return(value) } df$c <- apply(df, 1, myFunction)