Created
November 30, 2022 16:40
-
-
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)
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
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