Skip to content
Prev 243260 / 398500 Next

How do I subtract sequential values ?

Hi Eric,

I think this does what you want.  It may be a simple question, but
that does not necessarily mean the easiest/fastest answer is
intuitive.  Welcome to R!

dat <- data.frame(v = 1:100)
## the with() is just a convenient way to avoid having to type
## dat$v every single time---similar to attach(), but cleaner
## the idea is to create three vectors, that are offset by removing
## either the first observation or the last
with(dat, (v[-1] - v[-length(v)])/v[-length(v)])

HTH,

Josh
On Sun, Nov 28, 2010 at 7:29 PM, eric <ericstrom at aol.com> wrote: