[R] calculations combining values from different rows

Sebastián Daza sebastian.daza at gmail.com
Fri Feb 10 03:25:32 CET 2012


Hi everyone,
I looking for functions or systematic ways to do calculations between
different columns and rows in R, as one can do easily in Excel. For
example, I have two variables, a and b, where a1 represents an a value
in row 1, and b2 represents a b value in row 2, etc.

a  <- c(4,3,5,5,6,7,3,2,1,4)
b  <- c(2,4,1,2,5,3,1,8,7,5)
data  <- cbind(a,b)

I have to calculate something like this:

x1 = NA
x2 = -b1 /24 * a1 + b2 /2 * a2 + b3 /24 * a3
x3 = -b2 /24 *a2 + b3 /2 * a3 + b4 /24 * a4
x4 = -b3 /24 *a3 + b4 /2 * a4 + b5 /24 * a5
...
x9 = -b8 /24* a8 + b9 /2 * a9 + b10 /24 * a10
x10= NA

For example, x2 would be equal to: -2/24*4 +4/2*3 + 1/24 *5

Any ideas?
Thank you in advance.

-- 
Sebastián Daza



More information about the R-help mailing list