Learn R Programming

sjmisc (version 1.0.2)

set_na: Set NA for specific variable values

Description

This function sets specific values of a variable or data frame as missings (NA).

Usage

set_na(x, values)

Arguments

x
a variable (vector) or a data frame where new missing values should be defined. If x is a data frame, each column is assumed to be a new variable, where missings should be defined.
values
a numeric vector with values that should be replaced with NA's. Thus, for each variable in x, values are replaced by NA's.

Value

  • The variable or data frame x, where each value of values is replaced by an NA.

See Also

rec for general recoding of variables and recode_to for re-shifting value ranges.

Examples

Run this code
# create random variable
dummy <- sample(1:8, 100, replace = TRUE)
# show value distribution
table(dummy)
# set value 1 and 8 as missings
dummy <- set_na(dummy, c(1, 8))
# show value distribution, including missings
table(dummy, exclude = NULL)

# create sample data frame
dummy <- data.frame(var1 = sample(1:8, 100, replace = TRUE),
                    var2 = sample(1:10, 100, replace = TRUE),
                    var3 = sample(1:6, 100, replace = TRUE))
# show head of data frame
head(dummy)
# set value 2 and 4 as missings
dummy <- set_na(dummy, c(2, 4))
# show head of new data frame
head(dummy)

Run the code above in your browser using DataLab