Search code examples
raverage

Creating new dataset with averages in R


I currently have a dataset that looks like this:

enter image description here

I would like to create a new dataset of the average rate for each Jurisdiction. It would look something like this:

enter image description here

Do I need to use a subset option or do I need convert this table from long to wide? I'm a bit lost on the code I need to write to make this work.

Any help would be appreciated!

Thanks!


Solution

  • Likely something like this:

    dplyr

    library(dplyr)
    dat %>%
      group_by(Jurisdiction) %>%
      summarize(
        Years = paste(range(2021:2025), collapse = "-"),
        across(starts_with("rate"), mean)
      )