Skip to content
Prev 343135 / 398513 Next

Operating on the value from row i and row i+1

You didn?t say why you want it to return 6 and 4 for times 4 and 12
respectively, so I made an assumption. On that basis, try this example:


mydf <- data.frame(time=c(0,3,9), resp=c(5,6,4))

myint <- approx( mydf$time, mydf$resp, xout=c(6,12),
                 method='constant', f=0, rule=2)

It reproduces your two example desired results.

print(myint)
$x
[1] 6 12
$y
[1] 6 4


(aside)
If my assumption is correct, this is an example of a case where a simple
R-supplied function does the job and there?s no need to use anything else.
Simple tools for simple jobs. The approx() function has been in R since
the very beginning.