Hadley Wickham\'s haven
package, applied to a Stata file, returns a tibble with many columns of type \"labeled\". You can see these with str(), e.g.:
Jumping off @omar-waslow answer above, but adding the use of attr_getter
.
If the data (some_df
) is imported using read_dta
in the haven
package, then each column in the tibble has an attr
called "label"
. So we split up the dataframe, going column by column. This creates a two column dataframe which can be joined back (after pivot_longer, for example).
library(tidyverse)
label_lookup_map <- tibble(
col_name = some_df %>% names(),
labels = some_df %>% map_chr(attr_getter("label"))
)