Description Usage Arguments Details Value Examples
This function is similair to ifelse
with differences described in the details section.
1 | safe_ifelse(test, yes, no, na_as_false = TRUE, drop.levels = TRUE)
|
test, yes, no |
arguments passed to |
na_as_false |
should |
drop.levels |
This only applies when |
safe_ifelse
differs from ifelse
in the following ways:
Both 'yes' and 'no' must be vectors of the same type or class. This ensures that the output will be of correct format.
Factors can be combined without problem
The argument na.rm
makes it easier to handle cases when cond = NA
Vector of same length and class as yes
and no
.
1 2 3 4 5 6 7 8 9 10 11 12 | # Test must be TRUE to return 'yes'
safe_ifelse(NA, 1, 2) ## 2
ifelse(NA, 1, 2) ## NA
# Factors are problematic in ifelse
ifelse(TRUE, as.factor("hello"), 2) ## 1
## Not run:
safe_ifelse(TRUE, as.factor("hello"), 2) ## Error
## End(Not run)
safe_ifelse(TRUE, as.factor("hello"), as.factor(2)) ## hello
safe_ifelse(TRUE, as.factor("hello"), as.factor(2), drop.levels = FALSE)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.