ifelse: A factor-safe version of 'base::ifelse'

ifelseR Documentation

A factor-safe version of base::ifelse

Description

The ifelse function does weird things when you pass it factors. This version doesn't.

Usage

ifelse(test, yes, no)

Arguments

test

a vector of logicals

yes

values to return if test is TRUE

no

values to return if test if FALSE

Value

For each position, ifelse returns the value of yes or no depending on the value of test.

Examples

ifelse(
  c(TRUE, FALSE),
  c('Yes', 'Yeah!'),
  c('No', 'Nope'))

# base ifelse does weird stuff with factors
yesf <- factor(c('Yes', 'Yeah!'))
nof <- factor(c('No', 'Nope'))
base::ifelse(c(TRUE, FALSE), yesf, nof)
alrtools::ifelse(c(TRUE, FALSE), yesf, nof)


adamleerich/alrtools documentation built on March 12, 2024, 11:38 p.m.