Search code examples
rdplyrtidyversegtsummary

Using tbl_summary to create summary statistics with labels


I have read in a Stata (dta) file into R and a snippet of the data looks like this:

 short
# A tibble: 200 x 5
            q4_1          q4_2          q4_3          q4_4 treatment_cur
       <dbl+lbl>     <dbl+lbl>     <dbl+lbl>     <dbl+lbl> <chr>        
 1 NA(z)         NA(z)         NA(z)         NA(z)         Control      
 2 NA(z)         NA(z)         NA(z)         NA(z)         Control      
 3     1 [1.Yes]     0 [0.No]      0 [0.No]      1 [1.Yes] Treatment    
 4     0 [0.No]      0 [0.No]      1 [1.Yes]     0 [0.No]  Control      
 5     0 [0.No]      0 [0.No]      0 [0.No]      1 [1.Yes] Control      
 6 NA(z)         NA(z)         NA(z)         NA(z)         Control      
 7     1 [1.Yes]     1 [1.Yes]     1 [1.Yes]     1 [1.Yes] Control      
 8 NA(z)         NA(z)         NA(z)         NA(z)         Treatment    
 9 NA(z)         NA(z)         NA(z)         NA(z)         Control      
10     0 [0.No]      0 [0.No]      1 [1.Yes]     0 [0.No]  Control 

The format of the variables is such:

str(short)
tibble [200 x 5] (S3: tbl_df/tbl/data.frame)
 $ q4_1         : dbl+lbl [1:200] NA(z), NA(z),     1,     0,     0, NA(z),     1, NA(z), NA(z),     0, NA(z),     1, NA(z),     1, NA(z),     1, ...
   ..@ label       : chr "q4_1r.Do you have any of ...assignments? Bilingual/ELL"
   ..@ format.stata: chr "%15.0g"
   ..@ labels      : Named num [1:2] 0 1
   .. ..- attr(*, "names")= chr [1:2] "0.No" "1.Yes"
 $ q4_2         : dbl+lbl [1:200] NA(z), NA(z),     0,     0,     0, NA(z),     1, NA(z), NA(z),     0, NA(z),     0, NA(z),     0, NA(z),     0, ...
   ..@ label       : chr "q4_2r.Do you have any of ...assignments? Sp Ed (self-c)"
   ..@ format.stata: chr "%34.0g"
   ..@ labels      : Named num [1:2] 0 1
   .. ..- attr(*, "names")= chr [1:2] "0.No" "1.Yes"
 $ q4_3         : dbl+lbl [1:200] NA(z), NA(z),     0,     1,     0, NA(z),     1, NA(z), NA(z),     1, NA(z),     1, NA(z),     1, NA(z),     0, ...
   ..@ label       : chr "q4_3r.Do you have any of ...assignments? Sp Ed (incl.)"
   ..@ format.stata: chr "%72.0g"
   ..@ labels      : Named num [1:2] 0 1
   .. ..- attr(*, "names")= chr [1:2] "0.No" "1.Yes"
 $ q4_4         : dbl+lbl [1:200] NA(z), NA(z),     1,     0,     1, NA(z),     1, NA(z), NA(z),     0, NA(z),     1, NA(z),     0, NA(z),     0, ...
   ..@ label       : chr "q4_4r.Do you have any of ...assignments? Gifted/Talented"
   ..@ format.stata: chr "%17.0g"
   ..@ labels      : Named num [1:2] 0 1
   .. ..- attr(*, "names")= chr [1:2] "0.No" "1.Yes"
 $ treatment_cur: chr [1:200] "Control" "Control" "Treatment" "Control" ...
  ..- attr(*, "label")= chr "treatment_cur.treatment_cur"
  ..- attr(*, "format.stata")= chr "%9s"

This is the class of each variable:

> class(short$q4_1)
[1] "haven_labelled" "vctrs_vctr"     "double"

I need to create descriptive tabulations of the data using tbl_summary from library(gtsummary)--which is a really cool package to create quick and customizable summary stats of the data.

The cool thing about my data is that each value already has a label associated with it. For example in q4_2, 0 is "No" and 1 is "Yes". So that when I feed the data into tbl_summary, instead of this showing up in the freq count:

q4_1    n
   1    7 
   0    8

This can show up instead This is what I want:

"q4_1r.Do you have any of ...assignments? Bilingual/ELL"    
        n 
   No   7
   Yes  8

This code is not working because tbl_summary only accepts certain formats.

tbl_summary(short)
Column(s) ‘q4_1’, ‘q4_2’, ‘q4_3’, and ‘q4_4’ omitted from output.
Accepted classes are ‘character’, ‘factor’, ‘numeric’, ‘logical’, ‘integer’, or ‘difftime’. 

If I convert these variables into characters, they lose their value labels, and I only see the following, because converting it to a character makes the variable lose its label attributes.

q4_1    n
   1    7 
   0    8

Are there any idea's for how I can work around this? I can't find an inbuilt R file that has this type of var format to make this more reproducible.


Solution

  • In the the case of the haven labelled class, it was never meant to be a class that was used in analysis or data exploration. Rather, it was created as an in-between when importing data from other languages where the data types don't have a one-to-one relationship with R. This is from a tidyverse blog post about the haven labelled class of variables. (https://haven.tidyverse.org/articles/semantics.html)

    The goal of haven is not to provide a labelled vector that you can use everywhere in your analysis. The goal is to provide an intermediate data structure that you can convert into a regular R data frame.

    To use tbl_summary() you'll first want to apply the as_factor() function on your imported data frame, e.g. haven::as_factor(short). This will convert your data frame to base R types, and retain the Stata value labels as factors.

    FYI, we are making tbl_summary() compatible with all types, and in the next release of the package the as_factor() step will not be required. You can follow the progress of the implementation here: https://github.com/ddsjoberg/gtsummary/pull/603