简体   繁体   English

根据两列改变百分位排名

[英]Mutate percentile rank based on two columns

I've previously asked the following question: Mutate new column over a large list of tibbles & the solutions giving were perfect.我之前曾问过以下问题: 在大量 tibbles 上更改新列,并且给出的解决方案非常完美。 I now have a follow-up question to this.我现在对此有一个后续问题。

I now have the following dataset:我现在有以下数据集:

df1: df1:

name姓名 group团体 competition竞赛 metric公制 value价值
A一个 A一个 comp A比较A distance距离 10569 10569
B A一个 comp B补偿 B distance距离 12939 12939
C C A一个 comp C比较 C distance距离 11532 11532
A一个 A一个 comp B补偿 B psv-99 psv-99 29.30 29.30
B A一个 comp A比较A psv-99 psv-99 30.89 30.89
C C A一个 comp C比较 C psv-99 psv-99 32.00 32.00

I now want to find out the percentile rank of all the values in df1, but only based on the group & one of the competitions - competition A.我现在想找出 df1 中所有值的百分位排名,但仅基于组和其中一项比赛 - 比赛 A。

We could slice the rows where the 'comp A' is found %in% competition , then do a grouping by 'group' column and create a new column percentile with percent_rank我们可以在%in% competition中找到 'comp A' 的行进行slice ,然后按 'group' 列进行分组,并使用 percent_rank 创建一个新的percentile percent_rank

library(dplyr)
df <- df %>%
   slice(which(competition %in% "comp A")) %>%
   group_by(group) %>%
   mutate(percentile = percent_rank(value))

Maybe just change metric to competition in the previous code?也许只是在以前的代码中将metric更改为competition It would give you the percentile rank for all competitions, including A.它将为您提供所有比赛的百分位排名,包括 A。

df1 %>% 
  group_nest(group, competition) %>% 
  mutate(percentile = map(data, ~percent_rank(.$value))) %>% 
  unnest(c(data, percentile))

You can filter the competition and group_by group .您可以filter competitiongroup_by group

library(dplyr)

df %>%
  filter(competition == "comp A") %>%
  group_by(group) %>%
  mutate(percentile = percent_rank(value))

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

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