if_else: Transform a vector using a condition

View source: R/if_else.R

if_elseR Documentation

Transform a vector using a condition

Description

Specify a condition on a vector using a logical operator. Where the condition evaluates to a TRUE, the vector will take on one value; otherwise, the vector will take on another.

Usage

if_else(condition, true, false)


Arguments

condition

a logical condition on a vector.

true

A value to use for elements of the vector where the condition is TRUE.

false

A value to use for elements of the vector where the condition is FALSE.

Details

Logical operators include:

==: "equal to"

!=: "not equal to"

>, >=, <, <=: "greater than", "greater than or equal to", etc.

%in%: "in"

Examples

# `if_else` can be used on vectors:

x <- c(-2, 1, -1)

if_else(x >= 0, "positive", "negative")

#> [1] "negative" "positive" "negative"

-----------------------------------

# `if_else` can also be used with `mutate` on tibbles:

tibble(
  x = c(2, 1, 2, -4, -1),
  y = c(3, 1, 2, -2, 0)
) %>%
  mutate(z = if_else(x == y, 1, 0))

#> A tibble: 5 x 3
#      x     y     z
#  <dbl> <dbl> <dbl>
#     -2     3     0
#      1     1     1
#      2     2     1
#     -4    -2     0
#     -1     0     0



cobriant/qelp documentation built on July 1, 2022, 7:24 a.m.