Jamie
Jamie

Reputation: 553

Combining two binary variables in R?

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.

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

Answers (3)

GKi
GKi

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

user1505631
user1505631

Reputation: 529

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

Gives the result that you are after, I think.

Upvotes: 4

neilfws
neilfws

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

Related Questions