[英]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![]() |
distance![]() |
10569 ![]() |
B![]() |
A![]() |
comp B![]() |
distance![]() |
12939 ![]() |
C ![]() |
A![]() |
comp C![]() |
distance![]() |
11532 ![]() |
A![]() |
A![]() |
comp B![]() |
psv-99 ![]() |
29.30 ![]() |
B![]() |
A![]() |
comp A![]() |
psv-99 ![]() |
30.89 ![]() |
C ![]() |
A![]() |
comp C![]() |
psv-99 ![]() |
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
competition
和group_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.