繁体   English   中英

如何让 R 在 ifelse 语句中忽略 NA

[英]How to get R to ignore NA in ifelse statement

我为一些模型输出创建了一个表,并在 0.05 级别添加了一个显着性列。

codominant_table$`Significance (Unadjusted)` <- ifelse(codominant_table$`Unadjusted Global p-value` >= 0.05 & !is.na(codominant_table$`Unadjusted Global p-value`), "Not Significant", "Significant")

codominant_table$`Significance (Adjusted)` <- ifelse(codominant_table$`Adjusted Global p-value` >= 0.05  & !is.na(codominant_table$`Adjusted Global p-value`), "Not Significant", "Significant")

出于某种原因,即使我指定了 '!is.na(codominant_table$ Adjusted Global p-value )',该表仍然认为“NA”值小于 0.05,并将其评为“显着”。 这是当前表输出:

表输出

如您所见,全局 p 值列中的 NA 被评为“显着”。 如何让 R 忽略ifelse语句中的 NA?

条件不是评估您的思维方式。 它应该是“如果 p 值 >= 0.05 或 pvalue 缺失,则‘不重要’否则‘重要’。”

现在你有“如果 pvalue >= 0.05 并且该值没有丢失,那么'不重要' ELSE '重要'。” 在条件的 >= 部分中,NA 值将评估为 NA(不是 TRUE 或 FALSE),这会打乱您想要进行的比较。

x <- c(1, 0.01, NA)

# Is NOT missing
!is.na(x)
#> [1]  TRUE  TRUE FALSE

# Is greater than 0.05
x > 0.05
#> [1]  TRUE FALSE    NA ## Note result is NA here not FALSE

# "Is not significant" as 
# Is NOT missing AND is greater than 0.05
x
#> [1] 1.00 0.01   NA
!is.na(x) & x > 0.05  ## not what you want
#> [1]  TRUE FALSE FALSE

# "Is not significant" as 
# IS missing OR is greater than 0.05
x
#> [1] 1.00 0.01   NA
is.na(x) | x > 0.05
#> [1]  TRUE FALSE  TRUE

暂无
暂无

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

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