Home > Software engineering >  Sort out specific levels in one column according to the values in another column
Sort out specific levels in one column according to the values in another column

Time:07-02

I just came into R so I am stuck in a very basic question.

Right now I have a dataset listing soccer players and their goals, I have reordered the goals in descending order, now I need to find the top five players according to their goals.

the dataset looks like this:

Player Goals
A        10
A        10  
B        10
C        9
B        8
C        7
A        7
D        6
E        6
F        5

How can I sort out the top 5 players using the dplyr package?

CodePudding user response:

Try this one. If you could provide your data with dput() we could improve the code, Note: You have got ties in your dataset:

library(dplyr)

df %>% 
  distinct() %>% 
  top_n(5 )

OR

df %>% 
  distinct() %>% 
  slice_max(Goals, n=5)
  Player Goals
1      A    10
2      B    10
3      C     9
4      B     8
5      C     7
6      A     7

CodePudding user response:

Another dplyr option:

df <- read.table(text="Player Goals
A        10
A        10  
B        10
C        9
B        8
C        7
A        7
D        6
E        6
F        5", header = TRUE)
library(dplyr)
df %>% 
  arrange(desc(Goals)) %>%
  distinct() %>%
  filter(row_number() <= 5L)
#>   Player Goals
#> 1      A    10
#> 2      B    10
#> 3      C     9
#> 4      B     8
#> 5      C     7

Created on 2022-07-01 by the reprex package (v2.0.1)

  • Related