更改真假是和否

编程入门 行业动态 更新时间:2024-10-06 16:18:53
本文介绍了更改真假是和否的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有下述R脚本:

x <- c('PE', 'MG', 'SP', 'GO', 'ES', 'PB', 'DF') y <- c('PB', 'MG', 'SP', 'GO', 'ES', 'SE', 'DF') z <- x == y

这样

> z [1] FALSE TRUE TRUE TRUE TRUE FALSE TRUE

不过,我想以Z (和其他逻辑变量进一步下跌的脚本),以显示是和否来代替,所以我这样做录音:

However, I want z (and other logical variables further down the script) to show "Yes" and "No" instead, so I do this recoding:

z <- ifelse(z == TRUE, "Yes", "No")

有没有办法跳过这个额外的步骤,即定义以Z 显示是,而不是真与否,而不是假。

Is there any way to skip this extra step, i.e., define z show "Yes" instead of "TRUE" and "No" instead of "FALSE".

当然,我也可以做 z,其中; - ifelse(X == Y,是,否),但我仍然在寻找有点像选项里面的参数()的功能,我只定义一次,有它直到脚本的工作进行到底(或直到我重新定义了参数)。无法找到这样的事情?选项。

Of course, I could also do z <- ifelse(x == y, "Yes", "No"), but I'm still looking for something like a parameter inside the options() function I could define just once and have it work until the end of the script (or until I redefined the parameter). Couldn't find anything like it on ?options.

推荐答案

据我所知是没有办法覆盖的事实,研究打印 TRUE 和 FALSE 逻辑对象(我个人认为这是很好的)。

As far as I know there is no way to overwrite the fact that R prints TRUE and FALSE for logical objects (personally I think that's good).

最近的解决方案,你要寻找的可能是因素转换:

The closest solution to what you're looking for could be factor conversion:

z <- factor(x==y, labels=c("No", "Yes")) > z [1] No Yes Yes Yes Yes No Yes Levels: No Yes

更多推荐

更改真假是和否

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

发布评论

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

>www.elefans.com

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