Extract the labels attribute from “labeled” tibble columns from a haven import from Stata

后端 未结 3 1238
长发绾君心
长发绾君心 2021-02-02 16:24

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.:



        
3条回答
  •  旧巷少年郎
    2021-02-02 17:16

    The original question asks how 'to extract the values of the labels attribute to a list.' A solution to the main question follows (assuming some_df is imported via haven and has label attributes):

    library(purrr)
    n <- ncol(some_df)
    labels_list <- map(1:n, function(x) attr(some_df[[x]], "label") )
    
    # if a vector of character strings is preferable
    labels_vector <- map_chr(1:n, function(x) attr(some_df[[x]], "label") )
    

提交回复
热议问题