Skip to content

Instantly share code, notes, and snippets.

@JosiahParry
Last active November 30, 2019 17:46
Show Gist options
  • Save JosiahParry/a2908641951aad084d9e7e682ebcc5e8 to your computer and use it in GitHub Desktop.
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
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