Hi tmulflur,
A reprex could be as simple as just providing the example dataset and, optionally, the code with which you're trying to achieve your goal. Doing so, will make it easier for us to assist you quicker, instead of recreating the dataset manually.
Anyway, is the following approach, specifically the slice_head()
, providing you the results you're looking for?
library(tidyverse)
df <- tribble(
~home, ~score,
"UCLA", 4,
"UCLA", 7,
"UCLA", 9,
"UCLA", 10,
"FLORIDA", 3,
"FLORIDA", 5,
"FLORIDA", 6,
"FLORIDA", 8,
)
df %>% glimpse()
#> Rows: 8
#> Columns: 2
#> $ home <chr> "UCLA", "UCLA", "UCLA", "UCLA", "FLORIDA", "FLORIDA", "FLORID...
#> $ score <dbl> 4, 7, 9, 10, 3, 5, 6, 8
df %>%
group_by(home) %>%
arrange(desc(score)) %>%
slice_head(n = 2) %>%
ungroup()
#> # A tibble: 4 x 2
#> home score
#> <chr> <dbl>
#> 1 FLORIDA 8
#> 2 FLORIDA 6
#> 3 UCLA 10
#> 4 UCLA 9
Created on 2021-02-23 by the reprex package (v1.0.0)