我正在对大型data.frame
进行数据分析。有没有一种方法可以根据过滤后的标准保留完整的data.frame
来进行排名?该代码的工作原理与使用dplyr
filter
结果相似,但是在data.frame
mutate
的内容
library(tidyverse)
# Data
df <- read.table(sep="\t", text="
namePlayer groupPosition minutesTotals fgmTotals fgaTotals
fg3mTotals fg3aTotals fg2mTotals fg2aTotals ftmTotals ftaTotals
orbTotals drbTotals trbTotals astTotals stlTotals blkTotals
tovTotals pfTotals ptsTotals
Anthony Davis C 1267 353 698 34 105 319 593 236 294 114 340 454 151 58 90 71 86 976
Bradley Beal G 1392 336 714 89 262 247 452 137 174 36 148 184 180 47 32 98 115 898
Damian Lillard G 1347 335 741 117 297 218 444 229 256 30 151 181 219 35 19 104 69 1016
Giannis Antetokounmpo F 1146 335 572 12 79 323 493 212 305 83 346 429 207 45 54 145 116 894
James Harden G 1261 331 752 162 416 169 336 318 374 26 175 201 291 70 19 189 116 1142
Joel Embiid C 1255 330 680 42 142 288 538 295 368 89 413 502 129 21 70 128 125 997
Kemba Walker G 1276 324 734 122 335 202 399 171 208 16 142 158 223 48 18 89 61 941
Kevin Durant F 1399 383 760 70 192 313 568 274 300 19 274 293 237 32 39 133 74 1110
LeBron James F 1178 340 656 68 191 272 465 180 264 32 251 283 243 44 24 116 54 928
Paul George F 1271 332 734 119 315 213 419 179 215 56 235 291 146 82 22 99 106 962", header=TRUE, stringsAsFactors=FALSE)
df_calc <- df %>%
# Overall Rank
mutate(o_rank = rank(desc(ptsTotals))) %>%
# Rank by Position
group_by(groupPosition) %>%
mutate(position_rank = rank(desc(ptsTotals))) %>%
ungroup() %>%
# Conditional Rank
mutate(custom_rank = ifelse(groupPosition %in% c("G", "F") & position_rank > 3 |
groupPosition =="C" & position_rank > 3, rank(desc(ptsTotals)), NA ))
df_calc_correct <- df %>%
# Overall Rank
mutate(o_rank = rank(desc(ptsTotals))) %>%
# Rank by Position
group_by(groupPosition) %>%
mutate(position_rank = rank(desc(ptsTotals))) %>%
ungroup() %>%
# Conditional Rank
filter(groupPosition %in% c("G", "F") & position_rank > 3 |
groupPosition =="C" & position_rank > 3) %>%
mutate(custom_rank = rank(desc(ptsTotals)))
df_calc
#> # A tibble: 10 x 23
#> namePlayer groupPosition minutesTotals fgmTotals fgaTotals fg3mTotals
#> <chr> <chr> <int> <int> <int> <int>
#> 1 " ~ C 1267 353 698 34
#> 2 " ~ G 1392 336 714 89
#> 3 " ~ G 1347 335 741 117
#> 4 " ~ F 1146 335 572 12
#> 5 " ~ G 1261 331 752 162
#> 6 " ~ C 1255 330 680 42
#> 7 " ~ G 1276 324 734 122
#> 8 " ~ F 1399 383 760 70
#> 9 " ~ F 1178 340 656 68
#> 10 " ~ F 1271 332 734 119
#> # ... with 17 more variables: fg3aTotals <int>, fg2mTotals <int>,
#> # fg2aTotals <int>, ftmTotals <int>, ftaTotals <int>, orbTotals <int>,
#> # drbTotals <int>, trbTotals <int>, astTotals <int>, stlTotals <int>,
#> # blkTotals <int>, tovTotals <int>, pfTotals <int>, ptsTotals <int>,
#> # o_rank <dbl>, position_rank <dbl>, custom_rank <dbl>
df_calc_correct
#> # A tibble: 2 x 23
#> namePlayer groupPosition minutesTotals fgmTotals fgaTotals fg3mTotals
#> <chr> <chr> <int> <int> <int> <int>
#> 1 " ~ G 1392 336 714 89
#> 2 " ~ F 1146 335 572 12
#> # ... with 17 more variables: fg3aTotals <int>, fg2mTotals <int>,
#> # fg2aTotals <int>, ftmTotals <int>, ftaTotals <int>, orbTotals <int>,
#> # drbTotals <int>, trbTotals <int>, astTotals <int>, stlTotals <int>,
#> # blkTotals <int>, tovTotals <int>, pfTotals <int>, ptsTotals <int>,
#> # o_rank <dbl>, position_rank <dbl>, custom_rank <dbl>
由reprex软件包(v0.2.1)于2019-01-04创建
答案 0 :(得分:1)
我无法正确读取您的df
,但是这种常规方法应该可以
set.seed(1)
df <- data.frame(a = 1:10, b = sample(1:10))
df %>%
mutate(custom_rank = {
filt <- a %in% 3:5
replace(rep(NA, n()), which(filt), rank(desc(b[filt])))})
# a b custom_rank
# 1 1 3 NA
# 2 2 4 NA
# 3 3 5 2
# 4 4 7 1
# 5 5 2 3
# 6 6 8 NA
# 7 7 9 NA
# 8 8 6 NA
# 9 9 10 NA
# 10 10 1 NA
答案 1 :(得分:1)
无法加载表格,但是如果为正ifelse条件过滤ptsTotals向量,它应该可以工作:
df %>%
# Overall Rank
mutate(o_rank = rank(desc(ptsTotals))) %>%
# Rank by Position
group_by(groupPosition) %>%
mutate(position_rank = rank(desc(ptsTotals))) %>%
ungroup() %>%
# Conditional Rank
mutate(custom_rank = ifelse(groupPosition %in% c("G", "F") & position_rank > 3 |
groupPosition =="C" & position_rank > 3,
rank(desc(ptsTotals[groupPosition %in% c("G", "F") & position_rank > 3 |
groupPosition =="C" & position_rank > 3])), NA ))