根据 R 中的条件为每个组选择最大的 n 个值

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

我想选择每个中心每年最多举办 3 场活动,所以如果我有 1 年,则有 3 场,如果有 2 年,则有 6 场,依此类推。我已经计算了每个中心的年份并按最大值排序。我想要像 slice(1:aha) 这样的东西,但它似乎不起作用。有任何想法吗?提前致谢!

  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()