Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change TRUE and FALSE to Yes and No

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.

like image 614
Waldir Leoncio Avatar asked Dec 01 '22 18:12

Waldir Leoncio


2 Answers

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
like image 145
Señor O Avatar answered Dec 28 '22 23:12

Señor O


Another approach:

c('No', 'Yes')[z + 1]
like image 45
eddi Avatar answered Dec 28 '22 23:12

eddi