1

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

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

可重现的例子:

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

在以下输出中,我怎样才能摆脱两个表上的第一个列名: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 |              |                         |
4

1 回答 1

1

您需要将 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 |              |                         |
于 2020-09-22T23:22:51.837 回答