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?
如何获得将非NA组合的输出如下?
[1] 1 2 1 NA
thanks
2 个解决方案
#1
6
What you are asking is a bit vague. For example, what happens if you neither element is a NA?
你问的是有点模糊。例如,如果您的元素都不是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
#2
10
pmin(d, c, na.rm = TRUE)
will do the trick.
会做的伎俩。
[1] 1 2 1 NA
#1
6
What you are asking is a bit vague. For example, what happens if you neither element is a NA?
你问的是有点模糊。例如,如果您的元素都不是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
#2
10
pmin(d, c, na.rm = TRUE)
will do the trick.
会做的伎俩。
[1] 1 2 1 NA