Learn R Programming

sjmisc (version 1.0.3)

set_na: Set NA for specific variable values

Description

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

Usage

set_na(x, values)

Arguments

x
a variable (vector), data.frame or list of variables 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

  • x, where each value of values is replaced by an NA.

See Also

replace_na to replace NA's with specific value, 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)

# create list of variables
data(efc)
dummy <- list(efc$c82cop1, efc$c83cop2, efc$c84cop3)
# check original distribution of categories
lapply(dummy, table, exclude = NULL)
# set 3 to NA
lapply(set_na(dummy, 3), table, exclude = NULL)

Run the code above in your browser using DataLab