0

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))
1

3 Answers 3

4
test$Apple_or_Banana = as.numeric(test$Apple | test$Banana)

Gives the result that you are after, I think.

Sign up to request clarification or add additional context in comments.

Comments

4

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

1 Comment

A similar variant might be test$Apple_or_Banana <- +(rowSums(test) > 0)
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.