Skip to contents

Substitutes values when NA is detected.

Usage

ifna(x, y)

Arguments

x

An possibly containing NA values

y

An object of equal length or a single value to be used in substitution

Details

The objective of this function is to provide an ability to substitute values for NA. However, it is important to note that R is unique in the fact that it has an NA data type to represent missingness instead of relying on values being pre-coded (e.g. 0, -1111, et cetera). Thus, the simplicity of this function comes with the disclaimer of:

"An NA is the presence of an absence. Don't forget that some missing values are the absence of a presence"

  • Hadley Wickham on Twitter

Examples

# Data with missing values
x <- c(1, NA, NA, 4)
# Substitution vector of equal length
y <- 1:4

# Replace NA with values in `y` vector
ifna(x, y)
#> [1] 1 2 3 4

# Replace NA with 5
ifna(x, 5)
#> [1] 1 5 5 4