msening
msening

Reputation: 53

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?

Upvotes: 2

Views: 292

Answers (2)

TarJae
TarJae

Reputation: 78917

Here is a solution with ifelse:

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

[1] 0 0 1 1 0 0 1 0 1 0

Upvotes: 2

akrun
akrun

Reputation: 886948

We can use pmax

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

or with |

+(vec1|vec2)

Upvotes: 2

Related Questions