简体   繁体   English

在 R 中组合两个二进制变量?

[英]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.我想创建一个二进制变量来表示 Apple 和/或 Banana 是否为 1。

  • If both Apple and Banana are 0, then the value in the new variable should be 0.如果 Apple 和 Banana 均为 0,则新变量中的值应为 0。

  • If Apple is 1 but Banana is 0 (and vice versa), then the value in the new variable should be 1.如果 Apple 为 1 但 Banana 为 0(反之亦然),则新变量中的值应为 1。

  • If Apple AND Banana is 1, then the value in the new variable should be 1.如果 Apple AND Banana 为 1,则新变量中的值应为 1。

This should be the output:这应该是 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))

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
test$Apple_or_Banana = as.numeric(test$Apple | test$Banana)

Gives the result that you are after, I think.给出你想要的结果,我想。

A variant general approaches for more than two columns and also other conditions than 1 will be.多于两列的变体通用方法以及除 1 之外的其他条件。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM