Home > OS >  Pick the maximum n values based on a condition in R for each group
Pick the maximum n values based on a condition in R for each group

Time:04-09

I would like to pick the 3 maximum events per year per center so 3 events if I have 1 year, 6 events if I have 2 years and so on. I have already computed the years for each center and sorted for the maximum. I would like something like slice(1:aha) but it does not seem to work. Any ideas? Thanks in advance! enter image description here

CodePudding user response:

  library(dplyr)
  library(purrr)
  df <- tibble::tibble(
    center = c(
      "5580", "5580", "5580", "5580", "5580", "5580",
      "5580", "5580", "5580", "5580",
      "5855", "5855", "5855", "5855"
    ),
    max = c(23, 41, 32, 58, 26, 76, 98, 98, 45, 8, 8, 12, 4, 6),
    years = c(6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 3, 3, 3, 3)
  )



# get group names  
df <-  df %>% group_by(years, center) 
df %>%
  group_split() %>%
  set_names(interaction(group_keys(df))) %>%
  purrr::map(~ .x %>%
               arrange(desc(max)) %>%
               slice(1:unique(years))) %>%
  bind_rows()
  • Related