[R] R-help? how to take difference in next two elements

Gabor Grothendieck ggrothendieck at gmail.com
Mon Aug 11 18:13:55 CEST 2008


Here are a couple of ways:

> tail(a, -2) - head(a, -2)
[1]  2  5 -3
> c(ts(a) - lag(ts(a), -2))
[1]  2  5 -3


On Mon, Aug 11, 2008 at 11:26 AM, dott <dorothy99 at gmail.com> wrote:
>
> Hi,
>
> I'd like to take difference for a sequence a between a_i and a_i-2, for
> instance,
> a<-c(2,3,4,8,1)
> I need (2, 5, -3) as a result. If not using a for loop, can anyone help me?
> Thanks a lot.
>
> Dot
> --
> View this message in context: http://www.nabble.com/R-help--how-to-take-difference-in-next-two-elements-tp18927968p18927968.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list