Skip to content

Instantly share code, notes, and snippets.

@n3ssuno
Created November 30, 2022 16:40
Show Gist options
  • Save n3ssuno/c10848b4fbb8f67cc9ebb305c3b1bbe9 to your computer and use it in GitHub Desktop.
Save n3ssuno/c10848b4fbb8f67cc9ebb305c3b1bbe9 to your computer and use it in GitHub Desktop.
Function to extract the "label" attribute from each variable in a data frame (e.g., useful to read the labels of data stored in a dta-type file)
pull_labels <- function(data, as_list = FALSE) {
var_labels <- df %>%
purrr::map(~ attr(.x, "label")) %>%
purrr::map_dfr(~ tibble(label = .x), .id = "name")
if (as_list) {
var_labels <- as.list(spread(var_labels, name, label))
}
return(var_labels)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment