简体   繁体   English

R 中带有 NA 的逻辑索引 - 如何设置为 FALSE 或排除而不是返回 NA?

[英]Logical Indexing with NA in R - How to set to FALSE or exclude rather than return NA?

Apologies if this is a common question, but it has caused some unexpected frustration in a script I am running.如果这是一个常见问题,我们深表歉意,但它在我正在运行的脚本中引起了一些意想不到的挫败感。 I have a dataset which roughly looks like the following (though much larger in practice):我有一个大致如下所示的数据集(尽管实际上要大得多):

df <- data.frame(A = c(1, 2, 3, NA, NA, 6), 
                 B = c(10, 20, 30, 40 , 50, 60))

My script cycles through a list of values from column A and is supposed to take action based on whether the values in B are larger than 25. However, the corresponding values of B for missing values in A are ALWAYS returned, whereas I want them to always be excluded.我的脚本循环遍历列 A 中的值列表,并且应该根据 B 中的值是否大于 25 来采取行动。但是,总是返回 A 中缺失值的 B 的相应值,而我希望它们总是被排除在外。 For example,例如,

df$B[df$A == 6]

Gives the output给output

NA NA 60

Rather than the expected而不是预期的

60

Thus, the code因此,代码

df$B[df$A == 6] > 25

returns返回

NA NA TRUE

rather than just而不仅仅是

TRUE

Could someone explain the reason for this and any simple solutions?有人可以解释一下原因和任何简单的解决方案吗? The immediate solution that came to mind is to remove any rows with NA values in column A, but I would prefer a solution which is robust to missingness in A and will only return the single desired logical value from B.想到的直接解决方案是删除 A 列中具有 NA 值的任何行,但我更喜欢一种对 A 中的缺失具有鲁棒性并且只会从 B 返回单个所需逻辑值的解决方案。

Whenever you ask whether N ot A vailable ( NA ) value is equal to number or anything else - you got the only possible answer: The answer is Not Available ( NA ) .每当您询问 Not A vailable ( NA ) 值是否等于number其他任何值时,您都会得到唯一可能的答案:答案是Not Available ( NA )

NA might be equal to 6 , or to John the Baptist , or to ⛄ as well as to any other object . NA可能等于6John the Baptist或 ⛄ 以及任何其他 object It is just impossible to say if it does, since the value is n ot a vailable .只是不可能说它是否确实如此,因为该值不是vailable

To get the answer you want, you can use na.omit() or na.exclude() on the results.要获得您想要的答案,您可以对结果使用na.omit()na.exclude() Or you can apply yet another logical condition during subsetting:或者您可以在子集期间应用另一个逻辑条件

with(df, B[A == 6 & !is.na(A)])
# [1] 60

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

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