Skip to contents

Replaces tagged NAs with their original missing value codes. Works with data imported via read_spss() (with tag_na = TRUE) or any reader that used the tag_na parameter (read_stata(), read_sas(), read_xpt()). For native Stata/SAS tagged NAs (e.g., .a, .A) that have no numeric codes to recover, use strip_tags() instead.

Usage

untag_na(x, ...)

Arguments

x

A numeric vector with tagged NAs, or a data frame.

...

For a data frame: the columns to convert (tidyselect). If empty, every numeric column is converted. Ignored for vectors.

Value

The input with tagged NAs that have numeric codes replaced by their original values (e.g., -9, -8, -42). System NAs (untagged) remain NA. Value labels and the variable label are kept (labels of missing types are attached to their codes again), so the result is haven_labelled when the input was. For native Stata/SAS tagged NAs (no numeric codes), falls back to strip_tags() behavior with a warning. A data frame is returned with the selected columns converted.

Examples

# \donttest{
if (requireNamespace("haven", quietly = TRUE)) {
  # Tag -9/-8 as missing, then recover the original codes
  x <- set_na(c(1, 2, -9, 3, -8), -9, -8)
  untag_na(x)   # -9 and -8 are back
}
#> <labelled<double>[5]>
#> [1]  1  2 -9  3 -8
# }