添加向量时将NA视为零

Zwe*_*ler 0 r

添加两个向量很容易:

> c(1:5) + c(6:10)
[1]  7  9 11 13 15
Run Code Online (Sandbox Code Playgroud)

但是由于在NA上加上任何数字都会得到NA,因此会发生以下情况:

> c(1,NA,3:5)+c(6:10)
[1]  7 NA 11 13 15
Run Code Online (Sandbox Code Playgroud)

如何在可能存在一些NA的地方添加两个向量,将它们视为零?我需要得到以下结果:

> c(1,NA,3:5)+c(6:10)
[1]  7 7 11 13 15
Run Code Online (Sandbox Code Playgroud)

关于如何使用{base}原始向量而不将NA更改为零的任何想法?

Ale*_*lex 7

您也可以使用colSumsrowSums,例如:

rowSums(cbind(x, y), na.rm = T)
# [1]  7  7 11 13 15

colSums(rbind(x, y), na.rm = T)
# [1]  7  7 11 13 15
Run Code Online (Sandbox Code Playgroud)

基准;出乎意料colSums的最快速度:

microbenchmark::microbenchmark(fn_replace(x, y),
                               fn_rowSums(x, y),
                               fn_colSums(x, y),
                               fn_coalesce(x, y))

# Unit: milliseconds
# expr      min        lq     mean   median       uq      max neval
# fn_replace(x, y) 121.4322 130.99067 174.1531 162.2454 183.1781 385.7348   100
# fn_rowSums(x, y) 143.0654 146.20815 172.5396 149.3953 179.0337 370.1625   100
# fn_colSums(x, y)  96.8848  99.46521 121.5916 106.8800 140.9279 298.1607   100
# fn_coalesce(x, y) 259.2923 310.16915 357.0241 326.1245 360.9110 595.9711   100


## Code to generate x, y and functions for benchmark:
fn_replace <- function(x, y) {
    replace(x, is.na(x), 0) + replace(y, is.na(y), 0)
}

fn_rowSums <- function(x, y) {

    rowSums(cbind(x, y), na.rm = T)

}

fn_colSums <- function(x, y) {

    colSums(rbind(x, y), na.rm = T)

}

fn_coalesce <- function(x, y) {
    dplyr::coalesce(x, rep(0, length(x))) + 
    dplyr::coalesce(y, rep(0, length(y)))
}


n_rep <- 1e6
x <- as.numeric(rep(c(1, NA, 3:5, NA, NA, 5), n_rep))
y <- as.numeric(rep(c(NA, 6:9, NA, 3, 4), n_rep))
Run Code Online (Sandbox Code Playgroud)