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

dott dorothy99 at gmail.com
Mon Aug 11 20:18:46 CEST 2008


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 <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.
>>
> 
> ______________________________________________
> 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.
> 
> 

-- 
View this message in context: http://www.nabble.com/R-help--how-to-take-difference-in-next-two-elements-tp18927968p18930532.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list