if_na: Return a value or expression if something is NA

View source: R/vector_tools.R

if_naR Documentation

Return a value or expression if something is NA

Description

This is just shorthand for ifelse(is.na(x), TRUE, FALSE) because I like using that pattern in my dplyr pipelines.

Usage

if_na(x, yes = TRUE, no = NULL)

Arguments

x

(Vector) A vector to test.

yes

(Any) The value to return if x is NA.

no

(Any) The value to return if x is not NA. Set this to NULL (its default setting) to return x.

Value

The object in yes or no, depending on the outcome.

Authors

Examples

vec <- c("hello", NA, "hi")

if_na(vec, "REPLACED")
#> [1] "hello"    "REPLACED" "hi"

if_na(vec, "Was NA", "Was not NA")
#> [1] "Was not NA" "Was NA"     "Was not NA"


DesiQuintans/desiderata documentation built on April 9, 2023, 5:43 a.m.