Hi all, I need to create a new variable that ranks another variable, by value. Essentially, we had participants in a recent study rate how hard they found 6 different things to be, and we now want to have a rank-order of these things for each person. I've been messing with this code forever though and can't get it right.
So far I've been using this method:
dat=tibble::tribble(~name, ~score,
"bob", 0,
"bob", 5,
"bob", 50,
"bob", 50,
"bob", 50,
"bob", NA)
dat=dat %>% mutate(rank=rank(score,
ties.method = "max", na.last = FALSE))
# Flip the ranks around so they are highest to lowest
dat$rank=car::recode(dat$rank,"1 = 6 ; 2 = 5 ; 3 = 4 ; 4 = 3 ; 5 = 2 ; 6 = 1")
dat
The problem with this is that it assigns ranks like you see in sporting events; that is, if three people tied for second place, the rankings look like 1,2,2,2,5. This is not what I want...I need it to be 1,2,2,2,3. The current way I'm getting the ranks makes it look like there are several things missing in the data that aren't ranked when in reality there is only one NA.