Reputation: 645
In my example below, I'm trying to do a mutate that does a rank within filtered criteria. My end goal is to filter, but keep the full data frame. I simply want my ranks to be done based on the filtered criteria. My df_goal
below gets me the correct ranks, but would require me to then use a left_join
to add them to the original df
which seems like a waste. Is there a solution within dplyr
that would allow me to rank the original df
with the filter
criteria? I was thinking that perhaps a pipe within a mutate
would work, but I had no success.
Example:
library(tidyverse)
df <- tibble::tribble(
~slugSeason, ~namePlayer, ~groupPosition, ~slugPosition, ~ptsPerGame, ~astPerGame,
"2019-20", "Aaron Gordon", "F", "PF", 11.3, 3,
"2019-20", "Aaron Holiday", "G", "PG", 3, 1.8,
"2019-20", "Abdel Nader", "F", "SF", 3, 0.3,
"2019-20", "Admiral Schofield", "F", "SF", 4.4, 0.2,
"2019-20", "Al-Farouq Aminu", "F", "PF", 4.2, 1,
"2019-20", "Al Horford", "F", "PF", 18.8, 4.2,
"2019-20", "Alec Burks", "G", "SG", 8.5, 1.8,
"2019-20", "Alex Caruso", "G", "SG", 3.2, 1.6,
"2019-20", "Alex Len", "C", "C", 4.2, 1.6,
"2019-20", "Alfonzo McKinnie", "F", "SF", 0, 0
)
df <- df %>%
group_by(groupPosition) %>%
mutate(ptsRankbyPosition = rank(desc(ptsPerGame))) %>%
ungroup()
df_goal <- df %>%
group_by(groupPosition) %>%
mutate(ptsRankbyPosition = rank(desc(ptsPerGame))) %>%
ungroup() %>%
filter(slugPosition %in% c("PG", "SG", "SF") & ptsRankbyPosition > 1 |
slugPosition == "C" & ptsRankbyPosition > 1) %>%
mutate(addlRank = rank(desc(ptsPerGame)))
Created on 2019-11-05 by the reprex package (v0.3.0)
Thanks in advance.
Upvotes: 3
Views: 955
Reputation: 887691
One option is using replace
after creating a logical column
library(dplyr)
df %>%
group_by(groupPosition) %>%
mutate(ptsRankbyPosition = rank(desc(ptsPerGame))) %>%
ungroup() %>%
mutate(index = slugPosition %in% c("PG", "SG", "SF") &
ptsRankbyPosition > 1 |
slugPosition == "C" & ptsRankbyPosition > 1,
addlRank = NA_real_,
addlRank = replace(addlRank, index, rank(desc(ptsPerGame[index])))) %>%
select(-index)
# A tibble: 10 x 8
# slugSeason namePlayer groupPosition slugPosition ptsPerGame astPerGame ptsRankbyPosition addlRank
# <chr> <chr> <chr> <chr> <dbl> <dbl> <dbl> <dbl>
# 1 2019-20 Aaron Gordon F PF 11.3 3 2 NA
# 2 2019-20 Aaron Holiday G PG 3 1.8 3 3.5
# 3 2019-20 Abdel Nader F SF 3 0.3 5 3.5
# 4 2019-20 Admiral Schofield F SF 4.4 0.2 3 1
# 5 2019-20 Al-Farouq Aminu F PF 4.2 1 4 NA
# 6 2019-20 Al Horford F PF 18.8 4.2 1 NA
# 7 2019-20 Alec Burks G SG 8.5 1.8 1 NA
# 8 2019-20 Alex Caruso G SG 3.2 1.6 2 2
# 9 2019-20 Alex Len C C 4.2 1.6 1 NA
#10 2019-20 Alfonzo McKinnie F SF 0 0 6 5
Upvotes: 2