2

如何在 R 中订购 qwraps2 summary_table 的输出列?运行表格第一列下方的代码时,会显示 6 个气缸的输出,然后是 4 个气缸,然后是 8 个气缸。

我尝试对数据进行排序,但这对输出表没有影响。

library(qwraps2)
mtcars2 <-
  dplyr::mutate(mtcars,
                cyl_factor = factor(cyl,
                                    levels = c(6, 4, 8),
                                    labels = paste(c(6, 4, 8), "cylinders")),
                cyl_character = paste(cyl, "cylinders"))


our_summary1 <-
  list("Miles Per Gallon" =
         list("min" = ~ min(.data$mpg),
              "max" = ~ max(.data$mpg),
              "mean (sd)" = ~ qwraps2::mean_sd(.data$mpg)),
       "Displacement" =
         list("min" = ~ min(.data$disp),
              "median" = ~ median(.data$disp),
              "max" = ~ max(.data$disp),
              "mean (sd)" = ~ qwraps2::mean_sd(.data$disp)),
       "Weight (1000 lbs)" =
         list("min" = ~ min(.data$wt),
              "max" = ~ max(.data$wt),
              "mean (sd)" = ~ qwraps2::mean_sd(.data$wt)),
       "Forward Gears" =
         list("Three" = ~ qwraps2::n_perc0(.data$gear == 3),
              "Four"  = ~ qwraps2::n_perc0(.data$gear == 4),
              "Five"  = ~ qwraps2::n_perc0(.data$gear == 5))
  )

by_cyl <- summary_table(dplyr::group_by(mtcars2, cyl_factor), our_summary1)
by_cyl

我想以递增的顺序(先是 4,然后是 6,然后是 8)来可视化它。

4

2 回答 2

2

唯一将mtcars2数据与原始数据 ( mtcars) 区分开来的是factorfor cylin的创建mtcars

当我们更改cyl我们“排序”的因子的因子水平时,结果将以所需的方式显示:

mtcars2 <-
        dplyr::mutate(mtcars,
                      cyl_factor = factor(cyl,
                                          levels = c(4, 6, 8),
                                          labels = paste(c(4, 6, 8), "cylinders")),
                      cyl_character = paste(cyl, "cylinders"))

by_cyl <- summary_table(dplyr::group_by(mtcars2, cyl_factor), our_summary1)
by_cyl
于 2019-08-23T15:38:38.443 回答
0

列的顺序默认为因子水平的顺序。

为了说明这一点,mtcars2数据框以圆柱列作为一个因素来呈现。小插图中提供的示例有意以非递增顺序对因子进行排序以演示行为。

更新:从 qwraps2 的 0.5.0 版开始,mtcars2数据集作为包的一部分导出,.data不再需要使用代词。

library(qwraps2)
options(qwraps2_markup = "markdown")
data(mtcars2, package = "qwraps2")


our_summary1 <-
  list("Miles Per Gallon" =
       list("min" = ~ min(mpg),
            "max" = ~ max(mpg),
            "mean (sd)" = ~ qwraps2::mean_sd(mpg)),
       "Displacement" =
         list("min" = ~ min(disp),
              "median" = ~ median(disp),
              "max" = ~ max(disp),
              "mean (sd)" = ~ qwraps2::mean_sd(disp)),
       "Weight (1000 lbs)" =
         list("min" = ~ min(wt),
              "max" = ~ max(wt),
              "mean (sd)" = ~ qwraps2::mean_sd(wt)),
       "Forward Gears" =
         list("Three" = ~ qwraps2::n_perc0(gear == 3),
              "Four"  = ~ qwraps2::n_perc0(gear == 4),
              "Five"  = ~ qwraps2::n_perc0(gear == 5))
  )

