dplyr::group_by() - Accessing values that lie outside the scope of the group currently operated on?


I have a data.frame with multiple monthly time series data. I would like to dplyr::group_by() "year" and for all different time series simultaneously divide each observation within the group by the December value of the previous group (e.g.: I'd like to divide all observations within the year 2020 (across all different time series) by the December value of the year 2019 of each respective time series).

My question:
When grouping by a specific grouping variable, is it possible to access values that lie outside the specific group the function presently operates on (e.g.: the values of the group before my presently operated on group)?

What would be your preferred / best practice to do so?

I'd appreciate your help and guidance. Thank you!

I would do this by joining the December values to all existing values. I would:

  1. make a new data frame by filtering the original for just December values
  2. increment the year by 1
  3. inner_join to the original data, matching the year in the original data to the incremented year in the December data.
  4. divide the original values by the previous-year December values that are now in the same row.

Thank you, FJCC! That's exactly the way I did it. I just thought there might be a more direct way. :slight_smile:

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.