2

当使用 R 中的 expss 包创建表时,如何在嵌套变量中计算 row_percentages?在下面的示例中,我希望在每个时间段内计算行百分比。因此,我希望每个时间段(2015-2016 和 2017-2018)内的行百分比总和为 100%。但是,现在,百分比是在整个行上计算的。

library(expss)

data(mtcars)

mtcars$period <- "2015-2016"
mtcars <- rbind(mtcars, mtcars)
mtcars$period[33:64] <- "2017-2018"

mtcars = apply_labels(mtcars,
                      cyl = "Number of cylinders",
                      am = "Transmission",
                      am = c("Automatic" = 0,
                             "Manual"=1),
                      period = "Measurement period"
)

mtcars %>% 
  tab_cells(cyl) %>% 
  tab_cols(period %nest% am) %>% 
  tab_stat_rpct(label = "row_perc") %>% 
  tab_pivot()

reprex 包(v0.3.0)于 2019 年 9 月 28 日创建

 |                     |              |          | Measurement period |        |              |        |
 |                     |              |          |          2015-2016 |        |    2017-2018 |        |
 |                     |              |          |       Transmission |        | Transmission |        |
 |                     |              |          |          Automatic | Manual |    Automatic | Manual |
 | ------------------- | ------------ | -------- | ------------------ | ------ | ------------ | ------ |
 | Number of cylinders |            4 | row_perc |               13.6 |   36.4 |         13.6 |   36.4 |
 |                     |            6 | row_perc |               28.6 |   21.4 |         28.6 |   21.4 |
 |                     |            8 | row_perc |               42.9 |    7.1 |         42.9 |    7.1 |
 |                     | #Total cases | row_perc |               19.0 |   13.0 |         19.0 |   13.0 |
4

1 回答 1

1

我相信这就是你所追求的:

library(expss)

data(mtcars)

mtcars$period <- "2015-2016"
mtcars <- rbind(mtcars, mtcars)
mtcars$period[33:64] <- "2017-2018"

mtcars = apply_labels(mtcars,
                      cyl = "Number of cylinders",
                      am = "Transmission",
                      am = c("Automatic" = 0,
                             "Manual"=1),
                      period = "Measurement period"
)

mtcars %>% 
  tab_cells(cyl) %>% 
  tab_cols(period %nest% am ) %>% 
  tab_subgroup(period =="2015-2016") %>%
  tab_stat_rpct(label = "row_perc") %>%
  tab_subgroup(period =="2017-2018") %>%
  tab_stat_rpct(label = "row_perc") %>%
  tab_pivot(stat_position = "inside_rows")

注意使用tab_subgroup()哪个决定了我们要计算百分比的年度子组,以及stat_position = "inside_rows"决定我们要将计算出的输出放在最终表中的哪个位置。

输出:

 |                     |              |          | Measurement period |        |              |        |
 |                     |              |          |          2015-2016 |        |    2017-2018 |        |
 |                     |              |          |       Transmission |        | Transmission |        |
 |                     |              |          |          Automatic | Manual |    Automatic | Manual |
 | ------------------- | ------------ | -------- | ------------------ | ------ | ------------ | ------ |
 | Number of cylinders |            4 | row_perc |               27.3 |   72.7 |              |        |
 |                     |              |          |                    |        |         27.3 |   72.7 |
 |                     |            6 | row_perc |               57.1 |   42.9 |              |        |
 |                     |              |          |                    |        |         57.1 |   42.9 |
 |                     |            8 | row_perc |               85.7 |   14.3 |              |        |
 |                     |              |          |                    |        |         85.7 |   14.3 |
 |                     | #Total cases | row_perc |               19.0 |   13.0 |              |        |
 |                     |              |          |                    |        |         19.0 |   13.0 |

编辑:

%nest%如果我们不想要嵌套行(即行两倍),我们不需要。在这种情况下,代码的最后部分应该修改如下:

mtcars %>% 
  tab_cells(cyl) %>% 
  tab_cols(period,am) %>% 
  tab_subgroup(period ==c("2015-2016")) %>%
  tab_stat_rpct(label = "row_perc") %>%
  tab_subgroup(period ==c("2017-2018")) %>%
  tab_stat_rpct(label = "row_perc") %>%
  tab_pivot(stat_position = "outside_columns")

输出:

 |                     |              | Measurement period | Transmission |          |           |
 |                     |              |          2015-2016 |    Automatic |   Manual | Automatic |
 |                     |              |           row_perc |     row_perc | row_perc |  row_perc |
 | ------------------- | ------------ | ------------------ | ------------ | -------- | --------- |
 | Number of cylinders |            4 |                100 |         27.3 |     72.7 |      27.3 |
 |                     |            6 |                100 |         57.1 |     42.9 |      57.1 |
 |                     |            8 |                100 |         85.7 |     14.3 |      85.7 |
 |                     | #Total cases |                 32 |         19.0 |     13.0 |      19.0 |

          | Measurement period |
   Manual |          2017-2018 |
 row_perc |           row_perc |
 -------- | ------------------ |
     72.7 |                100 |
     42.9 |                100 |
     14.3 |                100 |
     13.0 |                 32 |
于 2019-09-28T14:17:31.377 回答