简体   繁体   中英

Unexplained mismatch in logical subsetting in R

I am a beginner in R. I have been working with a data frame (named df1 ). HOUSE. NO , E1, D11, DC11 HOUSE. NO , E1, D11, DC11 are various column names in the frame. Following is the result of the logical subsetting I performed.

   df1
   HOUSE.NO D11 DC11 E1
75   16/215   2    2 NA
76   15/262   1    1  2
77   16/220   1    1  2
78    14/13   1    1  1
79     14/9   2    2 NA
df1$HOUSE.NO[df1$E1==1&any(!df1$D11==1,!df1$DC11==1)]
[1] NA      "14/13" NA 

But for the " 14/13 " value, when I individually computed the value of the logical, it came out FALSE .

df1$E1[df1$HOUSE.NO=="14/13"]==1&any(df1$D11[df1$HOUSE.NO=="14/13"]!=1, df1$DC11[df1$HOUSE.NO=="14/13"]!=1)
[1] FALSE

I am unable to see how this came about. I also independently checked the Data frame and it made sense for it to come false. Please let me know why did this happen.

I think what you are observing is the fact that any applies to a whole vector while == is applied to each element of the vector.

For example:

e1 <- c(1, 1, 1)
d11 <- c(1, 2, 2)
dc11 <- c(1, 1, 2)
house <- c("14/13", "a", "b", "c")

When you test this

house[e1==1 & any(d11!=1, dc11!=1) ]

all the houses are said to fulfill the condition, which is correct. But when you take a closer look at only house number 14/13, its E1 sure is equal to 1, but its D11 and DC11 are both equal to 1. That's because in the comparison focused on this house particularly, there is no other house in the any comparison to pass the " any " test.

In other words: any(d11!=1, dc11!=1) returns a single TRUE when applied to all houses because (in both our examples) there is at least on house for which D11 or DC11 is not equal to 1. When you combine this single TRUE with a vector of booleans (here: c(TRUE, TRUE, TRUE) ) with & , it returns the vector c(TRUE & TRUE, TRUE & TRUE, TRUE & TRUE) .

Now if you make the operation for house number "14/13", you will run (for the any part) any(d11[house=="14/13"]!=1, dc11[house=="14/13"]!=1) and get FALSE .

In conclusion, the command you want to run is

house[ e1[house=="14/13"]]==1 & any(d11!=1, dc11!=1) ]

and not

house[ e1[house=="14/13"]]==1 & 
    any(d11[house=="14/13"]!=1, dc11[house=="14/13"]!=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