简体   繁体   English

R中逻辑子集的无法解释的不匹配

[英]Unexplained mismatch in logical subsetting in R

I am a beginner in R. I have been working with a data frame (named df1 ). 我是R的初学者。我一直在使用数据框(名为df1 )。 HOUSE. NO , E1, D11, DC11 HOUSE. NO , E1, D11, DC11 are various column names in the frame. HOUSE. NO , E1, D11, DC11是框架中的各种列名称。 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 . 但是对于“ 14/13 ”值,当我单独计算逻辑值时,结果为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. 我认为您正在观察的事实是, any一个any适用于整个向量,而==则适用于向量的每个元素。

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. 但是,当您仔细查看仅14/13号房屋时,它的E1肯定等于1,但其D11和DC11都等于1。这是因为在比较中,特别是针对这所房屋,没有其他房屋在any比较中都要通过“ any ”测试。

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) . 换句话说:当应用于所有房屋时any(d11!=1, dc11!=1)返回单个TRUE ,因为(在我们的两个示例中)至少在房屋上D11或DC11不等于1。您将这个单个TRUE与布尔向量(此处为c(TRUE, TRUE, TRUE) )与& ,则返回向量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 . 现在,如果您对门牌号“ 14/13”进行操作,则将( any部分)运行any(d11[house=="14/13"]!=1, dc11[house=="14/13"]!=1)并获得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) ]

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

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