I just want to generate a variable that captures the relative decline of a variable (v2x_polyarchy) from year to year.This is what my data frame 'episodes_demreg' basically looks like:
country_name year demreg_id v2x_polyarchy
<chr> <dbl> <dbl> <dbl>
Suriname 1974 1 0.536
Suriname 1975 1 0.375
Suriname 1979 2 0.713
Suriname 1980 2 0.226
Suriname 1990 3 0.743
Suriname 1991 3 0.455
Based on the suggestion on very similar thread (Percent change difference for time series data) I tried it like this:
episodes_demreg <- episodes_demreg %>%
group_by(demreg_id) %>%
mutate (decline_year_relative = ((v2x_polyarchy - lag(v2x_polyarchy)) / lag(v2x_polyarchy)) * 100)
However, this does not work, as it results in the value '0' for each observation. Going back and forth revealed that obviously 'lag' does not work and indeed it subtracts the observation of the same row and NOT the row before. Any ideas why this happens?
You are using lag
from the stats
package, while you want to use lag
from the dplyr
package.
You can either specify the package when calling lag
group_by(demreg_id) %>%
mutate (decline_year_relative = ((v2x_polyarchy - dplyr::lag(v2x_polyarchy)) / dplyr::lag(v2x_polyarchy)) * 100)
or set dplyr
as the default package to use lag
lag <- dplyr::lag