Possible Duplicate:
Combining two vectors element-by-element
I have two vectors
d = c(1, 2, NA, NA)
c = c(NA, NA, 1, NA)
How can I get an output that would combine the non NAs as follows?
[1] 1 2 1 NA
thanks
pmin(d, c, na.rm = TRUE)
will do the trick.
[1] 1 2 1 NA
What you are asking is a bit vague. For example, what happens if you neither element is a NA?
Anyway, here's one method that gives the desired result:
##Don't name things c - it's confusing.
d1 = c(1,2,NA,NA)
d2 = c(NA,NA,1,NA)
d1[is.na(d1)] = d2[is.na(d1)]
Which gives:
R> d1
[1] 1 2 1 NA
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With