Replace missing value placeholders with NA
Arguments
- df
A data frame containing raw survey data. Must have columns
entity_idand the variable specified byvar.- dict_df
A data frame. Data dictionary for
dffrom the data provider. Expected columns:variable: variable namename: value codemissing: whether the value is a missing value placeholderlabel: value label
- var
A string indicating the name of the variable in
dfto clean.
Value
A data frame with the same number of rows as df with the following
columns: entity_id and the cleaned variable, with missing value
placeholders replaced by NA.
Details
Missing codes are replaced with a plain NA, regardless of the reason for
missingness. Specialized NA values (e.g.,
haven's NA(a) or NA(z)) are not used, so
distinctions such as "refused" vs. "don't know" are not preserved.
Examples
df <- data.frame(
entity_id = c(8291, 8657, 3279, 9743, 5952),
GEN_HLTH_COM = c(1, 4, 5, 3, 9)
)
dict_df <- data.frame(
variable = rep("GEN_HLTH_COM", 7),
name = c(1:5, 8, 9),
missing = c(rep(0, 5), rep(1, 2)),
label = c(
"Excellent",
"Very good",
"Good",
"Fair",
"Poor",
"[DO NOT READ] Don't know/No answer",
"[DO NOT READ] Refused"
)
)
recode_missing(df, dict_df, "GEN_HLTH_COM")
#> entity_id GEN_HLTH_COM
#> 1 8291 1
#> 2 8657 4
#> 3 3279 5
#> 4 9743 3
#> 5 5952 NA
