Compound annual growth rate

Hello- I'm hoping this is a relatively simple question that someone can help me with. I'm trying to calculate growth rate in a small dataset. This is the code I've been trying to work with based on a previous post in RStudio Community. Does anyone happen to know where I'm going wrong?

year <- as.Date(c('2007','2008','2009','2010','2011','2012','2013','2014','2015','2016','2017','2018'),'%Y')
year <- format(as.Date(year, format="%d/%m/%Y"),"%Y")
rate <- c('0','0.3','0.2727','0.325','0.3529412','0.472','0.41666','0.294','0.4285714','0.6285714','0.4','0.5714286')
growth_rate <- data.frame(year,rate)
growth_ = growth_rate %>%

first sort by year

arrange(year) %>%
mutate(Diff_year = year - lag(year), # Difference in time (just in case there are gaps)
Diff_growth = rate - lag(rate), # Difference in route between years
Rate_percent = (Diff_growth / Diff_year)/lag(rate) * 100) # growth rate in percent

This is the error I am getting: Error: Problem with mutate() input Diff_year.
x non-numeric argument to binary operator
i Input Diff_year is year - lag(year).

Hi,

lag(year[1]) returns NA.
Second, str(year) returns 'chr'. I guess these causes your problems?

JW

Hi. Thank you for your reply. I fixed it so year is now a date. But I am still getting an error:
growth_ = growth_rate %>%

> +   arrange(year1) %>%
> +   mutate(Diff_year = year1 - lag(year1),  # Difference in time (just in case there are gaps)
> +          Diff_growth = rate - lag(rate), # Difference in route between years
> +          Rate_percent = (Diff_growth / Diff_year)/lag(rate) * 100) # growth rate in percent
> Error: Problem with `mutate()` input `Rate_percent`.
> x second argument of / cannot be a "difftime" object
> i Input `Rate_percent` is `(Diff_growth/Diff_year)/lag(rate) * 100`.
> Run `rlang::last_error()` to see where the error occurred.
> > Average_growth = mean(growth_$Rate_percent, na.rm = TRUE)
> Error in mean(growth_$Rate_percent, na.rm = TRUE) : 
>   object 'growth_' not found

I think rate is a character variable rather than numeric. What happens if you substitute for rate as.numeric(rate)?

I wonder if the lag() function is causing you issues as well. Would something simpler like:

tn <- rate[2:length(rate)]
tl <- rate[1:length(rate)-1]
(tn-tl)/tl

work? Also, I don't know your application, but are you sure you want arithmetic mean instead of geometric mean? And, to be clear, you're trying to calculate the change in the rate, not the rate itself?

Use numeric values for everything instead of dates

library(tidyverse)

growth_rate <- data.frame(
    year = as.Date(c('2007','2008','2009','2010','2011','2012',
                     '2013','2014','2015','2016','2017','2018'),'%Y'),
    rate = c('0','0.3','0.2727','0.325','0.3529412','0.472','0.41666',
             '0.294','0.4285714','0.6285714','0.4','0.5714286'))

growth_rate %>%
    mutate_all(as.numeric) %>% 
    arrange(year) %>%
    mutate(Diff_year = year - lag(year),
           Diff_growth = rate - lag(rate),
           Rate_percent = (Diff_growth / Diff_year) / lag(rate) * 100)
#>     year      rate Diff_year Diff_growth Rate_percent
#> 1  13714 0.0000000        NA          NA           NA
#> 2  14080 0.3000000       366   0.3000000          Inf
#> 3  14445 0.2727000       365  -0.0273000  -0.02493151
#> 4  14810 0.3250000       365   0.0523000   0.05254407
#> 5  15175 0.3529412       365   0.0279412   0.02355423
#> 6  15541 0.4720000       366   0.1190588   0.09216755
#> 7  15906 0.4166600       365  -0.0553400  -0.03212213
#> 8  16271 0.2940000       365  -0.1226600  -0.08065444
#> 9  16636 0.4285714       365   0.1345714   0.12540434
#> 10 17002 0.6285714       366   0.2000000   0.12750456
#> 11 17367 0.4000000       365  -0.2285714  -0.09962639
#> 12 17732 0.5714286       365   0.1714286   0.11741685

Created on 2021-07-20 by the reprex package (v2.0.0)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.