我有以下矢量:
A <- c(8.4, 9.5, 8.1) B <- c(NA,NA,NA)我希望R执行以下操作:如果列A中的值小于8.5,或者列B中的值小于8,则写入“TRUE”,否则写入“FALSE”。
我尝试了以下内容:
C <- (A <8.5 | B <8)我期待以下内容:TRUE,FALSE,TRUE
但结果是:
> C # [1] TRUE NA TRUE很显然,当R看到A在第二个变量中不小于8.5时,它就到达了B,当有NA时,它将NA写为输出。
你有什么建议可以避免这种情况吗?
I have following vectors:
A <- c(8.4, 9.5, 8.1) B <- c(NA,NA,NA)I wanted R to do the following: if in column A the values are less than 8.5 or in column B values are less than 8 to write "TRUE", otherwise write "FALSE".
I tried the following:
C <- (A <8.5 | B <8)I was expecting the following: TRUE, FALSE, TRUE
But the outcome was:
> C # [1] TRUE NA TRUEObviously when the R saw that A was not less than 8.5 in the second variable it went to B and when there was NA it wrote NA as an output.
Do you have any suggestions how I can avoid this?
最满意答案
使用isTRUE :
isTRUE( FALSE | NA) [1] FALSE使用“C”作为变量名是不明智的,因为“c”和“C”都是(不同的)R函数。 但以你的榜样:
> sapply(C, isTRUE) [1] TRUE FALSE TRUEUse isTRUE:
isTRUE( FALSE | NA) [1] FALSEIt is unwise to use "C" as a variable name because both "c" and "C" are (different) R functions. But with your example:
> sapply(C, isTRUE) [1] TRUE FALSE TRUE更多推荐
发布评论