Ms.G
Ms.G

Reputation: 25

combine two vectors using cbind and either or argument

I have a simple question but don't know how to do it myself. I have two vectors where each contains '0' and '1' numeric values. My aim is to combine two vectors as one, and have something like this:

A <- c(1,1,0,0,0,1)

B <- c(0,1,1,0,0,1) 

after combining two:

C <- c(1,1,1,0,0,1) 

Basically, if either of them has 1, then it should be combined as 1, if both of them have 1, then it should be 1 too, if none of them have 1, then it should be 0.

Hope you can answer

Thank you much! -G

Upvotes: 2

Views: 80

Answers (1)

akrun
akrun

Reputation: 887511

We can use pmax to get the output

pmax(A, B)

Or with | to coerce the binary vectors to a logical vector and then change it to binary with + or (as.integer)

+(A|B)
#[1] 1 1 1 0 0 1

Upvotes: 3

Related Questions