R 编程:如何在 expss 包的 fre 函数的 table 输出中将变量标签作为第一列名称删除?

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

我正在对调查数据进行探索性分析,dataframe是一个haven labeled数据集,也就是说,每个变量都已经有了值标签和变量标签。

我想将频率table存储在一个列表中,然后将每个列表元素命名为变量标签。我正在使用 expss 包。问题是输出 tables 在第一列名称中包含此描述:values2labels(Df$var。如何从 table 中删除此描述?

可重现的例子:

# 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)

在以下输出中,如何去掉 table 的第一列名称:values2labels(df$sex)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 |              |                         |

您需要将 var_lab 设置为空字符串而不是 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 |              |                         |