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))
CodePudding user response:
test$Apple_or_Banana = as.numeric(test$Apple | test$Banana)
Gives the result that you are after, I think.
CodePudding user response:
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