by_cyl <- summary_table(mtcars2, summaries = our_summary1, by = "cyl_factor")
by_cyl
#> 
#> 
#> |                       |6 cylinders (N = 7)   |4 cylinders (N = 11)  |8 cylinders (N = 14)  |
#> |:----------------------|:---------------------|:---------------------|:---------------------|
#> |**Miles Per Gallon**   |&nbsp;&nbsp;          |&nbsp;&nbsp;          |&nbsp;&nbsp;          |
#> |&nbsp;&nbsp; min       |17.8                  |21.4                  |10.4                  |
#> |&nbsp;&nbsp; max       |21.4                  |33.9                  |19.2                  |
#> |&nbsp;&nbsp; mean (sd) |19.74 &plusmn; 1.45   |26.66 &plusmn; 4.51   |15.10 &plusmn; 2.56   |
#> |**Displacement**       |&nbsp;&nbsp;          |&nbsp;&nbsp;          |&nbsp;&nbsp;          |
#> |&nbsp;&nbsp; min       |145                   |71.1                  |275.8                 |
#> |&nbsp;&nbsp; median    |167.6                 |108                   |350.5                 |
#> |&nbsp;&nbsp; max       |258                   |146.7                 |472                   |
#> |&nbsp;&nbsp; mean (sd) |183.31 &plusmn; 41.56 |105.14 &plusmn; 26.87 |353.10 &plusmn; 67.77 |
#> |**Weight (1000 lbs)**  |&nbsp;&nbsp;          |&nbsp;&nbsp;          |&nbsp;&nbsp;          |
#> |&nbsp;&nbsp; min       |2.62                  |1.513                 |3.17                  |
#> |&nbsp;&nbsp; max       |3.46                  |3.19                  |5.424                 |
#> |&nbsp;&nbsp; mean (sd) |3.12 &plusmn; 0.36    |2.29 &plusmn; 0.57    |4.00 &plusmn; 0.76    |
#> |**Forward Gears**      |&nbsp;&nbsp;          |&nbsp;&nbsp;          |&nbsp;&nbsp;          |
#> |&nbsp;&nbsp; Three     |2 (29)                |1 (9)                 |12 (86)               |
#> |&nbsp;&nbsp; Four      |4 (57)                |8 (73)                |0 (0)                 |
#> |&nbsp;&nbsp; Five      |1 (14)                |2 (18)                |2 (14)                |

如果我们构建同一个表,但使用cyl_characterto group by,我们将根据从字符到因子的默认强制以不同的顺序获取列。

summary_table(mtcars2, summaries = our_summary1, by = "cyl_character")
#> 
#> 
#> |                       |4 cylinders (N = 11)  |6 cylinders (N = 7)   |8 cylinders (N = 14)  |
#> |:----------------------|:---------------------|:---------------------|:---------------------|
#> |**Miles Per Gallon**   |&nbsp;&nbsp;          |&nbsp;&nbsp;          |&nbsp;&nbsp;          |
#> |&nbsp;&nbsp; min       |21.4                  |17.8                  |10.4                  |
#> |&nbsp;&nbsp; max       |33.9                  |21.4                  |19.2                  |
#> |&nbsp;&nbsp; mean (sd) |26.66 &plusmn; 4.51   |19.74 &plusmn; 1.45   |15.10 &plusmn; 2.56   |
#> |**Displacement**       |&nbsp;&nbsp;          |&nbsp;&nbsp;          |&nbsp;&nbsp;          |
#> |&nbsp;&nbsp; min       |71.1                  |145                   |275.8                 |
#> |&nbsp;&nbsp; median    |108                   |167.6                 |350.5                 |
#> |&nbsp;&nbsp; max       |146.7                 |258                   |472                   |
#> |&nbsp;&nbsp; mean (sd) |105.14 &plusmn; 26.87 |183.31 &plusmn; 41.56 |353.10 &plusmn; 67.77 |
#> |**Weight (1000 lbs)**  |&nbsp;&nbsp;          |&nbsp;&nbsp;          |&nbsp;&nbsp;          |
#> |&nbsp;&nbsp; min       |1.513                 |2.62                  |3.17                  |
#> |&nbsp;&nbsp; max       |3.19                  |3.46                  |5.424                 |
#> |&nbsp;&nbsp; mean (sd) |2.29 &plusmn; 0.57    |3.12 &plusmn; 0.36    |4.00 &plusmn; 0.76    |
#> |**Forward Gears**      |&nbsp;&nbsp;          |&nbsp;&nbsp;          |&nbsp;&nbsp;          |
#> |&nbsp;&nbsp; Three     |1 (9)                 |2 (29)                |12 (86)               |
#> |&nbsp;&nbsp; Four      |8 (73)                |4 (57)                |0 (0)                 |
#> |&nbsp;&nbsp; Five      |2 (18)                |1 (14)                |2 (14)                |

