Learn R Programming

DescTools (version 0.99.56)

ZeroIfNA: Replace NAs by 0

Description

Replace NAs in a numeric vector x with 0. This function has the same logic as the zeroifnull function in SQL. NAIfZero() does replace zeros with NA. BlankIfNA() and NAIfBlank() do the same, but for character vectors.

Usage

ZeroIfNA(x)
NAIfZero(x)
NAIf(x, what)

BlankIfNA(x, blank="") NAIfBlank(x)

Impute(x, FUN = function(x) median(x, na.rm = TRUE))

Value

the edited vector x

Arguments

x

the vector x, whose NAs should be overwritten with 0s.

blank

a character to be used for "blank". Default is an empty string ("").

what

a vector of elements to be set to NA in x.

FUN

the name of a function to be used as imputation. Can as well be a self defined function or a constant value. Default is median.

Author

Andri Signorell <andri@signorell.net>

See Also

Examples

Run this code
z <- c(8, NA, 9, NA, 3)

ZeroIfNA(z)
# [1] 8 0 9 0 3

# set 8 and 9 to NA
NAIf(ZeroIfNA(z), what=c(8, 9))


Impute(z)
# [1] 8 8 9 8 3


z <- c("a", NA, "", NA, "k")

BlankIfNA(z)
# [1] "a" "" "" "" "k"

Run the code above in your browser using DataLab