是否可以在mutate中使用过滤器?

时间:2019-11-05 18:04:06

标签: r dplyr

在下面的示例中,我试图做一个变异,该变异在过滤条件内进行排名。我的最终目标是过滤,但保留完整的数据帧。我只是希望根据过滤的标准完成排名。我下面的df_goal为我提供了正确的排名,但要求我然后使用left_join将其添加到原始df中,这似乎很浪费。 dplyr中是否存在一种解决方案,可以让我根据df条件对原始filter进行排名?我当时以为mutate中的管道可能会起作用,但我没有成功。

示例:

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)

先谢谢了。

1 个答案:

答案 0 :(得分:2)

一种选择是在创建逻辑列后使用replace

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