在R中嵌套变量内具有行百分比的expss表

时间:2019-09-28 13:13:45

标签: r expss

在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 package(v0.3.0)于2019-09-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 |

1 个答案:

答案 0 :(得分: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 |