in `as_tibble()`: ! All columns in a tibble must be vectors. ✖ Column `depressio...

2025年10月20日 14:39 响应: 30.38秒 状态: success

🚨 错误信息

> #检查 depression 列的情况 > summary(dat$depression) Min. 1st Qu. Median Mean 3rd Qu. Max. 0.0000 0.0000 0.0000 0.2652 1.0000 1.0000 > table(dat$depression, useNA = "always") 0 1 <NA> 5452 1968 0 > dat$depression <- as.numeric(dat$depression) # 确保是数值型 > #或者删除包含NULL的行 > dat <- dat[!is.null(dat$depression), ] > #使用数据集中的实际变量名(包含标签) > actual_vars_dat <- c( + "chronic_num", + "age (年龄)", + "ragender (性别)", + "raeduc_c (教育)", + "marry (婚姻)", + "hrural (居住在农村或城市)", + "smokev (是否吸过烟)", + "disfunction_degree", + "trajectory_group", + "depression" + ) > # 创建标签映射(使用命名列表格式) > labels_list_dat <- list( + "chronic_num"="NCDs", + "age (年龄)" = "年龄", + "ragender (性别)" = "性别", + "raeduc_c (教育)" = "教育", + "marry (婚姻)" = "婚姻", + "hrural (居住在农村或城市)" = "居住地(农村)", + "smokev (是否吸过烟)" = "吸烟", + "disfunction_degree" = "功能障碍", + "trajectory_group" = "轨迹分组", + "depression" = "抑郁" + ) > baseline <- dat %>% + select(all_of(actual_vars_dat)) %>% + tbl_summary( + by = depression, # 分组变量 + statistic = list( + all_continuous() ~ "{mean}±({sd})", # 连续变量显示均值±标准差 + all_categorical() ~ "{n} ({p}%)" # 分类变量显示频数(百分比) + ), + label = labels_list_dat, # 使用标签映射 + digits = list( + all_continuous() ~ 2, # 连续变量保留2位小数 + all_categorical() ~ 1 # 分类变量百分比保留1位小数 + ), + missing = "no" # 不显示缺失值信息 + ) %>% + add_overall() %>% # 添加总体列 + add_p(pvalue_fun = ~ style_pvalue(.x, digits = 3)) %>% # 添加P值 + add_n( # 添加样本量信息 + statistic = "{N_nonmiss}", + col_label = "**N**", + footnote = TRUE + ) %>% + bold_labels() # 加粗变量标签 10 missing rows in the "depression" column have been removed. Error in `as_tibble()`: ! All columns in a tibble must be vectors. ✖ Column `depression` is NULL. Run `rlang::last_trace()` to see where the error occurred.

🤖 AI解决方案

Error in `as_tibble()`:
! All columns in a tibble must be vectors.
✖ Column `depression` is NULL.

  • 错误翻译
  •     错误在 `as_tibble()`:
        ! tibble 中的所有列都必须是向量。
        ✖ 列 `depression` 是 NULL。
        
  • 原因分析
  • tbl_summary 函数中,by 参数指定的变量 depression 包含了 NULL 值(即使你之前尝试删除 NULL 行,但可能在 tbl_summary 处理过程中又出现了)。tbl_summary 需要一个有效的向量作为分组变量,而 NULL 无法被识别为向量。
  • 解决方案
  • 在调用 tbl_summary 之前,彻底清除 depression 列中的 NULL 值,并确保它是一个有效的向量类型。

        # 确保 depression 列是可用的(没有 NULL)
        dat <- dat[!is.null(dat$depression), ] 
        # 如果 depression 仍然不是一个有效的向量(例如,它可能是列表或其它类型), 
        # 可以先将其转换为字符型,然后再转换为因子或数值型,以确保其结构
        # 这里的例子是先确保不是 NULL,如果之前已经处理了,这一步可能不是必须的
        
        # ... (你的 actual_vars_dat 和 labels_list_dat 定义) ...
    
        baseline <- dat %>%
          select(all_of(actual_vars_dat)) %>%  
          tbl_summary(
            by = depression,  # 分组变量
            statistic = list(
              all_continuous() ~ "{mean}±({sd})",  # 连续变量显示均值±标准差
              all_categorical() ~ "{n} ({p}%)"     # 分类变量显示频数(百分比)
            ),
            label = labels_list_dat,  # 使用标签映射
            digits = list(
              all_continuous() ~ 2,   # 连续变量保留2位小数
              all_categorical() ~ 1   # 分类变量百分比保留1位小数
            ),
            missing = "no"  # 不显示缺失值信息
          ) %>%
          add_overall() %>%  # 添加总体列
          add_p(pvalue_fun = ~ style_pvalue(.x, digits = 3)) %>%  # 添加P值
          add_n(  # 添加样本量信息
            statistic = "{N_nonmiss}",
            col_label = "**N**",
            footnote = TRUE
          ) %>%
          bold_labels()
        

  • 预防措施

1. 在进行数据汇总或分组分析前,务必检查所有涉及的分组变量是否包含 NANULL 值,并进行妥善处理。
2. 确保分类变量在转换时,不会产生意外的 NULL 或非向量类型。例如,使用 factor() 函数时,检查其levels。