I have the following R script:
x <- c('PE', 'MG', 'SP', 'GO', 'ES', 'PB', 'DF')
y <- c('PB', 'MG', 'SP', 'GO', 'ES', 'SE', 'DF')
z <- x == y
So that
> z
[1] FALSE TRUE TRUE TRUE TRUE FALSE TRUE
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")
Is there any way to skip this extra step, i.e., define z
show "Yes" instead of "TRUE" and "No" instead of "FALSE".
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
.
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
Another approach:
c('No', 'Yes')[z + 1]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With