简体   繁体   English

跨组应用排名

[英]Apply a rank across groups

I cant seem to get this working.我似乎无法让这个工作。 I'd like to rank a variable based on max value in a range of years.我想根据几年范围内的最大值对变量进行排名。 I can rank within a group ok, but I cant seem to be able to assign the ranking across groups.我可以在一个组内排名,但我似乎无法跨组分配排名。

It needs to be a dplyr mutate solution if possible as I am piping this into a plot.如果可能的话,它需要是一个 dplyr mutate 解决方案,因为我将它输送到一个情节中。

Data:数据:

data <- structure(list(
    YEAR = c(2020L, 2019L, 2020L, 2019L, 2020L, 2019L), 
    Grp = c("A", "A", "B", "B", "C", "C"), 
    Value = c(25L, 24L, 35L, 34L, 45L, 44L)), 
    class = "data.frame", row.names = c(NA, -6L))

Table looks like this:表如下所示:

YEAR Grp组别 value价值
2020 2020 A一个 25 25
2019 2019 A一个 24 24
2020 2020 B 35 35
2019 2019 B 34 34
2020 2020 C C 45 45
2019 2019 C C 44 44

I'd like to create the following output that ranks the Grp based on the value of the maximum year - in this case 2020.我想创建以下输出,根据最大年份的值对 Grp 进行排名 - 在本例中为 2020 年。

YEAR Grp组别 value价值 Rank
2020 2020 A一个 25 25 3 3
2019 2019 A一个 24 24 3 3
2020 2020 B 35 35 2 2
2019 2019 B 34 34 2 2
2020 2020 C C 45 45 1 1
2019 2019 C C 44 44 1 1

You could try你可以试试

library(dplyr)

data %>%
  group_by(Grp) %>%
  mutate(Rank = Value[which.max(YEAR)]) %>%
  ungroup() %>% 
  mutate(Rank = dense_rank(-Rank))

#   YEAR Grp Value Rank
# 1 2020   A    25    3
# 2 2019   A    24    3
# 3 2020   B    35    2
# 4 2019   B    34    2
# 5 2020   C    45    1
# 6 2019   C    44    1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM