简体   繁体   中英

Logical Operators not subsetting as expected

I am trying to create a subset of the rows that have a value of 1 for variable A, and a value of 1 for at least one of the following variables: B, C, or D.

Subset1 <-  subset(Data,
              Data$A==1 &
                Data$B ==1  ||
                Data$C ==1 |
                Data$D == 1,
              select= A)
Subset1

The problem is that the code above returns some rows that have A=0 and I am not sure why.

To troublehsoot:

  1. I know that && and || are the long forms or and and or which vectorizes it. I have run this code several times using && , || , & and | in different places. Nothing returns what I am looking for exactly.

  2. When I shorten the code, it works fine and I subset only the rows that I would expect:

Subset1 <- subset(Data, Data$A==1 & Data$B==0, select= A)

Subset1

Unfortunately, this doesn't suffice since I also need to capture rows whose C or D value = 1. Can anyone explain why my first code block is not subsetting what I am expecting it to?

You can use parens to be more specific about what your & is referring to. Otherwise (as @Patrick Trentin clarified) your logical operators are combined according to operator precedence (within the same level of precedence they are evaluated from left to right).

Example:

> FALSE & TRUE | TRUE #equivalent to (FALSE & TRUE) | TRUE
[1] TRUE
> FALSE & (TRUE | TRUE)
[1] FALSE

So in your case you can try something like below (assuming you want items that A == 1 & that meet one of the other conditions):

Data$A==1 & (Data$B==1 | Data$C==1 | Data$D==1)

Since you didn't provide the data you're working with, I've replicated some here.

set.seed(20)
Data = data.frame(A = sample(0:1, 10, replace=TRUE),
                  B = sample(0:1, 10, replace=TRUE),
                  C = sample(0:1, 10, replace=TRUE),
                  D = sample(0:1, 10, replace=TRUE))

If you use parenthesis, which can evaluate to a logical function, you can achieve what you're looking for.

Subset1 <- subset(Data,
                  Data$A==1 &
                    (Data$B == 1 |
                    Data$C == 1 |
                    Data$D ==1),
                  select=A)
Subset1
  A
1 1
2 1
4 1
5 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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