如何使用summary_table R qwraps2创建子标题?

时间:2019-04-05 14:53:11

标签: r dplyr summary qwraps2

我想用summary_table生成一个汇总统计表。这需要有副标题。

我一直在关注这个例子: https://cran.r-project.org/web/packages/qwraps2/vignettes/summary-statistics.html

stats_summary1 <-
  list("Sex (female)" =
         list("number (%)" = ~ qwraps2::n_perc(.data$sex=="F", digits = 1)),
       "Age" =
         list("min" = ~ min(.data$age_d, digits = 1),
              "max" = ~ max(.data$age_d, digits = 1),
              "median (IQR)" = ~ qwraps2::median_iqr(.data$age_d, digits = 1)),
       "Disease" =
         list("A" = ~ qwraps2::n_perc(.data$d==1, digits = 1),
              "B" = ~ qwraps2::n_perc(.data$d==2, digits = 1),
              "C" = ~ qwraps2::n_perc(.data$d==3, digits = 1),
              "D" = ~ qwraps2::n_perc(.data$d==4, digits = 1),
              "E" = ~ qwraps2::n_perc(.data$d==5, digits = 1)),
       "Disease duration" =
         list("min" = ~ min(.data$dis_dur, digits = 1),
              "max" = ~ max(.data$dis_dur, digits = 1),
              "median (IQR)" = ~ qwraps2::median_iqr(.data$dis_dur, digits = 1)) 
    )
whole <- summary_table(df, stats_summary1)

表输出不包含子标题,例如“疾病”。例如,它还会产生带有正斜杠的百分比。 65 \%

2 个答案:

答案 0 :(得分:0)

由于没有提供您data,因此您的示例实际上是不可复制的。

对我有用的是:

  • 我使用qwraps2包中的摘要统计参数,而不使用carpenter中的摘要统计参数。这是因为在通过bookdown包呈现的PDF文档中,qwraps2::mean_sd()在表中将“±”符号呈现为&plusmn;。 相反,carpenter::stat_meanSD()避免使用“±”号,而在SD上使用方括号。即使您更喜欢“±”号,也可以使用paste()进行其他解决。

  • 在您的示例中,qwraps2忽略了诸如Sex (female)Age等顶级标题的事实非常令人讨厌。我的解决方法是通过kableExtra包手动重新引入这些标题。在您的示例中,将是这样的:

options(qwraps2_markup = "markdown")

summary_table(df, stats_summary1) %>%
kableExtra::pack_rows("Sex (female)", 1, 1) %>%
kableExtra::pack_rows("Age", 2, 4) %>% 
kableExtra::pack_rows("Disease", 5, 9) %>% 
kableExtra::pack_rows("Disease duration", 10, 12)

答案 1 :(得分:0)

首先,我将创建一个示例数据集以匹配提供的摘要

set.seed(42)
df <- data.frame(sex     = sample(c("M", "F"), size = 100, replace = TRUE),
                 age_d   = runif(100, 18, 80),
                 d       = sample(1:5, size = 100, replace = TRUE),
                 dis_dur = runif(100, 20, 43),
                 stringsAsFactors = FALSE)
str(df)
#> 'data.frame':    100 obs. of  4 variables:
#>  $ sex    : chr  "M" "M" "M" "M" ...
#>  $ age_d  : num  56.8 31.5 31.4 42.1 76.4 ...
#>  $ d      : int  2 1 4 3 5 2 4 2 3 2 ...
#>  $ dis_dur: num  33.3 21.7 23.8 37 30.9 ...

加载并附加qwraps2名称空间

library(qwraps2)

默认情况下,在LaTeX中输出qwraps2格式。将默认设置切换为 markdown设置以下选项

options(qwraps2_markup = "markdown")

stats_summary1 <-
  list("Sex (female)" =
         list("number (%)" = ~ qwraps2::n_perc(.data$sex=="F", digits = 1)),
       "Age" =
         list("min" = ~ min(.data$age_d, digits = 1),
              "max" = ~ max(.data$age_d, digits = 1),
              "median (IQR)" = ~ qwraps2::median_iqr(.data$age_d, digits = 1)),
       "Disease" =
         list("A" = ~ qwraps2::n_perc(.data$d==1, digits = 1),
              "B" = ~ qwraps2::n_perc(.data$d==2, digits = 1),
              "C" = ~ qwraps2::n_perc(.data$d==3, digits = 1),
              "D" = ~ qwraps2::n_perc(.data$d==4, digits = 1),
              "E" = ~ qwraps2::n_perc(.data$d==5, digits = 1)),
       "Disease duration" =
         list("min" = ~ min(.data$dis_dur, digits = 1),
              "max" = ~ max(.data$dis_dur, digits = 1),
              "median (IQR)" = ~ qwraps2::median_iqr(.data$dis_dur, digits = 1)) 
    )
whole <- summary_table(df, stats_summary1)
whole
#> 
#> 
#> |                          |df (N = 100)      |
#> |:-------------------------|:-----------------|
#> |**Sex (female)**          |&nbsp;&nbsp;      |
#> |&nbsp;&nbsp; number (%)   |56 (56.0%)        |
#> |**Age**                   |&nbsp;&nbsp;      |
#> |&nbsp;&nbsp; min          |1                 |
#> |&nbsp;&nbsp; max          |77.6817           |
#> |&nbsp;&nbsp; median (IQR) |52.8 (33.7, 64.8) |
#> |**Disease**               |&nbsp;&nbsp;      |
#> |&nbsp;&nbsp; A            |26 (26.0%)        |
#> |&nbsp;&nbsp; B            |20 (20.0%)        |
#> |&nbsp;&nbsp; C            |15 (15.0%)        |
#> |&nbsp;&nbsp; D            |21 (21.0%)        |
#> |&nbsp;&nbsp; E            |18 (18.0%)        |
#> |**Disease duration**      |&nbsp;&nbsp;      |
#> |&nbsp;&nbsp; min          |1                 |
#> |&nbsp;&nbsp; max          |42.54641          |
#> |&nbsp;&nbsp; median (IQR) |31.0 (25.1, 35.8) |

该问题解决了,百分号前加斜杠 (LaTeX需要转义)。确保您拥有results = "asis"块 选项集,以便该表将在最终文档中很好地呈现。

对于省略的副标题,qwraps2_summary_table对象是一个 具有相应的class属性的字符矩阵,并且具有 附加属性rgroups,由打印方法用来 正确格式化输出。

str(whole)
#>  'qwraps2_summary_table' chr [1:12, 1] "56 (56.0%)" "1" "77.6817" ...
#>  - attr(*, "dimnames")=List of 2
#>   ..$ : chr [1:12] "number (%)" "min" "max" "median (IQR)" ...
#>   ..$ : chr "df (N = 100)"
#>  - attr(*, "rgroups")= Named int [1:4] 1 3 5 3
#>   ..- attr(*, "names")= chr [1:4] "Sex (female)" "Age" "Disease" "Disease duration"

reprex package(v0.3.0)于2019-11-14创建