Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
可能重复: 逐个元素组合两个向量
我有两个向量
d = c(1, 2, NA, NA) c = c(NA, NA, 1, NA)
如何获得将非 NA 组合如下的输出?
[1] 1 2 1 NA
谢谢
pmin(d, c, na.rm = TRUE)
会成功的。
你问的有点含糊。例如,如果两个元素都不是 NA,会发生什么?
无论如何,这是一种可以提供所需结果的方法:
##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)]
这使:
R> d1 [1] 1 2 1 NA