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)
.