Created
August 11, 2020 03:45
-
-
Save guyabel/e6cb53d09ca21c0357b6981d9dbf685e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ipums_clean_dta <- function( | |
d = NULL, numeric_convert = NULL, string_as_factors = TRUE, clean_labels = FALSE, | |
mis_lab = c("unknown", "niu (not in universe)", "not reported/missing", | |
"unknown/missing", "response suppressed", "unclassifiable") | |
){ | |
d %>% | |
{if(clean_labels) dplyr::mutate_if(is.labelled, ipumsr::lbl_clean) else .} %>% | |
dplyr::mutate_if(is.labelled, ~ipumsr::lbl_na_if(., ~.lbl %in% mis_lab)) %>% | |
dplyr::mutate_at({numeric_convert}, haven::zap_labels) %>% | |
dplyr::mutate_if(is.labelled, haven::as_factor) %>% | |
{if(!string_as_factors) dplyr::mutate_if(., is.factor, as.character) else .} | |
} | |
# library(devtools) | |
# source_url("https://raw.github.com/guyabel/teaching/master/ipums_clean_dta.R") | |
# ipums_clean_dta(d0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment