Hi,
What do you think of this solution:
library(dplyr)
library(purrr)
myData = data.frame(
Company=c(1,1,1,1,2,2,2,3,3),
Year=c(1,2,3,4,1,2,3,1,2),
Yearly_Growth=c(0.1,0.2,0.3,0.1,0.1,0.05,0.1,0.1,0.1)
)
CGR = function(prev, new){
(prev+1)*(new+1) - 1
}
myData = myData %>% arrange(Company, Year) %>% group_by(Company) %>%
mutate(cGR = accumulate(Yearly_Growth, CGR))
myData
# A tibble: 9 x 4
# Groups: Company [3]
Company Year Yearly_Growth cGR
<dbl> <dbl> <dbl> <dbl>
1 1 1 0.1 0.1
2 1 2 0.2 0.32
3 1 3 0.3 0.716
4 1 4 0.1 0.888
5 2 1 0.1 0.1
6 2 2 0.05 0.155
7 2 3 0.1 0.271
8 3 1 0.1 0.1
9 3 2 0.1 0.21
It took me a while to find the function I wanted, but the accumulate function from the purrr package seemed to do the trick. I wrote a special cumulative function CGR that will be called for every year per company and take the previous years into account when calculating the growth rate.
Of note, it is important that the data is sorted properly (hence using arrange) because the accumulation of data needs to be in order of year.
Hope this helps,
PJ