rr-haven

Extract Value Labels from a Stata file loaded with Haven (Value Labels not Variable Labels)


I am trying to get a list of the value labels from a data.frame I loaded with haven. My variables are stored as haven_labelled and I know that the value labels are there because when I run str() they are listed as an attribute.

 str( x$tranwork )
 'haven_labelled' num [1:498381] NA NA NA NA NA NA NA NA NA NA ...
 - attr(*, "label")= chr "Means of transportation to work"
 - attr(*, "format.stata")= chr "%24.0g"
 - attr(*, "labels")= Named num [1:19] 0 10 11 12 13 14 15 20 30 31 ...
  ..- attr(*, "names")= chr [1:19] "N/A " "Auto, truck, or van" "Auto" "Driver" ...
> 

There seem to be alot of good ways to get the variable label. I can't figure out how to get the value label Variable labels in the R package Haven with SPSS or Convenient way to access variables label after importing Stata data with haven

I have tried converting variables to factors, and

attr( x$tranwork , "label" )
[1] "Means of transportation to work"
> attr( x$tranwork , "names" )
NULL

Essentially I would like to see the label associated with x$transwork 1- through 19


Solution

  • There are a few ways to get the value labels.

    With the labelled package:

    library(labelled)
    names(val_labels(x$tranwork))
    

    With the sjlabelled package:

    sjlabelled::get_labels(x$tranwork)
    

    With base:

    names(attr(x$tranwork, "labels"))
    

    If you want to see the value labels along with the values, then use:

    labelled::val_labels(x$tranwork)
    

    or

    attr(x$tranwork, "labels")