html 中的 R gtsummary 3 方式 table
R gtsummary 3 way table in html
我需要用频率和行百分比绘制 3 种方式 HTML tables。
我喜欢 gtsummary 绘制 2 种方式 HTML tables 但不知道如何绘制 3 种方式。所以,我尝试了 xtab 但无法弄清楚如何在同一个 table 中添加频率和行百分比。请建议我如何用频率和行百分比绘制漂亮的 HTML table。
这是可重现的例子。
library(vcd)
head(Arthritis) # top few observations from the data
## ID Treatment Sex Age Improved
## 1 57 Treated Male 27 Some
## 2 46 Treated Male 29 None
## 3 77 Treated Male 30 None
## 4 17 Treated Male 32 Marked
## 5 36 Treated Male 46 Marked
## 6 23 Treated Male 58 Marked
mytable <- xtabs(~ Treatment+Sex+Improved, data=Arthritis)
mytable
## , , Improved = None
##
## Sex
## Treatment Female Male
## Placebo 19 10
## Treated 6 7
##
## , , Improved = Some
##
## Sex
## Treatment Female Male
## Placebo 7 0
## Treated 5 2
##
## , , Improved = Marked
##
## Sex
## Treatment Female Male
## Placebo 6 1
## Treated 16 5
我需要添加行百分比,还需要使其成为 HTML。
如果我能用 gtsummary 做到这一点,那是最好的。
但我愿意使用任何其他包,例如 kableExtra。
也许这样的东西对你有用:
library(gtsummary)
packageVersion("gtsummary")
#> [1] '1.4.0'
tbl <-
trial %>%
tbl_strata(
strata = trt,
~.x %>%
tbl_cross(stage, grade, percent = "row", margin = NULL),
.combine_with = "tbl_stack"
)
由 reprex package (v2.0.0)
创建于 2021-05-27
我需要用频率和行百分比绘制 3 种方式 HTML tables。 我喜欢 gtsummary 绘制 2 种方式 HTML tables 但不知道如何绘制 3 种方式。所以,我尝试了 xtab 但无法弄清楚如何在同一个 table 中添加频率和行百分比。请建议我如何用频率和行百分比绘制漂亮的 HTML table。 这是可重现的例子。
library(vcd)
head(Arthritis) # top few observations from the data
## ID Treatment Sex Age Improved
## 1 57 Treated Male 27 Some
## 2 46 Treated Male 29 None
## 3 77 Treated Male 30 None
## 4 17 Treated Male 32 Marked
## 5 36 Treated Male 46 Marked
## 6 23 Treated Male 58 Marked
mytable <- xtabs(~ Treatment+Sex+Improved, data=Arthritis)
mytable
## , , Improved = None
##
## Sex
## Treatment Female Male
## Placebo 19 10
## Treated 6 7
##
## , , Improved = Some
##
## Sex
## Treatment Female Male
## Placebo 7 0
## Treated 5 2
##
## , , Improved = Marked
##
## Sex
## Treatment Female Male
## Placebo 6 1
## Treated 16 5
我需要添加行百分比,还需要使其成为 HTML。 如果我能用 gtsummary 做到这一点,那是最好的。 但我愿意使用任何其他包,例如 kableExtra。
也许这样的东西对你有用:
library(gtsummary)
packageVersion("gtsummary")
#> [1] '1.4.0'
tbl <-
trial %>%
tbl_strata(
strata = trt,
~.x %>%
tbl_cross(stage, grade, percent = "row", margin = NULL),
.combine_with = "tbl_stack"
)