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

gtsummary包tbl_summary函数statistic参数失效问题求助

问题

作为gtsummary包新手,尝试使用tbl_summary()的statistic参数自定义汇总统计量时持续报错,更换不同数据框后问题仍存在。

运行代码:

data %>% 
  tbl_summary(statistic = list(
      all_continuous() ~  "{mean} ({sd})",
      all_categorical() ~ "{n} ({P}%)"
      ))

得到错误:

Error in `mutate()`:
ℹ In argument: `tbl_stats = pmap(...)`.
Caused by error in `pmap()`:
ℹ In index: 1.
Caused by error in `value[[3L]]()`:
! There was an error assembling the summary statistics for 'age'
  with summary type 'categorical'.

There are 2 common sources for this error.
1. You have requested summary statistics meant for continuous
   variables for a variable being as summarized as categorical.
   To change the summary type to continuous, add the argument
  `type = list(age ~ 'continuous')`
2. One of the functions or statistics from the `statistic=` argument is not valid.
Run `rlang::last_trace()` to see where the error occurred.

数据预览及summary:

> summary(data)
      age            year              gender           residence        
 Min.   :17.00   Length:373         Length:373         Length:373        
 1st Qu.:18.00   Class :character   Class :character   Class :character  
 Median :18.00   Mode  :character   Mode  :character   Mode  :character  
 Mean   :18.67                                                            
 3rd Qu.:19.00                                                            
 Max.   :30.00                                                            
> data
# A tibble: 373 × 4
     age year     gender residence          
   <dbl> <chr>    <chr>  <chr>              
 1    18 1st year Male   Rural (Countryside)
 2    19 1st year Female Rural (Countryside)
 3    18 1st year Male   Urban (City)       
 4    18 1st year Female Urban (City)       
 5    18 1st year Female Urban (City)       
 6    18 1st year Female Urban (City)       
 7    17 1st year Female Rural (Countryside)
 8    19 2nd year Female Urban (City)       
 9    21 2nd year Male   Urban (City)       
10    18 1st year Male   Rural (Countryside)
# ℹ 363 more rows
# ℹ Use `print(n = ...)` to see more rows

错误提示将age识别为分类变量,但age实际是连续型;即使移除age,其他变量也会出现类似错误。

解决方法

核心原因

gtsummary包默认会将取值重复率高、唯一值数量少的连续变量自动判定为分类变量,导致你指定的连续型统计量(均值、标准差)无法匹配,从而报错。

具体解决方案

  1. 全局强制指定变量类型
    直接明确所有变量的类型,避免自动识别出错:

    data %>% 
      tbl_summary(
        type = list(
          all_continuous() ~ "continuous",
          all_categorical() ~ "categorical"
        ),
        statistic = list(
          all_continuous() ~ "{mean} ({sd})",
          all_categorical() ~ "{n} ({P}%)"
        )
      )
    
  2. 单独指定目标变量类型
    如果只想修正age的识别问题,单独指定即可:

    data %>% 
      tbl_summary(
        type = list(age ~ "continuous"),
        statistic = list(
          all_continuous() ~ "{mean} ({sd})",
          all_categorical() ~ "{n} ({P}%)"
        )
      )
    
  3. 额外检查要点

    • 确认statistic参数中的语法正确:{P}%是gtsummary支持的百分比格式,不要写成{p}%或其他错误形式;
    • 若其他变量仍报错,检查变量的实际类型(比如year是字符型,属于分类变量,确保统计量匹配)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.03 04:37:07