I have the below df
df <-dput(l)
structure(list(l = c(7.4, 7, 6.7, 8.8, 7.3, 9.1, 7.7, 9.5, 7.5,
7.4, 6.7, 7.3, 7.5)), row.names = c("rankings_message_1", "rankings_message_2",
"rankings_message_3", "rankings_message_4", "rankings_message_5",
"rankings_message_6", "rankings_message_7", "rankings_message_8",
"rankings_message_9", "rankings_message_10", "rankings_message_11",
"rankings_message_12", "rankings_message_13"), class = "data.frame")
but I would like to add a column to rank the numbers (they are %) where the highest # would be labeled as rank 1 and so on.
I tried this but I get an error:
df$Rank <- rank(desc(df))
CodePudding user response:
You have probably loaded tidyverse
without including that in your code. Another way to get there would be
df$Rank <- (nrow(df) 1) - rank(df$l)
df[order(Rank), ]
# l Rank
# rankings_message_8 9.5 1.0
# rankings_message_6 9.1 2.0
# rankings_message_4 8.8 3.0
# rankings_message_7 7.7 4.0
# rankings_message_9 7.5 5.5
# rankings_message_13 7.5 5.5
# rankings_message_1 7.4 7.5
# rankings_message_10 7.4 7.5
# rankings_message_5 7.3 9.5
# rankings_message_12 7.3 9.5
# rankings_message_2 7.0 11.0
# rankings_message_3 6.7 12.5
# rankings_message_11 6.7 12.5
CodePudding user response:
I think this would just be
rev(rank(df$l)) # which is wrong, should have been `rank(-df$l)`
The handling of ties is "average" by default but if you wnat ties to have at least one 1
then you have choices. See ?rank
rev(rank(df$l))
#[1] 8.5 4.5 1.5 6.5 8.5 13.0 10.0 12.0 4.5 11.0 1.5 3.0 6.5
?desc # you appear to be using a package that is not base R.
#No documentation for ‘desc’ in specified packages and libraries:
#you could try ‘??desc’
?rank
rev(rank(df$l, ties="first"))
#[1] 9 5 2 7 8 13 10 12 4 11 1 3 6
I couldn't figure out why the 1 came after the 2 and then I started wondering about the correctness of this code. I think it should instead be:
(df$r1 <- abs( rank(df$l, ties="first") - (length(df$l) 1)) )
# [1] 8 11 13 3 10 2 4 1 6 7 12 9 5
Could also rank the negative of the vector:
df$r2 <- rank(-df$l, ties="last")