英文:
Change order of strata in table
问题
尝试更改分层中 cyl 组的顺序为 6 Cylinder、4 Cylinder、8 Cylinder。
library(tidyverse)
library(gtsummary)
tbl <-
mtcars %>%
select(am, cyl, mpg, hp) %>%
dplyr::mutate(
cyl = paste(cyl, "Cylinder"),
am = factor(am, labels = c("Automatic", "Manual"))
) %>%
tbl_strata(
strata = cyl,
~.x %>%
tbl_summary(
by = am,
type = where(is.numeric) ~ "continuous"
) %>%
modify_header(all_stat_cols() ~ "**{level}**")
)
英文:
Trying to change the order of the cyl groups within the strata to 6 Cylinder, 4 Cylinder, 8 Cylinder.
library(tidyverse)
library(gtsummary)
tbl <-
mtcars %>%
select(am, cyl, mpg, hp) %>%
dplyr::mutate(
cyl = paste(cyl, "Cylinder"),
am = factor(am, labels = c("Automatic", "Manual"))
) %>%
tbl_strata(
strata = cyl,
~.x %>%
tbl_summary(
by = am,
type = where(is.numeric) ~ "continuous"
) %>%
modify_header(all_stat_cols() ~ "**{level}**")
)
答案1
得分: 3
要更改分类变量的顺序,请将其转换为因子,并在levels
部分指定顺序,如下所示:
library(tidyverse)
library(gtsummary)
tbl <-
mtcars %>%
select(am, cyl, mpg, hp) %>%
dplyr::mutate(
cyl = paste(cyl, "Cylinder"),
am = factor(am, labels = c("Automatic", "Manual")),
cyl = factor(cyl, levels = c("8 Cylinder", "6 Cylinder", "4 Cylinder"))
) %>%
tbl_strata(
strata = cyl,
~.x %>%
tbl_summary(
by = am,
type = where(is.numeric) ~ "continuous"
) %>%
modify_header(all_stat_cols() ~ "**{level}**")
)
tbl
[![gtsummary table][1]][1]
[1]: https://i.stack.imgur.com/8XIS5.png
<details>
<summary>英文:</summary>
To change the order of a categorical variable, change it to a factor, and specify the order in the `levels` part as shown below:
library(tidyverse)
library(gtsummary)
tbl <-
mtcars %>%
select(am, cyl, mpg, hp) %>%
dplyr::mutate(
cyl = paste(cyl, "Cylinder"),
am = factor(am, labels = c("Automatic", "Manual")),
cyl = factor(cyl, levels = c("8 Cylinder", "6 Cylinder", "4 Cylinder"))
) %>%
tbl_strata(
strata = cyl,
~.x %>%
tbl_summary(
by = am,
type = where(is.numeric) ~ "continuous"
) %>%
modify_header(all_stat_cols() ~ "{level}")
)
tbl
[![gtsummary table][1]][1]
[1]: https://i.stack.imgur.com/8XIS5.png
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论