使用 dplyr 返回组最大值和 NA

Returning group maximum and NA using dplyr

我需要一个可以使用 returns 组最大值和任何 NA 值的函数。这是玩具数据:

df <- data.frame(id = rep(1:5, 
                          each = 3),
                 score = rnorm(15))

df$score[c(3,7,10,14)] <- NA

#    id      score
# 1   1 -1.4666164
# 2   1  0.4392647
# 3   1         NA
# 4   2 -0.6010311
# 5   2  1.9845774
# 6   2  0.1749082
# 7   3         NA
# 8   3 -0.3089731
# 9   3  0.4427471
# 10  4         NA
# 11  4  1.7156319
# 12  4 -0.2354253
# 13  5  1.1781350
# 14  5         NA
# 15  5  0.0642082

我可以用slice_max得到每组中的最大值:

df %>%
  group_by(id) %>%
    slice_max(score)

#      id score
#   <int> <dbl>
# 1     1 0.439
# 2     2 1.98 
# 3     3 0.443
# 4     4 1.72 
# 5     5 1.18 

但是我如何获得最大 加上 返回的任何 NA?

使用自定义函数你可以做到:

library(dplyr)

set.seed(123)

slice_max_na <- function(.data, order_by, ..., n, prop, with_ties = TRUE) {
  bind_rows(
    slice_max(.data, order_by = {{order_by}}, ..., n = n, prop = prop, with_ties = with_ties),
    filter(.data, is.na({{order_by}})),
  )
}

df %>%
  group_by(id) %>%
  slice_max_na(score)
#> # A tibble: 9 × 2
#> # Groups:   id [5]
#>      id  score
#>   <int>  <dbl>
#> 1     1 -0.230
#> 2     2  1.72 
#> 3     3 -0.687
#> 4     4  1.22 
#> 5     5  0.401
#> 6     1 NA    
#> 7     3 NA    
#> 8     4 NA    
#> 9     5 NA

我们可以group_by id 列,然后使用summarize 输出带有max 的摘要。这里使用了两个max,其中一个有na.rm = T,一个没有。 union() 用于组合 max.

中存在的输出
library(dplyr)

df %>% 
  group_by(id) %>% 
  summarize(score = union(max(score, na.rm = T), max(score)))  

更新:以上代码只有在每个 ID 最多有一个 NA 时才有效。感谢@KU99 提醒

如果每个ID有多个NA,则需要将max的结果与is.na()找到的NA的记录合并。

df %>% 
  group_by(id) %>% 
  summarize(score = c(max(score, na.rm = T), score[is.na(score)]))

结果

# A tibble: 9 × 2
# Groups:   id [5]
     id  score
  <int>  <dbl>
1     1  0.735
2     1 NA    
3     2  0.314
4     3  0.994
5     3 NA    
6     4  0.847
7     4 NA    
8     5  1.95 
9     5 NA  

数据

df <- structure(list(id = c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 
4L, 4L, 5L, 5L, 5L), score = c(-1.05089006245306, 0.734652105895187, 
NA, -1.31427279695036, -0.250038722057874, 0.314204596436828, 
NA, 0.994420599790523, 0.855768431757766, NA, 0.834325037545013, 
0.846790152407738, 1.95410525460771, NA, 0.971120269710021)), row.names = c(NA, 
-15L), class = "data.frame")

一个选项是使用 slice| 创建一个逻辑条件 is.na 到 return NA 行和 max 行。

library(dplyr)

df %>%
  group_by(id) %>% 
  slice(which(score == max(score, na.rm = T)|is.na(score)))

另一种选择是像您一样使用 slice.max,然后使用 bind_rowsNA 值添加回数据框。

library(dplyr)

df %>% 
  group_by(id) %>%
  slice_max(score) %>% 
  bind_rows(df %>% filter(is.na(score))) %>% 
  arrange(id)

输出

     id   score
  <int>   <dbl>
1     1 -0.161 
2     1 NA     
3     2  1.49  
4     3 -0.451 
5     3 NA     
6     4  0.878 
7     4 NA     
8     5 -0.0652
9     5 NA     

数据

df <- structure(list(id = c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 
4L, 4L, 5L, 5L, 5L), score = c(-0.161217942983375, -0.456571996252207, 
NA, 0.540071362460494, 1.49325799630099, -0.17985218510166, NA, 
-0.451301758592, -0.839100876399644, NA, -0.0432130218441599, 
0.87779273806634, -0.339260854059069, NA, -0.065177224102029)), row.names = c(NA, 
-15L), class = "data.frame")

这里是 dplyr 版本更多使用 rank:

library(dplyr)

df %>%
  group_by(id) %>%
  mutate(rank = rank(-score, ties.method = "random")) %>% 
  filter(rank == 1 | is.na(score)) %>% 
  select(-rank)
     id  score
  <int>  <dbl>
1     1  0.505
2     1 NA    
3     2 -0.109
4     3 NA    
5     3  1.45 
6     4 NA    
7     4  0.355
8     5 NA    
9     5 -0.298