Learn R Programming

sjmisc (version 1.8)

to_na: Convert missing values of labelled variables into NA

Description

This function converts defined missing values that are stored as original value code into NA.

Usage

to_na(x)

Arguments

x
Variable (vector), data.frame or list of variables with value label attributes and defined missing value codes (see labelled).

Value

x, where each value code of missing values is converted to NA.

Details

to_na converts values to NA, which are defined as missing through the is_na-attribute of a vector (see labelled). set_na, by contrast, converts those values to NA that are specified in the function's values argument; hence, set_na ignores the is_na-attribute. Furthermore, see 'Details' in get_values and get_na.

See Also

get_na to get value codes of missing values.

Examples

Run this code
# create labelled factor, with missing flag
x <- labelled(c("M", "M", "F", "X", "N/A"),
              c(Male = "M", Female = "F",
                Refused = "X", "Not applicable" = "N/A"),
              c(FALSE, FALSE, TRUE, TRUE))
x
get_na(x)
to_na(x)

# create labelled integer, with missing flag
x <- labelled(c(1, 2, 1, 3, 4, 1),
             c(Male = 1, Female = 2, Refused = 3, "N/A" = 4),
             c(FALSE, FALSE, TRUE, TRUE))
x
get_na(x)
to_na(x)

# get summary
x <- labelled(c(1, 2, 1, 3, 4, 1, NA, 5),
              c(Male = 1, Female = 2, Refused = 5),
              c(FALSE, FALSE, TRUE))
frq(x)

Run the code above in your browser using DataLab