R中的TRUE / FALSE结果(TRUE/FALSE outcome in R)

编程入门 行业动态 更新时间:2024-10-26 06:27:34
R中的TRUE / FALSE结果(TRUE/FALSE outcome in R)

我有以下矢量:

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 TRUE

Obviously 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 TRUE

Use isTRUE:

isTRUE( FALSE | NA) [1] FALSE

It 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

更多推荐

本文发布于:2023-07-26 00:10:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1268306.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:TRUE   FALSE   outcome

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!