I'm trying to calculate the growth rate of the gdp per capita(pib_pc) for 32 states in a time horizon of 40 years but the time horizon changes. For example, in the first row I would like to get the growth rate of pib_pc between 1980 and 2017 for state i, in the second row the growth rate between 1981 and 2017 for state i and so on for each year and state.
Attached an image of how my panel looks like: The panel
So far I have been able to compute the growth rates of pib_pc for 5 and 10 year horizon using mutate and lead from the package dplyr. Here's the code:
data <- data %>%
group_by(estado) %>%
mutate(g_10 = (dplyr::lead(pib_pc,10)-pib_pc)/pib_pc) %>%
mutate(g_15 = (dplyr::lead(pib_pc,15)-pib_pc)/pib_pc) %>%
ungroup()