I have the following dataframe:
> x
tags freq.Freq
1 a 740
2 b 722
3 c 722
4 d 541
5 e 525
6 f 525
7 g 525
8 h 326
9 i 296
i.e.
x<- structure(list(tags = c("a", "b", "c", "d", "e", "f", "g", "h", "i"),
freq.Freq = c(740L, 722L, 722L, 541L, 525L, 525L, 525L, 326L, 296L)),
class = "data.frame", row.names = c("1", "2", "3", "4", "5", "6", "7", "8", "9"))
I would like to replace the column freq.Freq
by ranking of each letter in tags
. E.g., a
is 1, d
is 4, i
is 9. Anyway, b
, c
, and e
, f
, g
have the same ranks. For these cases I would like to replace freq.Freq
by the average of "tied" ranks. In this way the desired output is:
tags freq.Freq
1 a 1
2 b 2.5
3 c 2.5
4 d 4
5 e 6
6 f 6
7 g 6
8 h 8
9 i 9
My attempt:
library(dplyr)
min_rank(x$freq.Freq)
gives a wrong result:
9 7 7 6 3 3 3 2 1
We can just use rank
from base R
. The default method for ties.method
is "average"
x$freq.Freq <- rank(-x$freq.Freq)
x$freq.Freq
#[1] 1.0 2.5 2.5 4.0 6.0 6.0 6.0 8.0 9.0