将 TRUE 和 FALSE 更改为 Yes 和 No [英] Change TRUE and FALSE to Yes and No

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

问题描述

我有以下 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 显示Yes"而不是TRUE"和No"而不是FALSE".

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, "Yes", "No"),但我仍在寻找类似参数的东西options() 函数我可以只定义一次并让它工作到脚本结束(或直到我重新定义参数).在 ?options 上找不到类似的东西.

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.

推荐答案

据我所知,没有办法覆盖 R 打印 TRUEFALSE 用于逻辑对象(我个人认为这很好).

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

这篇关于将 TRUE 和 FALSE 更改为 Yes 和 No的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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