Last active
November 30, 2019 17:46
-
-
Save JosiahParry/a2908641951aad084d9e7e682ebcc5e8 to your computer and use it in GitHub Desktop.
The SPSS hellscape adds questions as the column label. I want these as the col header
This file contains hidden or 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
label_to_colname <- function(df, pattern) { | |
get_label <- purrr::attr_getter("label") | |
col_labels <- purrr::map(df, get_label) | |
col_labels[unlist(map(col_labels, is.null))] <- names(col_labels[unlist(purrr::map(col_labels, is.null))]) | |
if (missing(pattern)) { | |
names_to_replace <- rep(TRUE, ncol(df)) | |
} else { | |
names_to_replace <- stringr::str_detect(names(col_labels), pattern) | |
} | |
colnames(df)[names_to_replace] <- janitor::make_clean_names(unlist(col_labels[names_to_replace])) | |
haven::zap_label(df) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment