1

我想用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\%

4

2 回答 2

1

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

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)

默认情况下 qwraps2 以 LaTeX 格式输出。要将默认值切换为降价设置,请设置以下选项

options(qwraps2_markup = "markdown")

更新:从 qwraps2 的 0.5.0 版开始,.data不再需要或不推荐使用代词。

stats_summary1 <-
  list("Sex (female)" =
         list("number (%)" = ~ qwraps2::n_perc(sex=="F", digits = 1)),
       "Age" =
         list("min" = ~ min(age_d, digits = 1),
              "max" = ~ max(age_d, digits = 1),
              "median (IQR)" = ~ qwraps2::median_iqr(age_d, digits = 1)),
       "Disease" =
         list("A" = ~ qwraps2::n_perc(d==1, digits = 1),
              "B" = ~ qwraps2::n_perc(d==2, digits = 1),
              "C" = ~ qwraps2::n_perc(d==3, digits = 1),
              "D" = ~ qwraps2::n_perc(d==4, digits = 1),
              "E" = ~ qwraps2::n_perc(d==5, digits = 1)),
       "Disease duration" =
         list("min" = ~ min(dis_dur, digits = 1),
              "max" = ~ max(dis_dur, digits = 1),
              "median (IQR)" = ~ qwraps2::median_iqr(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.6816968536004  |
#> |&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.5464100171812  |
#> |&nbsp;&nbsp; median (IQR) |31.0 (25.1, 35.8) |

这可以解决百分号上的正斜杠问题(LaTeX 需要转义)。确保您设置了results = "asis"块选项,以便表格在您的最终文档中很好地呈现。

至于省略的子标题,该qwraps2_summary_table对象是一个字符矩阵,其类属性已相应设置,并具有rgroups打印方法用于正确格式化输出的附加属性。

str(whole)
#>  'qwraps2_summary_table' chr [1:12, 1] "56 (56.0%)" "1" "77.6816968536004" ...
#>  - 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"
#>  - attr(*, "n")= int 100

reprex 包(v0.3.0)于 2020 年 9 月 14 日创建

devtools::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value                       
#>  version  R version 4.0.2 (2020-06-22)
#>  os       macOS Catalina 10.15.6      
#>  system   x86_64, darwin17.0          
#>  ui       X11                         
#>  language (EN)                        
#>  collate  en_US.UTF-8                 
#>  ctype    en_US.UTF-8                 
#>  tz       America/Denver              
#>  date     2020-09-14                  
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version date       lib source        
#>  assertthat    0.2.1   2019-03-21 [1] CRAN (R 4.0.0)
#>  backports     1.1.9   2020-08-24 [1] CRAN (R 4.0.2)
#>  callr         3.4.4   2020-09-07 [1] CRAN (R 4.0.2)
#>  cli           2.0.2   2020-02-28 [1] CRAN (R 4.0.0)
#>  crayon        1.3.4   2017-09-16 [1] CRAN (R 4.0.0)
#>  desc          1.2.0   2018-05-01 [1] CRAN (R 4.0.0)
#>  devtools      2.3.1   2020-07-21 [1] CRAN (R 4.0.2)
#>  digest        0.6.25  2020-02-23 [1] CRAN (R 4.0.0)
#>  ellipsis      0.3.1   2020-05-15 [1] CRAN (R 4.0.0)
#>  evaluate      0.14    2019-05-28 [1] CRAN (R 4.0.0)
#>  fansi         0.4.1   2020-01-08 [1] CRAN (R 4.0.0)
#>  fs            1.5.0   2020-07-31 [1] CRAN (R 4.0.2)
#>  glue          1.4.2   2020-08-27 [1] CRAN (R 4.0.2)
#>  highr         0.8     2019-03-20 [1] CRAN (R 4.0.0)
#>  htmltools     0.5.0   2020-06-16 [1] CRAN (R 4.0.0)
#>  knitr         1.29    2020-06-23 [1] CRAN (R 4.0.0)
#>  magrittr      1.5     2014-11-22 [1] CRAN (R 4.0.0)
#>  memoise       1.1.0   2017-04-21 [1] CRAN (R 4.0.0)
#>  pkgbuild      1.1.0   2020-07-13 [1] CRAN (R 4.0.2)
#>  pkgload       1.1.0   2020-05-29 [1] CRAN (R 4.0.0)
#>  prettyunits   1.1.1   2020-01-24 [1] CRAN (R 4.0.0)
#>  processx      3.4.4   2020-09-03 [1] CRAN (R 4.0.2)
#>  ps            1.3.4   2020-08-11 [1] CRAN (R 4.0.2)
#>  qwraps2     * 0.5.0   2020-08-31 [1] local         
#>  R6            2.4.1   2019-11-12 [1] CRAN (R 4.0.0)
#>  Rcpp          1.0.5   2020-07-06 [1] CRAN (R 4.0.0)
#>  remotes       2.2.0   2020-07-21 [1] CRAN (R 4.0.2)
#>  rlang         0.4.7   2020-07-09 [1] CRAN (R 4.0.2)
#>  rmarkdown     2.3     2020-06-18 [1] CRAN (R 4.0.0)
#>  rprojroot     1.3-2   2018-01-03 [1] CRAN (R 4.0.0)
#>  sessioninfo   1.1.1   2018-11-05 [1] CRAN (R 4.0.0)
#>  stringi       1.5.3   2020-09-09 [1] CRAN (R 4.0.2)
#>  stringr       1.4.0   2019-02-10 [1] CRAN (R 4.0.0)
#>  testthat      2.3.2   2020-03-02 [1] CRAN (R 4.0.0)
#>  usethis       1.6.1   2020-04-29 [1] CRAN (R 4.0.0)
#>  withr         2.2.0   2020-04-20 [1] CRAN (R 4.0.0)
#>  xfun          0.17    2020-09-09 [1] CRAN (R 4.0.2)
#>  yaml          2.2.1   2020-02-01 [1] CRAN (R 4.0.0)
#> 
#> [1] /Library/Frameworks/R.framework/Versions/4.0/Resources/library
于 2019-11-14T21:30:27.937 回答
0

data由于未提供您的示例,因此您的示例并不是真正可重现的。

对我有用的是以下内容:

  • qwraps2我没有使用来自 的汇总统计参数,而是使用carpenter包中的参数。bookdown这是因为在通过包呈现的PDF 文档中,qwraps2::mean_sd()会呈现表格中的“±”符号&plusmn;。相反,carpenter::stat_meanSD()避免使用“±”符号并使用括号代替 SD。paste()如果您更喜欢“±”符号,您可以使用另一种解决方法。

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

.

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)
于 2019-08-26T09:31:31.693 回答