R - coalesce 2 vectors with dplyr::coalesce()

10 Feb 2021

Merge 2 vectors: if y null populates with z

require(dplyr)

y <- c(1, 2, NA, NA, 5)
z <- c(NA, NA, 3, 4, 5)
coalesce(y, z)
# [1] 1 2 3 4 5

y <- c(1, 2, 7, NA, 5)
z <- c(NA, NA, 3, 4, 5)
coalesce(y, z)
# [1] 1 2 7 4 5
[ R  function  dplyr  ]