Search code examples
rsummarytools

Having Problems with Labels on "freq" Command in R


so my general problem is that I want to analyse a nominal variable called "eseg" - which indicates socioeconomic status. I want to use the "freq" command from the "summarytools" package. However, the problem is that the variable eseg is only nominally scaled by the labels, so in the tabular output they show numbers instead of labels.

My problem is that I can't manage to make the labels already stored in the data set visible despite the help function

To solve this problem myself, I tried to use the help command and found some options that might help me solve the problem. Unfortunately, these options did not help and the labels are still invisible. The code currently looks like this:

freq(df_work$eseg, headings = st_options("headings"), 
     display.labels = st_options("display.labels"))

Quick additions that may simplify the solution: The variables are already labelled and the command runs (without the labels) smoothly - even the options do not generate an error message, but the output does not change either

Can anyone help me further?

(If anyone has found a way to generate tables with labels in a different way, I would also be grateful for new problem-solving approaches)


Solution

  • It appears from your post that you've imported the data using the haven package and your eseg variable is a labelled numeric variable. The display.labels argument of the freq function applies to the variable (or data frame) label, not the levels of a factor variable (or the labels attributes of a numeric variable). So, you will need to convert your eseg variable to a factor to see its levels.

    Example:

    library(haven)
    
    path <- system.file("examples", "iris.sav", package = "haven")
    iris2 <- read_sav(path)
    
    class(iris2$Species)
    #[1] "haven_labelled" "vctrs_vctr"     "double"
    
    (Species.labels <- attributes(iris2$Species)$labels
    #setosa versicolor  virginica 
    #     1          2          3
    
    iris2$Species <- factor(iris2$Species, 
                            levels=Species.labels,
                            labels=names(Species.labels))
    
    freq(iris2$Species)
    

    Frequencies  
    iris2$Species  
    Type: Factor  
    
                       Freq   % Valid   % Valid Cum.   % Total   % Total Cum.
    ---------------- ------ --------- -------------- --------- --------------
              setosa     50     33.33          33.33     33.33          33.33
          versicolor     50     33.33          66.67     33.33          66.67
           virginica     50     33.33         100.00     33.33         100.00
                <NA>      0                               0.00         100.00
               Total    150    100.00         100.00    100.00         100.00