Get / Set SPSS missing values
na_values(x) na_values(x) <- value na_range(x) na_range(x) <- value set_na_values(.data, ...) set_na_range(.data, ...) user_na_to_na(x)
| x | A vector. |
|---|---|
| value | A vector of values that should also be considered as missing
(for |
| .data | a data frame |
| ... | name-value pairs of missing values (see examples) |
na_values will return a vector of values that should also be considered as missing.
na_range will return a numeric vector of length two giving the (inclusive)
extents of the range.
set_na_values and set_na_range will return an updated
copy of .data.
See labelled_spss for a presentation of SPSS's user defined missing values.
Note that is.na will return TRUE for user defined misssing values.
You can use user_na_to_na to convert user defined missing values to NA.
set_na_values and set_na_range could be used with dplyr.
labelled_spss, user_na_to_na
#> <Labelled double> #> [1] 1 2 2 2 3 9 1 3 2 NA #> #> Labels: #> value label #> 1 yes #> 3 no #> 9 don't knowna_values(v) <- 9 na_values(v)#> [1] 9v#> <Labelled SPSS double> #> [1] 1 2 2 2 3 9 1 3 2 NA #> Missing values: 9 #> #> Labels: #> value label #> 1 yes #> 3 no #> 9 don't knowis.na(v)#> [1] FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE TRUEuser_na_to_na(v)#> <Labelled double> #> [1] 1 2 2 2 3 NA 1 3 2 NA #> #> Labels: #> value label #> 1 yes #> 3 no #> 9 don't knowna_values(v) <- NULL v#> <Labelled double> #> [1] 1 2 2 2 3 9 1 3 2 NA #> #> Labels: #> value label #> 1 yes #> 3 no #> 9 don't know#> [1] 5 Infv#> <Labelled SPSS double> #> [1] 1 2 2 2 3 9 1 3 2 NA #> Missing range: [5, Inf] #> #> Labels: #> value label #> 1 yes #> 3 no #> 9 don't knowuser_na_to_na(v)#> <Labelled double> #> [1] 1 2 2 2 3 NA 1 3 2 NA #> #> Labels: #> value label #> 1 yes #> 3 no #> 9 don't knowif (require(dplyr)) { # setting value labels df <- data_frame(s1 = c("M", "M", "F", "F"), s2 = c(1, 1, 2, 9)) %>% set_value_labels(s2 = c(yes = 1, no = 2)) %>% set_na_values(s2 = 9) na_values(df) # removing missing values df <- df %>% set_na_values(s2 = NULL) df$s2 }#> Warning: `data_frame()` is deprecated, use `tibble()`. #> This warning is displayed once per session.#> <Labelled double> #> [1] 1 1 2 9 #> #> Labels: #> value label #> 1 yes #> 2 no