使用 dplyr 中的条件合并两列

Coalesce two columns with a condition in dplyr

我想合并两列以删除所有 NA 但同时两列都有值我想只保留最高值。

示例:

df <- data.frame(A = c(1,0,1,0,1,1,0,0,NA),
                 B = c(0,NA,1,1,NA,1,0,1,1))

   A  B
1  1  0
2  0 NA
3  1  1
4  0  1
5  1 NA
6  1  1
7  0  0
8  0  1
9 NA  1

想要的结果

   A  B C
1  1  0 1
2  0 NA 0
3  1  1 1
4  0  1 1
5  1 NA 1
6  1  1 1
7  0  0 0
8  0  1 1
9 NA  1 1

您可以计算行最大移除 NA 个值:

matrixStats::rowMaxs(as.matrix(df), na.rm = TRUE)
#[1] 1 0 1 1 1 1 0 1 1

dplyr :

library(dplyr)

df %>%
  rowwise() %>%
  mutate(C = max(c_across(), na.rm = TRUE))

#     A     B     C
#  <dbl> <dbl> <dbl>
#1     1     0     1
#2     0    NA     0
#3     1     1     1
#4     0     1     1
#5     1    NA     1
#6     1     1     1
#7     0     0     0
#8     0     1     1
#9    NA     1     1

我们可以使用 reducepmax

library(dplyr)
library(purrr)
df %>% 
    mutate(C = reduce(., pmax, na.rm = TRUE))

-输出

#   A  B C
#1  1  0 1
#2  0 NA 0
#3  1  1 1
#4  0  1 1
#5  1 NA 1
#6  1  1 1
#7  0  0 0
#8  0  1 1
#9 NA  1 1