为啥我的 table_summary 在 r 中看起来很奇怪

Posted

技术标签:

【中文标题】为啥我的 table_summary 在 r 中看起来很奇怪【英文标题】:Why does my table_summary looks weird in r为什么我的 table_summary 在 r 中看起来很奇怪 【发布时间】:2020-07-26 11:44:44 【问题描述】:

完全是初学者,非常希望有人可以帮助我(: 编写了以下代码来创建按城市分组的统计汇总表,但我的表格看起来很奇怪(它看起来不像表格,而是显示了表格分隔符)。 我该怎么办?

load('myData.RData')

#install.packages('qwraps2')
library(qwraps2)
options(qwraps2_markup = 'markdown')
summary_statistics <-
  list(
    "Hobby(hours/week)" =
      list(
        "mean (sd)" = ~qwraps2::mean_sd(myData$hobby_hr_week, na_rm = TRUE),
        "min" = ~min(myData$hobby_hr_week, na.rm = TRUE),
        "max" = ~max(myData$hobby_hr_week, na.rm = TRUE)
      ),
    "Work(hours/week)" =
      list(
        "mean (sd)" = ~qwraps2::mean_sd(myData$work_hr_week, na_rm = TRUE),
        "min" = ~min(myData$work_hr_week, na.rm = TRUE),
        "max" = ~max(myData$work_hr_week, na.rm = TRUE)
      ),
    "Wellness" =
      list(
        "mean (sd)" = ~qwraps2::mean_sd(myData$wellness, na_rm = TRUE),
        "min" = ~min(myData$wellness, na.rm = TRUE),
        "max" = ~max(myData$wellness, na.rm = TRUE)
      ),
    
    "Happiness" =
      list(
        "Happiness" = ~qwraps2::n_perc(myData$RU_happy)

      )
  )



表格的样子:

|Summary Statistics Table for the Wellness Data Set |myData$city: Eilat (N = 25) |myData$city: jerusalem (N = 25) |myData$city: Metula (N = 25) |myData$city: TelAviv (N = 25) |
|:--------------------------------------------------|:---------------------------|:-------------------------------|:----------------------------|:-----------------------------|
|**Hobby(hours/week)**                              |&nbsp;&nbsp;                |&nbsp;&nbsp;                    |&nbsp;&nbsp;                 |&nbsp;&nbsp;                  |
|&nbsp;&nbsp; mean (sd)                             |15.54 &plusmn; 4.49         |15.54 &plusmn; 4.49             |15.54 &plusmn; 4.49          |15.54 &plusmn; 4.49           |
|&nbsp;&nbsp; min                                   |3.926501                    |3.926501                        |3.926501                     |3.926501                      |
|&nbsp;&nbsp; max                                   |27.00809                    |27.00809                        |27.00809                     |27.00809                      |
|**Work(hours/week)**                               |&nbsp;&nbsp;                |&nbsp;&nbsp;                    |&nbsp;&nbsp;                 |&nbsp;&nbsp;                  |
|&nbsp;&nbsp; mean (sd)                             |30.45 &plusmn; 19.51        |30.45 &plusmn; 19.51            |30.45 &plusmn; 19.51         |30.45 &plusmn; 19.51          |
|&nbsp;&nbsp; min                                   |1.945099                    |1.945099                        |1.945099                     |1.945099                      |
|&nbsp;&nbsp; max                                   |68.70944                    |68.70944                        |68.70944                     |68.70944                      |
|**Wellness**                                       |&nbsp;&nbsp;                |&nbsp;&nbsp;                    |&nbsp;&nbsp;                 |&nbsp;&nbsp;                  |
|&nbsp;&nbsp; mean (sd)                             |-56.11 &plusmn; 100.01      |-56.11 &plusmn; 100.01          |-56.11 &plusmn; 100.01       |-56.11 &plusmn; 100.01        |
|&nbsp;&nbsp; min                                   |-259.3496                   |-259.3496                       |-259.3496                    |-259.3496                     |
|&nbsp;&nbsp; max                                   |144.8053                    |144.8053                        |144.8053                     |144.8053                      |
|**Happiness**                                      |&nbsp;&nbsp;                |&nbsp;&nbsp;                    |&nbsp;&nbsp;                 |&nbsp;&nbsp;                  |
|&nbsp;&nbsp; Happiness                             |35 (35.00%)                 |35 (35.00%)                     |35 (35.00%)                  |35 (35.00%)                   |

【问题讨论】:

我觉得我在这里遗漏了一些东西......options(qwraps2_markup = 'markdown') 你是在要求它给你 Markdown 输出,是吗?然后它给你一个表格的 Markdown ......有什么问题? 嗨,tnx,问题是它显示不同的标记,而不是简单地将它们以图形方式应用于表格(&nbsp,&plusmn,** ...**) 为此,您需要实际渲染 Markdown,这与您要求 options(qwraps2_markup = 'latex') 没有什么不同——您必须编译 LaTeX 才能获得图形输出想。降价不是魔术。它必须被渲染。 【参考方案1】:

所以你必须实际渲染 Markdown 以获得你想要的图形输出。

我将你的 R 命令吐出的 Markdown 复制到一个新的 R Markdown 文件中。 由于 RStudio 的 knit 函数的工作原理,在转义美元符号($\$)后,当我 knit 时,我得到下表:

【讨论】:

谢谢!将我的脚本移动到 .Rmd 文件中的代码块并编织它 - 它工作! 太好了,很高兴它有帮助!不要忘记将答案标记为已接受as described in the Help Center 嗨,对不起,我也在使用 knitr,它运行良好,但我仍然得到类似于 op 的输出,有什么想法吗? @James hmm... 如果没有可重复的示例,可能很难说。我会发布一个新问题,显示您的降价代码,并列出您的操作系统、r 版本和 rstudio 版本,以及链接此问题【参考方案2】:

duckmayr 的回答解决了发布的问题。不过,我会 如果我没有提请注意构造中的一个重大错误,那就失职了 summary_statistics。每个汇总统计量定义为 myData$&lt;variable&gt; 表示分组功能看不到 组内的结果(城市名称)。查看表中的值 - 每一列都有相同的汇总统计信息。

我预计问题的一部分是在 qwraps2 的 0.5.0 版之前 建议/需要使用数据代词.data。然而,截至 发布 0.5.0 版(2020 年 9 月 1 日在 CRAN 上发布) summary_table 方法已被重构以避免使用数据 代词.data 和支持但不要求dplyr

例如,让我们构建和示例数据集:

set.seed(42)
myData <-
  data.frame(city          = gl(n = 4, k = 25, labels = c("Eilat", "Jerusalem", "Metula", "TelAviv")),
             hobby_hr_week = rpois(n = 100, lambda = 15.54),
             work_hr_week  = rpois(n = 100, lambda = 30.34),
             wellness      = rnorm(n = 100, mean = -56.11, sd = 100.01),
             RU_happy      = rbinom(n = 100, size = 1, p = 0.35))

加载并附加 qwraps2

library(qwraps2)
options(qwraps2_markup = "markdown")
packageVersion("qwraps2")
#> [1] '0.5.0'

定义summary_table 就像在原始问题帖子中一样:

summary_statistics <-
  list(
    "Hobby(hours/week)" =
      list(
           "mean (sd)" = ~ qwraps2::mean_sd(myData$hobby_hr_week, na_rm = TRUE),
           "min"       = ~ min(myData$hobby_hr_week, na.rm = TRUE),
           "max"       = ~ max(myData$hobby_hr_week, na.rm = TRUE)
           ),
       "Work(hours/week)" =
         list(
              "mean (sd)" = ~ qwraps2::mean_sd(myData$work_hr_week, na_rm = TRUE),
              "min"       = ~ min(myData$work_hr_week, na.rm = TRUE),
              "max"       = ~ max(myData$work_hr_week, na.rm = TRUE)
              ),
       "Wellness" =
         list(
              "mean (sd)" = ~ qwraps2::mean_sd(myData$wellness, na_rm = TRUE),
              "min"       = ~ min(myData$wellness, na.rm = TRUE),
              "max"       = ~ max(myData$wellness, na.rm = TRUE)
              ),

       "Happiness" =
         list(
              "Happiness" = ~qwraps2::n_perc(myData$RU_happy)

         )
  )

我们将得到一个表格,如原始问题中所述,但同样,值 不正确,它们是整个数据集的值,因为myData 每个变量名的前缀。

summary_table(myData, summary_statistics)
#> 
#> 
#> |                       |myData (N = 100)      |
#> |:----------------------|:---------------------|
#> |**Hobby(hours/week)**  |&nbsp;&nbsp;          |
#> |&nbsp;&nbsp; mean (sd) |15.76 &plusmn; 4.06   |
#> |&nbsp;&nbsp; min       |4                     |
#> |&nbsp;&nbsp; max       |27                    |
#> |**Work(hours/week)**   |&nbsp;&nbsp;          |
#> |&nbsp;&nbsp; mean (sd) |29.40 &plusmn; 5.73   |
#> |&nbsp;&nbsp; min       |19                    |
#> |&nbsp;&nbsp; max       |43                    |
#> |**Wellness**           |&nbsp;&nbsp;          |
#> |&nbsp;&nbsp; mean (sd) |-51.84 &plusmn; 97.33 |
#> |&nbsp;&nbsp; min       |-391.075901351521     |
#> |&nbsp;&nbsp; max       |209.058254168693      |
#> |**Happiness**          |&nbsp;&nbsp;          |
#> |&nbsp;&nbsp; Happiness |34 (34.00%)           |

summary_table(myData, summary_statistics, by = "city")
#> 
#> 
#> |                       |Eilat (N = 25)        |Jerusalem (N = 25)    |Metula (N = 25)       |TelAviv (N = 25)      |
#> |:----------------------|:---------------------|:---------------------|:---------------------|:---------------------|
#> |**Hobby(hours/week)**  |&nbsp;&nbsp;          |&nbsp;&nbsp;          |&nbsp;&nbsp;          |&nbsp;&nbsp;          |
#> |&nbsp;&nbsp; mean (sd) |15.76 &plusmn; 4.06   |15.76 &plusmn; 4.06   |15.76 &plusmn; 4.06   |15.76 &plusmn; 4.06   |
#> |&nbsp;&nbsp; min       |4                     |4                     |4                     |4                     |
#> |&nbsp;&nbsp; max       |27                    |27                    |27                    |27                    |
#> |**Work(hours/week)**   |&nbsp;&nbsp;          |&nbsp;&nbsp;          |&nbsp;&nbsp;          |&nbsp;&nbsp;          |
#> |&nbsp;&nbsp; mean (sd) |29.40 &plusmn; 5.73   |29.40 &plusmn; 5.73   |29.40 &plusmn; 5.73   |29.40 &plusmn; 5.73   |
#> |&nbsp;&nbsp; min       |19                    |19                    |19                    |19                    |
#> |&nbsp;&nbsp; max       |43                    |43                    |43                    |43                    |
#> |**Wellness**           |&nbsp;&nbsp;          |&nbsp;&nbsp;          |&nbsp;&nbsp;          |&nbsp;&nbsp;          |
#> |&nbsp;&nbsp; mean (sd) |-51.84 &plusmn; 97.33 |-51.84 &plusmn; 97.33 |-51.84 &plusmn; 97.33 |-51.84 &plusmn; 97.33 |
#> |&nbsp;&nbsp; min       |-391.075901351521     |-391.075901351521     |-391.075901351521     |-391.075901351521     |
#> |&nbsp;&nbsp; max       |209.058254168693      |209.058254168693      |209.058254168693      |209.058254168693      |
#> |**Happiness**          |&nbsp;&nbsp;          |&nbsp;&nbsp;          |&nbsp;&nbsp;          |&nbsp;&nbsp;          |
#> |&nbsp;&nbsp; Happiness |34 (34.00%)           |34 (34.00%)           |34 (34.00%)           |34 (34.00%)           |

要获得每个城市的正确汇总统计信息,请仅使用变量 名字。

summary_statistics <-
  list(
    "Hobby(hours/week)" =
      list(
           "mean (sd)" = ~ qwraps2::mean_sd(hobby_hr_week, na_rm = TRUE),
           "min"       = ~ min(hobby_hr_week, na.rm = TRUE),
           "max"       = ~ max(hobby_hr_week, na.rm = TRUE)
           ),
       "Work(hours/week)" =
         list(
              "mean (sd)" = ~ qwraps2::mean_sd(work_hr_week, na_rm = TRUE),
              "min"       = ~ min(work_hr_week, na.rm = TRUE),
              "max"       = ~ max(work_hr_week, na.rm = TRUE)
              ),
       "Wellness" =
         list(
              "mean (sd)" = ~ qwraps2::mean_sd(wellness, na_rm = TRUE),
              "min"       = ~ min(wellness, na.rm = TRUE),
              "max"       = ~ max(wellness, na.rm = TRUE)
              ),

       "Happiness" =
         list(
              "Happiness" = ~qwraps2::n_perc(RU_happy)

         )
  )

以及更新后的表格:

print(
      summary_table(myData, summary_statistics, by = "city"),
      rtitle = "Summary Statistics Table for the Wellness Data Set"
)
#> 
#> 
#> |Summary Statistics Table for the Wellness Data Set |Eilat (N = 25)        |Jerusalem (N = 25)     |Metula (N = 25)       |TelAviv (N = 25)      |
#> |:--------------------------------------------------|:---------------------|:----------------------|:---------------------|:---------------------|
#> |**Hobby(hours/week)**                              |&nbsp;&nbsp;          |&nbsp;&nbsp;           |&nbsp;&nbsp;          |&nbsp;&nbsp;          |
#> |&nbsp;&nbsp; mean (sd)                             |16.20 &plusmn; 4.47   |15.96 &plusmn; 4.59    |15.64 &plusmn; 3.68   |15.24 &plusmn; 3.60   |
#> |&nbsp;&nbsp; min                                   |8                     |4                      |10                    |11                    |
#> |&nbsp;&nbsp; max                                   |24                    |27                     |22                    |24                    |
#> |**Work(hours/week)**                               |&nbsp;&nbsp;          |&nbsp;&nbsp;           |&nbsp;&nbsp;          |&nbsp;&nbsp;          |
#> |&nbsp;&nbsp; mean (sd)                             |29.36 &plusmn; 6.75   |31.24 &plusmn; 5.06    |28.72 &plusmn; 5.78   |28.28 &plusmn; 5.05   |
#> |&nbsp;&nbsp; min                                   |20                    |20                     |19                    |21                    |
#> |&nbsp;&nbsp; max                                   |43                    |41                     |40                    |38                    |
#> |**Wellness**                                       |&nbsp;&nbsp;          |&nbsp;&nbsp;           |&nbsp;&nbsp;          |&nbsp;&nbsp;          |
#> |&nbsp;&nbsp; mean (sd)                             |-84.07 &plusmn; 94.75 |-51.45 &plusmn; 124.61 |-36.56 &plusmn; 77.73 |-35.28 &plusmn; 83.20 |
#> |&nbsp;&nbsp; min                                   |-232.587140717081     |-391.075901351521      |-184.147379739545     |-172.159781696125     |
#> |&nbsp;&nbsp; max                                   |120.55715927506       |209.058254168693       |113.344403784632      |113.643322269377      |
#> |**Happiness**                                      |&nbsp;&nbsp;          |&nbsp;&nbsp;           |&nbsp;&nbsp;          |&nbsp;&nbsp;          |
#> |&nbsp;&nbsp; Happiness                             |9 (36.00%)            |10 (40.00%)            |9 (36.00%)            |6 (24.00%)            |

由reprex package (v0.3.0) 于 2020-09-01 创建

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-01                  
#> 
#> ─ 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.3   2020-03-28 [1] CRAN (R 4.0.0)
#>  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.3   2020-07-05 [1] CRAN (R 4.0.0)
#>  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.4.6   2020-02-17 [1] CRAN (R 4.0.0)
#>  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.16    2020-07-24 [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

【讨论】:

问题:如何从未完成的print( summary_table(myData, summary_statistics, by = "city"), rtitle = "Summary Statistics Table for the Wellness Data Set" ) 到完成的表?即使在编织之后,我仍然收到一张与您未完成的桌子相似的桌子。 @james,我猜你需要设置代码块选项results = 'asis' @Peter...哈哈,太棒了。你是对的。非常感谢!

以上是关于为啥我的 table_summary 在 r 中看起来很奇怪的主要内容,如果未能解决你的问题,请参考以下文章

为啥我的课程架构在 fastAPi 和 swagger 中看起来很糟糕?

为啥我的模型在关系子查询中看不到 spatie 权限特征方法?

我在我的列表中看不到广告商(多点连接),为啥?

为啥我的随机数生成器在 C# 中看起来不是随机的?

为啥我的设计元素在所有浏览器中看起来都不同?更新[关闭]

为啥我的 React 应用程序只在新选项卡中看起来不错?