更改真假是和否 [英] Change TRUE and FALSE to Yes and No

查看:94
本文介绍了更改真假是和否的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下述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

这篇关于更改真假是和否的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