I am new to R
I have a data frame STAT.VECTORS with two columns A and B. I need to add column C to STAT.VECTORS such that C[i] = func( A[i] , B[i] )
What is the most syntactically efficient way to do this in R?
Any way to extend the solution such that C[i] = func( A[i-1] , B[i-2] )
Having a developer background, I would create a loop and assign values to C. I expect better from R:)
UPDATE
The following example should elaborate my question. The loop picks specific elements relative to the current row and hand them to a function.
Can I do the loop logic below without loop by using apply family?
l=50
func=function(x,y){return(y-x)}
a=sample(1:6,l,replace = T)
b=sample(1:6,l,replace = T)
c=sample(1:6,l,replace = T)
d=sample(1:6,l,replace = T)
e=sample(1:6,l,replace = T)
f=vector("numeric",l)
test.data = cbind(a,b,c,d,e,f)
for(i in 2:l)
{
test.data[i,"f"]= func(test.data[i,"d"],test.data[i-1,"b"])
}
test.data