reprex 包(v0.3.0)于 2020 年 9 月 14 日创建

devtools::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value                       
#>  version  R version 4.0.2 (2020-06-22)
#>  os       macOS Catalina 10.15.6      
#>  system   x86_64, darwin17.0          
#>  ui       X11                         
#>  language (EN)                        
#>  collate  en_US.UTF-8                 
#>  ctype    en_US.UTF-8                 
#>  tz       America/Denver              
#>  date     2020-09-14                  
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version date       lib source        
#>  assertthat    0.2.1   2019-03-21 [1] CRAN (R 4.0.0)
#>  backports     1.1.9   2020-08-24 [1] CRAN (R 4.0.2)
#>  callr         3.4.4   2020-09-07 [1] CRAN (R 4.0.2)
#>  cli           2.0.2   2020-02-28 [1] CRAN (R 4.0.0)
#>  crayon        1.3.4   2017-09-16 [1] CRAN (R 4.0.0)
#>  desc          1.2.0   2018-05-01 [1] CRAN (R 4.0.0)
#>  devtools      2.3.1   2020-07-21 [1] CRAN (R 4.0.2)
#>  digest        0.6.25  2020-02-23 [1] CRAN (R 4.0.0)
#>  ellipsis      0.3.1   2020-05-15 [1] CRAN (R 4.0.0)
#>  evaluate      0.14    2019-05-28 [1] CRAN (R 4.0.0)
#>  fansi         0.4.1   2020-01-08 [1] CRAN (R 4.0.0)
#>  fs            1.5.0   2020-07-31 [1] CRAN (R 4.0.2)
#>  glue          1.4.2   2020-08-27 [1] CRAN (R 4.0.2)
#>  highr         0.8     2019-03-20 [1] CRAN (R 4.0.0)
#>  htmltools     0.5.0   2020-06-16 [1] CRAN (R 4.0.0)
#>  knitr         1.29    2020-06-23 [1] CRAN (R 4.0.0)
#>  magrittr      1.5     2014-11-22 [1] CRAN (R 4.0.0)
#>  memoise       1.1.0   2017-04-21 [1] CRAN (R 4.0.0)
#>  pkgbuild      1.1.0   2020-07-13 [1] CRAN (R 4.0.2)
#>  pkgload       1.1.0   2020-05-29 [1] CRAN (R 4.0.0)
#>  prettyunits   1.1.1   2020-01-24 [1] CRAN (R 4.0.0)
#>  processx      3.4.4   2020-09-03 [1] CRAN (R 4.0.2)
#>  ps            1.3.4   2020-08-11 [1] CRAN (R 4.0.2)
#>  qwraps2     * 0.5.0   2020-08-31 [1] local         
#>  R6            2.4.1   2019-11-12 [1] CRAN (R 4.0.0)
#>  Rcpp          1.0.5   2020-07-06 [1] CRAN (R 4.0.0)
#>  remotes       2.2.0   2020-07-21 [1] CRAN (R 4.0.2)
#>  rlang         0.4.7   2020-07-09 [1] CRAN (R 4.0.2)
#>  rmarkdown     2.3     2020-06-18 [1] CRAN (R 4.0.0)
#>  rprojroot     1.3-2   2018-01-03 [1] CRAN (R 4.0.0)
#>  sessioninfo   1.1.1   2018-11-05 [1] CRAN (R 4.0.0)
#>  stringi       1.5.3   2020-09-09 [1] CRAN (R 4.0.2)
#>  stringr       1.4.0   2019-02-10 [1] CRAN (R 4.0.0)
#>  testthat      2.3.2   2020-03-02 [1] CRAN (R 4.0.0)
#>  usethis       1.6.1   2020-04-29 [1] CRAN (R 4.0.0)
#>  withr         2.2.0   2020-04-20 [1] CRAN (R 4.0.0)
#>  xfun          0.17    2020-09-09 [1] CRAN (R 4.0.2)
#>  yaml          2.2.1   2020-02-01 [1] CRAN (R 4.0.0)
#> 
#> [1] /Library/Frameworks/R.framework/Versions/4.0/Resources/library
于 2020-03-02T16:16:35.660 回答