Reputation: 553
I have a dataset that looks like this:
> dput(test)
structure(list(Apple = c(0L, 1L, 1L, 0L, 1L), Banana = c(1L,
1L, 0L, 0L, 0L)), class = "data.frame", row.names = c(NA, -5L
))
I want to create a binary variable that signifies if either Apple and/or Banana is 1.
If both Apple and Banana are 0, then the value in the new variable should be 0.
If Apple is 1 but Banana is 0 (and vice versa), then the value in the new variable should be 1.
If Apple AND Banana is 1, then the value in the new variable should be 1.
This should be the output:
dput(test)
structure(list(Apple = c(0L, 1L, 1L, 0L, 1L), Banana = c(1L,
1L, 0L, 0L, 0L), Apple_or_Banana = c(1L, 1L, 1L, 0L, 1L)), class = "data.frame", row.names = c(NA,
-5L))
Upvotes: 0
Views: 213
Reputation: 39727
A variant general approaches for more than two columns and also other conditions than 1 will be.
test$Apple_or_Banana <- +(apply(test==1, 1, any))
#test
# Apple Banana Apple_or_Banana
#1 0 1 1
#2 1 1 1
#3 1 0 1
#4 0 0 0
#5 1 0 1
Upvotes: 0
Reputation: 529
test$Apple_or_Banana = as.numeric(test$Apple | test$Banana)
Gives the result that you are after, I think.
Upvotes: 4
Reputation: 33802
One way:
test$Apple_or_Banana <- ifelse(rowSums(test) > 0, 1, 0)
Result:
Apple Banana Apple_or_Banana
1 0 1 1
2 1 1 1
3 1 0 1
4 0 0 0
5 1 0 1
Upvotes: 4