r – How to find value with condition

We can try with dplyr. After grouping by ‘Name’, filter the rows where the row_number() is less than or equal to 3 and then get the max of ‘Score’

library(dplyr)
df1 %>%
    group_by(Name) %>% 
    filter(row_number() <=3) %>% 
    summarise(Score = max(Score))
# A tibble: 3 × 2
#   Name Score
#  <chr> <int>
#1     A     7
#2     B     4
#3     C     4

Leave a Comment