[R] how to get growth rate of a (time series) data?
rolf.turner at xtra.co.nz
Thu Jun 20 00:16:24 CEST 2013
On 19/06/13 23:24, R. Michael Weylandt wrote:
> On Wed, Jun 19, 2013 at 12:04 PM, Yanyuan Zhu <yyz at tongji.edu.cn> wrote:
>> 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 ...
> Odd, I would have thought it did.
No, "clearly" ( :-) ) it doesn't. It gives
(Y - Y)/Y, (Y-Y)/Y, .... when what is wanted is
(Y - Y)/Y, (Y-Y)/Y, ....
The OP needs to do:
This is yet another illustration of the rule that if there is a 50-50
getting things the wrong way around, then there is actually a probability
of one of getting things the wrong way around.
More information about the R-help