寻找在存在 NA 时使用 ifelse 的 R 函数

looking for R function that utilizes ifelse when there are NA

我有这个数据,我需要如图所示的输出。

    a<-c(0,0,0,1,1,1,0,1)
    b<-c(1,0,0,0,1,0,NA,NA)
    data<-data.frame(a,b)
    
    data$c<-c(1,0,0,1,1,1,0,1)

也许是这样:

apply(data, 1, max, na.rm=T)
1 0 0 1 1 1 0 1

这些中的任何一个都适用于即时情况

pmax(a, b, na.rm = T)
[1] 1 0 0 1 1 1 0 1

data$c <- +(as.logical(rowSums(data, na.rm = T)))

 data$c
[1] 1 0 0 1 1 1 0 1

dplyr::if_else 有最后一个参数提供附加值,如果提供的条件导致 NA