Search code examples
rexpss

R Programming: How to drop variable labels as first column name in the table output from the fre function of the expss package?


I'm doing exploratory analysis of survey data and the dataframe is a haven labelled dataset, that is, each variable already has value labels and variable labels.

I want to store frequencies tables in a list, and then name each list element as the variable label. I'm using the expss package. The problem is that the output tables contain in the first column name this description: values2labels(Df$var. How can this description be dropped from the table?

Reproducible example:

# Dataframe 
df <- data.frame(sex = c(1, 1, 2, 2, 1, 2, 2, 2, 1, 2),
                 agegroup= c(1, 3, 1, 2, 3, 3, 2, 2, 2, 1),
                 weight = c(100, 20, 400, 300, 50, 50, 80, 250, 100, 100))
library(expss)

# Variable labels
   var_lab(df$sex) <-"Sex"
   var_lab(df$agegroup) <-"Age group"

# Value labels 
   val_lab(df$sex) <- make_labels("1 Male 
                               2 Female")

   val_lab(df$agegroup) <- make_labels("1 1-29
                                        2 30-49
                                        3 50 and more")

# Save variable labels  
   var_labels1 <- var_lab(df$sex)
   var_labels2 <- var_lab(df$agegroup)

# Drop variable labels
   var_lab(df$sex) <- NULL
   var_lab(df$agegroup) <- NULL

# Save frequencies
   f1 <- fre(values2labels(df$sex))
   f2 <- fre(values2labels(df$agegroup))

  # Note: I use the function 'values2labels' from 'expss' package in order to display the value <br />
      labels instead of the values of the variable.In this example, since I manually created the value <br /> 
      labels, I don't need that function, but when I import haven labelled data, I need it to 
      display value labels by default.

# Add frequencies on list
   my_list <- list(f1, f2)

# Name lists elements as variable labels
   names(my_list) <- c(var_labels1, 
                       var_labels2)

In the following output, how can I get rid of the first column name on both tables: values2labels(df$sex) and values2labels(df$agegroup) ?

$Sex
                                                                                                     
 | values2labels(df$sex) | Count | Valid percent | Percent | Responses, % | Cumulative responses, % |
 | --------------------- | ----- | ------------- | ------- | ------------ | ----------------------- |
 |                Female |     6 |            60 |      60 |           60 |                      60 |
 |                  Male |     4 |            40 |      40 |           40 |                     100 |
 |                #Total |    10 |           100 |     100 |          100 |                         |
 |                  <NA> |     0 |               |       0 |              |                         |

$`Age group`
                                                                                                          
 | values2labels(df$agegroup) | Count | Valid percent | Percent | Responses, % | Cumulative responses, % |
 | -------------------------- | ----- | ------------- | ------- | ------------ | ----------------------- |
 |                       1-29 |     3 |            30 |      30 |           30 |                      30 |
 |                      30-49 |     4 |            40 |      40 |           40 |                      70 |
 |                50 and more |     3 |            30 |      30 |           30 |                     100 |
 |                     #Total |    10 |           100 |     100 |          100 |                         |
 |                       <NA> |     0 |               |       0 |              |                         |

Solution

  • You need to set var_lab to empty string instead of NULL:

    library(expss)
    a = 1:2
    val_lab(a) = c("One" = 1, "Two" = 2)
    var_lab(a) = ""
    fre(values2labels(a))
     # |        | Count | Valid percent | Percent | Responses, % | Cumulative responses, % |
     # | ------ | ----- | ------------- | ------- | ------------ | ----------------------- |
     # |    One |     1 |            50 |      50 |           50 |                      50 |
     # |    Two |     1 |            50 |      50 |           50 |                     100 |
     # | #Total |     2 |           100 |     100 |          100 |                         |
     # |   <NA> |     0 |               |       0 |              |                         |