For these sorts of questions ("how do I compute XXX by category YYY")? there are always solutions based on by()
, the data.table()
package, and plyr
. I generally prefer plyr
, which is often slower, but (to me) more transparent/elegant.
df <- data.frame(Category=c(rep("A",6),rep("B",6)),
Year=rep(2010:2015,2),Value=1:12)
library(plyr)
ddply(df,"Category",transform,
Growth=c(NA,exp(diff(log(Value)))-1))
The main difference between this answer and @krlmr's is that I am using a geometric-mean trick (taking differences of logs and then exponentiating) while @krlmr computes an explicit ratio.
Mathematically, diff(log(Value))
is taking the differences of the logs, i.e. log(x[t+1])-log(x[t])
for all t
. When we exponentiate that we get the ratio x[t+1]/x[t]
(because exp(log(x[t+1])-log(x[t])) = exp(log(x[t+1]))/exp(log(x[t])) = x[t+1]/x[t]
). The OP wanted the fractional change rather than the multiplicative growth rate (i.e. x[t+1]==x[t]
corresponds to a fractional change of zero rather than a multiplicative growth rate of 1.0), so we subtract 1.
I am also using transform()
for a little bit of extra "syntactic sugar", to avoid creating a new anonymous function.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…