微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

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

但是,我得到的输出显示未使用的值标签。如何删除未使用的标签,而无需手动删除每个变量的未使用的值标签? (顺便说一句:fre包中的expss函数认具有以下参数:drop_unused_labels = TRUE,但cro函数没有)

以下是可重现的示例:

# Dataframe 
df <- data.frame(sex = c(1,2,99,1,2),agegroup= c(1,3,1),weight = c(100,20,400,300,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 |

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。