osteocyt
osteocyt

Reputation: 47

Using multiple ifelse

I have two vectors with equal length containing 0 and 1, but are not identical:

a <- c(1,1,0,1,1,1,0,1,1,0,0,1)
b <- c(1,1,0,0,1,0,0,0,1,1,1,1)

I want to create a new vector according to following if-rules, I tried

c <- ifelse((a==1 & b==1),1,0) | 
     ifelse((a==0 & b==0),0,1) | 
     ifelse((a==1 & b==0),0,1) | 
     ifelse((a==0 & b==1),0,1)

but was not working... Does anyone has an idea how to realize this?

Upvotes: 0

Views: 81

Answers (1)

Jason Clark
Jason Clark

Reputation: 78

  • Probably not best idea to mask the variable c
  • If I understand correctly, it looks like you want c to be equal to 1 if and only if a and b are both equal to 1. This could be solved with c <- a*b

Upvotes: 4

Related Questions