如何使用 expss 包中的 cro 函数删除交叉表 table 输出中未使用的值标签?

How to drop unused value labels in crosstabulations table outputs using cro function from expss package?

我正在使用带有天堂标签的数据框(导入数据集时变量已经有值标签)。我需要 运行 两个变量的许多交叉制表。我正在使用 expss 包中的 cro 函数,因为默认情况下显示值标签,并计算加权交叉表。

但是,我得到的输出表显示未使用的值标签。如何在不手动删除每个变量未使用的值标签的情况下删除未使用的标签? (顺便说一下:expss 包中的 fre 函数默认有这个参数:drop_unused_labels = TRUE,但是 cro 函数没有)

这是一个可重现的例子:

# Dataframe 
df <- data.frame(sex = c(1, 2, 99, 2, 1, 2, 2, 2, 1, 2),
                 agegroup= c(1, 2, 99, 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
                               97 Didn't know
                               98 Didn't respond
                               99 Abandoned survey")

val_lab(df$agegroup) <- make_labels("1 1-29
                                        2 30-49
                                        3 50 and more
                                       97 Didn't know
                                       98 Didn't respond
                                       99 Abandoned survey")

cro(df$sex, df$agegroup, weight = df$weight)

 |     |                  | Age group |       |             |             |                |                  |
 |     |                  |      1-29 | 30-49 | 50 and more | Didn't know | Didn't respond | Abandoned survey |
 | --- | ---------------- | --------- | ----- | ----------- | ----------- | -------------- | ---------------- |
 | Sex |             Male |       100 |   100 |          50 |             |                |                  |
 |     |           Female |       100 |   650 |          50 |             |                |                  |
 |     |      Didn't know |           |       |             |             |                |                  |
 |     |   Didn't respond |           |       |             |             |                |                  |
 |     | Abandoned survey |           |       |             |             |                |              400 |
 |     |     #Total cases |         2 |     5 |           2 |             |                |                1 |

我想删除名为 ‘Didn't know’‘Didn't respond’ 的列和行。

您可以使用drop_unused_labels函数删除不用的标签。

library(expss)
df1 <- drop_unused_labels(df)
cro(df1$sex, df1$agegroup, weight = df1$weight)
                                                                           
 |     |                  | Age group |       |             |                  |
 |     |                  |      1-29 | 30-49 | 50 and more | Abandoned survey |
 | --- | ---------------- | --------- | ----- | ----------- | ---------------- |
 | Sex |             Male |       100 |   100 |          50 |                  |
 |     |           Female |       100 |   650 |          50 |                  |
 |     | Abandoned survey |           |       |             |              400 |
 |     |     #Total cases |         2 |     5 |           2 |                1 |