[英]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.