[英]How do I merge two vectors of same length into one vector that has the same length as well in R
I have two vectors like this:我有两个这样的向量:
vec1<-c(0, 0, 1, 1, 0, 0, 0, 0, 0, 0)
vec2<-c(0, 0, 0, 1, 0, 0, 1, 0, 1, 0)
I want to merge it somehow to turn it into this:我想以某种方式合并它以将其变成这样:
vec<-c(0, 0, 1, 1, 0, 0, 1, 0, 1, 0)
Is there any way to do this?有没有办法做到这一点?
We can use pmax
我们可以使用
pmax
pmax(vec1, vec2)
[1] 0 0 1 1 0 0 1 0 1 0
or with |
或与
|
+(vec1|vec2)
Here is a solution with ifelse
:这是
ifelse
的解决方案:
ifelse(vec1==0 & vec2==0, 0, 1)
[1] 0 0 1 1 0 0 1 0 1 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.