# A tibble: 11 x 3
# Groups: year [11]
year var pct_change
<chr> <dbl> <dbl>
1 2010 111. NA
2 2011 99.6 NA
3 2012 102. NA
4 2013 87.2 NA
5 2014 99.3 NA
6 2015 103. NA
7 2016 110. NA
8 2017 109. NA
9 2018 79.8 NA
10 2019 113. NA
11 2020 123. NA
Could someone please show me how to resolve this problem?
E.g. (99.6 - 11/ 111)*100 , (102 - 99.6/99.6)*100, etc.
you can either use data.table::shift() or, easier, just collapse::fgrowth() (and I think there is a tidyverse equivalent as well, but those two just came two my mind).
Looking at your code, if you only have one entry per year, your main problem is the grouping (since if you group the df, dplyr will try to look inbetween years to compute the growth rate, e.g. the percentage change between val1 and val2 in one year. Added the dplyr solution above.
If you group by year but there is only one value for each year then there are no lagged values in the groups. Just drop the group_by(). The first value is NA because there is no lagged value for the first year. Given that the years are already in chronological order, there is also no need to arrange them.
I also changed the percentage calculation to (new - old/old ) from (old - new/new).