我有下述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更多推荐
更改真假是和否
发布评论