if_else: Vectorised if

View source: R/if_else.R

if_elseR Documentation

Vectorised if

Description

if_else() returns a vector filled with elements selected from true, false, and (optionally) na based on the value of condition. It is a type- and size-stable version of ifelse().

Usage

if_else(condition, true, false, na = NULL)

Arguments

condition

A logical vector.

true, false

Values to use for TRUE and FALSE values of condition.

na

Values to use for NA.

Value

A vector with the common type of true, false, and na; and the size of condition. Where condition is TRUE, the result comes from true, where FALSE it comes from false.

Examples

x <- c(NA, 1:4)
if_else(x > 2, 1, 2)
if_else(x > 2, "small", "big")
if_else(x > 2, factor("small"), factor("big"))

y <- as.Date("2020-01-01")
if_else(x > 2, NA, y + x)

tidyverse/funs documentation built on May 21, 2022, 12:12 p.m.