Learn R Programming

lambda.tools (version 1.0.9)

use_default: Apply a default value whenever a variable is not well-formed

Description

This function provides a functional approach for a specific use case of conditional expressions: that of applying default values when a variable is not well-formed. In this context, well-formedness is considered to be any scalar value that is not NA. By encapsulating this behavior in a function, referential transparency is preserved.

Arguments

x
a scalar variable
default
the value to replace empty, NULL, or NA

Value

A well-formed value, either the original value or the default if x is not well-formed.

See Also

onlyif

Examples

Run this code
x <- c(1, 2, 3, NA, NA)
map(x, function(y) use_default(y, 0))

Run the code above in your browser using DataLab