Starting with a simple matrix and a simple function:
numbers <- matrix(c(1:10), nrow = 5, ncol=2)
numbers
[,1] [,2]
[1,] 1 6
[2,] 2 7
[3,] 3 8
[4,] 4 9
[5,] 5 10
add <- function(x,y){
sum <- x + y
return(sum)
}
I'd like to add a third column that applies the add function by taking the first two elements of each row.
cheet_sheet <- cbind(numbers, apply(numbers,<MARGIN=first_2_elements_of_row>, add))
The MARGIN seems like a natural place to specify this, but MARGIN=1 is not enough, as it seems to only take one variable from each row while I need two.