You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在expss中对因子变量透视且保留标签?

问题描述

我需要报告多个因子变量的汇总表,希望生成节省空间的格式(无需重复每个变量的标签)。现有数据如下:

df<-
structure(list(answer3 = 
               structure(c(NA, 2L, NA, 1L, 2L), 
              levels = c("Strongly agree", 
              "Agree", "Neutral", "Disagree", "Strongly disagree"), label = "Confident in math class", class = c("labelled", 
              "factor")), answer4 = structure(c(NA, 2L, NA, 2L, 2L), levels = c("Strongly agree", 
              "Agree", "Neutral", "Disagree", "Strongly disagree"), label = "Strong belong scientific community", class = c("labelled", 
              "factor")), answer5 = structure(c(NA, 5L, NA, 2L, 3L), levels = c("Strongly agree", 
              "Agree", "Neutral", "Disagree", "Strongly disagree"), label = "Think myself a scientist", class = c("labelled", 
              "factor")), answer6 = structure(c(NA, 3L, NA, 1L, 3L), levels = c("Strongly agree", 
              "Agree", "Neutral", "Disagree", "Strongly disagree"), label = "Important to learn concepts", class = c("labelled", 
              "factor")), answer7 = structure(c(NA, 2L, NA, 3L, 2L), levels = c("Strongly agree", 
              "Agree", "Neutral", "Disagree", "Strongly disagree"), label = "Goal learn as much as I can", class = c("labelled", 
              "factor")), answer8 = structure(c(NA, 1L, NA, 3L, 2L), levels = c("Strongly agree", 
              "Agree", "Neutral", "Disagree", "Strongly disagree"), label = "Later changes depend on doing well", class = c("labelled", 
              "factor"))), row.names = c(NA, -5L), class = c("tbl_df", "tbl", "data.frame"))

尝试过两种方法,但都有问题:

  • 方法1:生成的表格会重复选项标签,浪费空间
df %>% 
 tab_cells(answer3,answer4,answer5,answer6,answer7,answer8) %>%  
 tab_rows() %>%  tab_stat_cpct() %>%  
 tab_pivot()  # 每个变量列都会重复显示"Strongly agree"等选项标签
  • 方法2:转长后丢失原变量的标签,表格只能显示answer3这类变量名
df %>% 
  to_long(cols=starts_with("answ"),value_factor = T) %>% # 变量标签被清除
    tab_cells(value) %>%
    tab_cols(variable) %>%
 tab_stat_cpct() %>%
tab_pivot() %>% tab_transpose() 

请问如何实现透视操作且不丢失变量标签?


解决方案

方法1:用tbl_summary直接生成紧凑汇总表

gtsummary的tbl_summary可以自动识别变量标签,默认生成的格式会把所有分类变量的选项合并到同一列,变量标签作为行名,完全符合节省空间的需求:

library(gtsummary)

df %>%
  tbl_summary(
    type = all_categorical() ~ "categorical",
    statistic = all_categorical() ~ "{p}%", # 指定显示百分比
    label = everything() ~ label # 直接调用原数据的变量标签
  ) %>%
  modify_spanning_header(all_categorical() ~ "**Response Options**") %>%
  modify_header(label ~ "**Question**")

方法2:转长时手动保留变量标签

如果一定要用to_long转长,可以先提取原数据的变量标签,再合并到转长后的数据集里:

library(gtsummary)
library(dplyr)

# 提取所有变量的标签
var_labels <- df %>%
  map_chr(~attr(.x, "label")) %>%
  tibble(variable = names(.), question_label = .)

# 转长并合并标签
df_long <- df %>%
  to_long(cols = starts_with("answ"), value_factor = TRUE) %>%
  left_join(var_labels, by = "variable") %>%
  mutate(variable = question_label) # 用标签替换原变量名

# 生成汇总表
df_long %>%
  tab_cells(value) %>%
  tab_cols(variable) %>%
  tab_stat_cpct() %>%
  tab_pivot() %>%
  tab_transpose() %>%
  modify_header(label ~ "**Question**", all_stat_cols() ~ "**{level}**")

内容的提问来源于stack exchange,提问作者Ben

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 20:52:05