'Average rate for 20 years, 10 years, and 5 years in R

I am trying to find the average rate of a certain virus between 2002-2021, 2002-2012, and 2002-2007 by another variable "jurisdiction". The code I have right now is:

avgrate20 <- ratesmerge %>%
  group_by(Jurisdiction) %>%
  summarize(
    Years = paste(range(2002:2021), collapse = "-"),
    across(starts_with("rate"), mean)
  )

When I change Years = paste(range(2002:2021), collapse = "-") to 2002-2012, it still takes the mean from 2002-2021.

Here is my output when doing head(df) enter image description here

Any help would be appreciated



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source