Skip to content
Prev 366108 / 398502 Next

Computing growth rate

Dear Mr. Barradas,

Thanks a lot for pointing that. I tried that in a few steps-
1. when I evaluated

d<-ddply(df1,"co_code1",transform, growth <- ifelse(diff(fyear1)==1,
(exp(diff(log(df1$sales1)))-1)*100, NA))

I got the following, i.e., I was not getting the growth column automatically.

co_code1 fyear1 sales1
1      1100   1990   1000
2      1100   1991   1100
3      1100   1992   1200
4      1100   1993   1300
5      1100   1995   1500
6      1100   1996   1600
7      1200   1991   1100
8      1200   1992   1200
9      1200   1993   1300
10     1200   1994   1400
11     1200   1995   1500
12     1200   1996   1600
13     1300   1990   1000
14     1300   1992   1200
15     1300   1993   1300
16     1300   1994   1400
17     1300   1995   1500
18     1300   1996   1600

2. When, just for the heck of it, the assign mark (<-) was changed to
'=' as done previously,

d<-ddply(df1,"co_code1",transform, growth = ifelse(diff(fyear1)==1,
(exp(diff(log(df1$sales1)))-1)*100, NA))

It was no longer evaluated-error was

"Error in data.frame(list(co_code1 = c(1100, 1100, 1100, 1100, 1100, 1100 :
  arguments imply differing number of rows: 6, 5"

3. The following gives the desired result

df1$growth<-c(NA, ifelse(diff(df1$fyear1)==1,
(exp(diff(log(df1$sales1)))-1)*100, NA))

But now I am no longer restricting each iteranation to
'co_code1'-hypothetically if one co_code1 is followed by another with
incremental 'fyear1' difference as 1, growth will be evaluated.

Is there a better and more elegant way of doing it?

Thanks and regards,

Brijesh
On Thu, Dec 15, 2016 at 5:02 PM, Rui Barradas <ruipbarradas at sapo.pt> wrote: