Reputation: 25
I have 2 dummy variables and I would like to combine them in a way such that I have a new variable that puts a 1 if either Variable A or B had a 1.
e.g.
Variable A: 0 0 0 1 1
Variable B: 1 0 1 0 1
Variable C (ideally): 1 0 1 1 1
Any help is greatly appreciated! I know this is pretty simple.. getting back into R and feeling rusty!
Upvotes: 0
Views: 512
Reputation: 2071
We can do this with logical comparisons! The best one in this case is the OR operator, the pipe: |
set.seed(1234)
var_a <- round(runif(10))
var_b <- round(runif(10))
var_c <- var_a|var_b
rbind(var_a, var_b, var_c)
This works because R automagically coerces the numeric 0 and 1s into logical FALSE and TRUE, which we then compare using OR before automagically coercing back into numeric.
Welcome back to R!
Upvotes: 1