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

2008-08-11 Thread dott
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-

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

2008-08-11 Thread Chuck Cleland
On 8/11/2008 11:26 AM, dott 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 a <- c(2,3,4,8,1) diff(a, lag=2) [1] 2 5 -3 ?diff -- Chuc

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

2008-08-11 Thread Gabor Grothendieck
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 <[EMAIL PROTECTED]> 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 nee

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

2008-08-11 Thread Bert Gunter
p@r-project.org Subject: [R] R-help? how to take difference in next two elements 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 m

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

2008-08-11 Thread dott
Thanks all for the help, that is done. Gabor Grothendieck wrote: > > 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 <[EMAIL PROTECTED]> wrote: >> >> Hi, >> >> I'd like to take d