小编Beg*_*dev的帖子

是否可以在 R 中使用 tbl_summary 制作一个包含两个 group by 的表?

我正在使用 mtcars 数据库,并且使用 tbl_summary 函数。我想做的是有两个分组依据,首先是变速箱类型,然后是气缸数量,所以我总共有六列分组依据加上整个列,到目前为止我只有能够仅使用一个变量进行分组。

这是我的代码:

mtcars2 <- within(mtcars, {
  vs <- factor(vs, labels = c("V", "S"))
  am <- factor(am, labels = c("Automatic", "Manual"))
  cyl  <- ordered(cyl)
  gear <- ordered(gear)
  carb <- ordered(carb)
})

mtcars2 %>%
  tbl_summary(
    by = cyl,
    type = all_continuous() ~ "continuous2",
    statistic = list(all_continuous() ~ c("{mean} ({sd})",
                                          "{min}, {max}",
                                          "{skew}"),
                     all_categorical() ~ "{n} / {N} ({p}%)"),
    digits = all_continuous() ~ 1,
    label = list(mpg ~ "Miles/ Gallon", disp ~ "Displacement (cu.in.)", hp ~ "Gross Horsepower", drat …
Run Code Online (Sandbox Code Playgroud)

r gtsummary

8
推荐指数
1
解决办法
6136
查看次数

标签 统计

gtsummary ×1

r ×1