I am trying to calculate percentage change between elements in a vector of a dataframe:
Year
Year | Count |
---|---|
2010 | 55302 |
2011 | 58943 |
2012 | 59633 |
2013 | 50194 |
But I the new column I create is only giving NA values instead of the percentage change calculation I want between the count of each year:
Year | Count | Pct_change |
---|---|---|
2010 | 55302 | NA |
2011 | 58943 | NA |
2012 | 59633 | NA |
2013 | 50194 | NA |
My code is as follows:
df1 <- df %>%
group_by(Year) %>%
mutate(Pct_change = (Count - lead(Count) / lead(Count)*100)
I thought it may be the fact the Count column is integer values so I changed it to numeric, no success. Thanks in advance for any help