Combine two vectors using non-NA values

Possible duplicate:
Combining two vectors by elements

I have two vectors

d = c(1, 2, NA, NA) c = c(NA, NA, 1, NA) 

How can I get a conclusion that would combine non NAs as follows?

 [1] 1 2 1 NA 

thanks

+7
source share
2 answers

What you are asking is a bit vague. For example, what happens if you are not a member of any NA?

In any case, here is one method that gives the desired result:

 ##Don't name things c - it confusing. d1 = c(1,2,NA,NA) d2 = c(NA,NA,1,NA) d1[is.na(d1)] = d2[is.na(d1)] 

What gives:

 R> d1 [1] 1 2 1 NA 
+6
source
 pmin(d, c, na.rm = TRUE) 

will do the trick.

 [1] 1 2 1 NA 
+9
source

All Articles