v <- labelled(c(1,2,2,2,3,9,1,3,2,NA), c(yes = 1, no = 3, "don't know" = 9))
v
na_values(v) <- 9
na_values(v)
v
is.na(v) # TRUE for the 6th and 10th values
is_user_na(v) # TRUE only for the 6th value
user_na_to_na(v)
na_values(v) <- NULL
v
na_range(v) <- c(5, Inf)
na_range(v)
v
user_na_to_na(v)
user_na_to_tagged_na(v)
# it is not recommended to mix user NAs and tagged NAs
x <- c(NA, 9, tagged_na("a"))
na_values(x) <- 9
x
is.na(x)
is_user_na(x)
is_tagged_na(x)
is_regular_na(x)
if (require(dplyr)) {
# setting value label and user NAs
df <- tibble(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
# example with a vector
v <- 1:10
v <- v %>% set_na_values(5, 6, 7)
v
v %>% set_na_range(8, 10)
v %>% set_na_range(.values = c(9, 10))
v %>% set_na_values(NULL)
}
Run the code above in your browser using DataLab