Home > other >  How do I merge two vectors of same length into one vector that has the same length as well in R
How do I merge two vectors of same length into one vector that has the same length as well in R

Time:06-17

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?

CodePudding user response:

We can use pmax

pmax(vec1, vec2)
[1] 0 0 1 1 0 0 1 0 1 0

or with |

 (vec1|vec2)

CodePudding user response:

Here is a solution with ifelse:

ifelse(vec1==0 & vec2==0, 0, 1)

[1] 0 0 1 1 0 0 1 0 1 0
  • Related