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

Chuck Cleland ccleland at optonline.net
Mon Aug 11 18:07:35 CEST 2008


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

-- 
Chuck Cleland, Ph.D.
NDRI, Inc. (www.ndri.org)
71 West 23rd Street, 8th floor
New York, NY 10010
tel: (212) 845-4495 (Tu, Th)
tel: (732) 512-0171 (M, W, F)
fax: (917) 438-0894



More information about the R-help mailing list