0

I am creating two employment growth graphs that span over a 60-year period.

The dataset shows the year and month, the metro area, and the size of the total labor force, and it is structured like this:

Year  Period  Area       No. of jobs 
2016  Jan     Metro1     10000
2016  Feb     Metro1     10200
...
2021  May     Metro2     14800
2021  Jun     Metro1     15000
2016  Jan     Metro2     30000
2016  Feb     Metro2     30200
...
2021  May     Metro2     48800
2021  Jun     Metro2     50000

I need to calculate the monthly change and the cumulative growth for each metro which I have already done using dplyr:

data_update <- data %>%
  group_by(Area) %>%
  mutate(Monthly_Change = (`No. of jobs` - lag(`No. of jobs`))) %>%
  replace_na(list(Monthly_Change = 0)) %>%
  mutate(Cummulative_Growth = cumsum(Monthly_Change))

However, my problem is that this calculates the cumulative growth for a period greater than 60 months because I can only download the data for every month of every year. In other words, the selection dialogue requires me to select the years in one box and then the months in a separate box so if I were to not include January 2016 because it exceeds the 60 month period then January 2017-2021 would not be included either.

Is there a way to group the data by area and then subset the last 60 rows of each group?

0 Answers0