# Compound Annual Growth Rate Function in R Statistics

I need to calculate the compound annual growth rate for two periods of time: 2014 & 2009. I'm currently working with Excel, I have two columns with data for each year (One column with only data of 2009 and the other only with data of 2014), where each row contains info for 2009 & 2014 for certain category.

I've been trying to calculate the CAGR between the data for each row, but I can't seem to find a function to do precisely that in "R".

I found a package with a growth.rate function for time series, but it doesn't take my data in the order I need (it uses bayes method for velocity of growth, which I don´t need).

Is there any package in "R" where I can get especially that rate? Any tips in how I can accommodate the data so that R reads row per row and calculates like that?

## 2 Answers

Instead of depending on a library, you can create your own vectorized function:

### Code:

library(tibble); library(dplyr)

example_data <- tribble(
~Values_2009, ~Values_2014,
10000, 19500,
10500, 15000,
25000, 35500
)

CAGR_formula <- function(FV, PV, yrs = 4) {
values <- ((FV/PV)^(1/yrs)-1)
return(values)
}

formula is (FV/PV)^1/n -1


### Results:

example_data %>%
mutate(CAGR = CAGR_formula(Values_2009, Values_2014, 4)*100)
#> # A tibble: 3 x 3
#>   Values_2009 Values_2014      CAGR
#>         <dbl>       <dbl>     <dbl>
#> 1       10000       19500 18.170386
#> 2       10500       15000  9.326511
#> 3       25000       35500  9.162151

# If Percentage is needed use the below mutate instead
#mutate(CAGR = CAGR_formula(Values_2009, Values_2014, 4)*100)


Vasim's code gives me negative results

  Values_2009 Values_2014   CAGR
<dbl>       <dbl>  <dbl>
1       10000       19500 -15.4
2       10500       15000  -8.53
3       25000       35500  -8.39


You need to swap the parameters in the function and also there are 5 years growth in the data. Full example:

example_data <- tribble(
~Values_2009, ~Values_2014, ~Years,
10000, 19500, 5,
10500, 15000, 5,
25000, 35500, 5
)

CAGR_formula <- function(PV, FV, yrs) {
values <- ((FV/PV)^(1/yrs)-1)
return(values)
}

example_data %>%
mutate(CAGR = CAGR_formula(Values_2009, Values_2014, Years),
check = Values_2009 * (1 + CAGR) ^ Years
)

Values_2009 Values_2014 Years   CAGR  check
<dbl>       <dbl> <dbl>  <dbl>  <dbl>
1       10000       19500     5 0.143  19500
2       10500       15000     5 0.0739 15000.
3       25000       35500     5 0.0726 35500.