Calculate percentage change in an R data frame

后端 未结 2 1714
一向
一向 2020-12-23 02:18

I have a time series object in R with multiple vectors. I would like to calculate the period-over-period percentage change at each point in time (save t = 1, which would obv

2条回答
  •  半阙折子戏
    2020-12-23 02:51

    Something like this?

    > diff(data)/data[-nrow(data),] * 100
                   x1       x2       x3
    2010 Q4 100.00000 9.090909 4.761905
    2011 Q1  50.00000 8.333333 4.545455
    2011 Q2  33.33333 7.692308 4.347826
    2011 Q3  25.00000 7.142857 4.166667
    2011 Q4  20.00000 6.666667 4.000000
    2012 Q1  16.66667 6.250000 3.846154
    2012 Q2  14.28571 5.882353 3.703704
    2012 Q3  12.50000 5.555556 3.571429
    2012 Q4  11.11111 5.263158 3.448276
    

提交回复
热议问题