0

What I'd like to do is create a vector and filling it with values from other vectors. Namely: I have:

a <- c(NA, 1, NA, 2, NA)
b <- c(1, NA, 4, NA, 5)

I want to end with something like:

c <- c(1,1,4,2,5)

These values may change every time I run the code and the length of the two first vectors may change, but the third one is always the same.

user20650
  • 24,654
  • 5
  • 56
  • 91

1 Answers1

0

We can use coalesce

library(dplyr)
c1 <- coalesce(a, b)

Or in base R either pmin/pmax can be used as long as both corresponding elements are non-NA

pmin(a, b, na.rm = TRUE)
[1] 1 1 4 2 5
akrun
  • 874,273
  • 37
  • 540
  • 662