I am still new to R, so please excuse if this is basic question. I have the following dataframe:
df <- data.frame(a=c(2,4,6,8), x = c(1,0,0,0), y=c(0,1,0,0), z=c(0,0,0,1))
> df
a x y z
1 2 1 0 0
2 4 0 1 0
3 6 0 0 0
4 8 0 0 1
I define the function sumfunc as
sumfunc <- function(a,x,y,z){a*2+x+y-z}
So each row has the inputs a, x,y, z and I am expecting a vector of 4 elements.
- element 1 = 5 (2*2+1+0-0)
- element 2 = 9 (4*2+0+1-0)
- element 3 = 12(6*2+0+0-0)
- element 4 = 15 (8*2+0+0-1)
I tried using
abc <- lapply(df$a, FUN=sumfunc, x=df$x, y=df$y, z=df$z)
But this is not giving me the desired output. Kindly advice which apply family function is the right one for my use-case.
thanks very much!