用平均数代替并列排名

Replacing tied rank by their average

我有以下数据框:

> 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

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"))

我想通过 tags 中每个字母的排名来替换列 freq.Freq。例如,a 是 1,d 是 4,i 是 9。无论如何,bcef, g 有相同的排名。对于这些情况,我想将 freq.Freq 替换为 "tied" 排名的平均值。这样,所需的输出是:

  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

我的尝试:

library(dplyr)
min_rank(x$freq.Freq)

给出了错误的结果:

9 7 7 6 3 3 3 2 1
x %>%
    mutate(rank = match(tags, letters)) %>%
    group_by(freq.Freq) %>%
    mutate(rank = mean(rank)) %>%
    ungroup()

建议的基本 R 版本
transform(x, new = ave(match(tags, letters), freq.Freq, FUN = mean))

我们可以直接使用 base R 中的 rankties.method 的默认方法是 "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