Ranking based on grouped data - R

Viewed 34

I would please like to get help with ranking data. So I have this dataframe (with many more columns and unique "raceId") with grouped data based on "raceId". What I am trying to do is giving a rank based on column "avg.position", for each unique raceId.

DF

What I have tried to do is using dplyr and the code down below:

rank.3 <- arrange(raceId, constructorId) %>% 
group_by(raceId) %>% 
mutate(rank = rank(avg.position, na.last = TRUE)) %>% 
ungroup()

The problem I am facing is that it returns a rank based on "avg.position" but NOT for each unique "raceId". So it does not give me a rank for each race, but instead based on the whole dataset.

Is there anyone who might have an solution for this? Thanks in advance!

0 Answers
Related