[R] how to get growth rate of a (time series) data?

Blaser Nello nblaser at ispm.unibe.ch
Wed Jun 19 13:50:34 CEST 2013

diff(test$Y)/(test$Y)[-1] calculates (Y(t)-Y(t-1))/Y(t). 

To get (Y(t)-Y(t-1))/Y(t-1) instead, use 
or better
diff(test[,"Y"])/test[-nrow(test), "Y"]

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
On Behalf Of Yanyuan Zhu
Sent: Mittwoch, 19. Juni 2013 13:04
To: r-help at r-project.org
Subject: [R] how to get growth rate of a (time series) data?

Hello all, now I'm trying to switch from Excel to R to deal with the
data, and as a newbie i got the problem as follows.

suppose I have a data named "test"
test<- data.frame(year=c(1996:2011),
in which Y means the GDP of a country

If i want to get Delta Y = Y(t)-Y(t-1) , i could use diff() in R

but what if i want to get gY=(Y(t)-Y(t-1))/Y(t-1)?
seems diff(test$Y)/(test$Y)[-1] doesnt work ...

thanks in advance

	[[alternative HTML version deleted]]

R-help at r-project.org mailing list
PLEASE do read the posting guide
and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list