0

我正在尝试 qwraps2 包及其一些功能。特别是我对用于输出的 summary_table 工具感兴趣。我正在使用 iris 数据集进行练习,但在 summary_table 中使用 group_by 时发现了一些奇怪的现象:

library(datasets)
data("iris")
options(qwraps2_markup = "markdown")
our_summary1 <-
  list("Sepal Length" =
       list("min" = ~ min(iris$Sepal.Length),
            "max" = ~ max(iris$Sepal.Length),
            "mean (sd)" = ~ qwraps2::mean_sd(iris$Sepal.Length)),
       "Sepal Width" =
       list("min" = ~ min(iris$Sepal.Width),
            "median" = ~ median(iris$Sepal.Width),
            "max" = ~ max(iris$Sepal.Width),
            "mean (sd)" = ~ qwraps2::mean_sd(iris$Sepal.Width)),
       "Petal Length" =
       list("min" = ~ min(iris$Petal.Length),
            "max" = ~ max(iris$Petal.Length),
            "mean (sd)" = ~ qwraps2::mean_sd(iris$Sepal.Length)),
       "Petal Width" =
       list("min" = ~ min(iris$Petal.Width),
            "max" = ~ max(iris$Petal.Width),
            "mean (sd)" = ~ qwraps2::mean_sd(iris$Petal.Width)),
        "Species" =
       list("Setosa" = ~ qwraps2::n_perc0(iris$Species == "setosa"),
            "Versicolor"  = ~ qwraps2::n_perc0(iris$Species == "versicolor"),
            "Virginica"  = ~ qwraps2::n_perc0(iris$Species == "virginica"))
       )

bytype <- qwraps2::summary_table(dplyr::group_by(iris,Species),our_summary1)
bytype

我得到的输出是: 上述代码的输出

这没有意义,它说不同花种的不同变量的统计数据是相同的,但事实并非如此。我通过这样做交叉检查了这一点:

aggregate(iris[1:4], list(iris$Species), mean)

这表明,例如,不同变量的平均值因物种而异。

为什么dplyr::group_by不做应该做的事?

我尽我所能发布了输出,对不起,谢谢你的理解。

4

2 回答 2

0

声明变量时尝试使用 .data$ 而不是 iris$。我有同样的问题,这解决了它。

于 2020-01-29T04:08:49.737 回答
0

group_by调用似乎没有做任何事情的原因是数据代词 未.data在摘要定义中使用。如所写,汇总表是基于整个iris数据集构建的,而不考虑任何分组或子集。.data需要代词,以便tidyverse后面的工具使用summary_table正确的范围。

library(datasets)
library(qwraps2)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

data("iris")
options(qwraps2_markup = "markdown")

our_summary1 <-
  list("Sepal Length" =
       list("min" = ~ min(.data$Sepal.Length),
            "max" = ~ max(.data$Sepal.Length),
            "mean (sd)" = ~ qwraps2::mean_sd(.data$Sepal.Length)),
       "Sepal Width" =
       list("min" = ~ min(.data$Sepal.Width),
            "median" = ~ median(.data$Sepal.Width),
            "max" = ~ max(.data$Sepal.Width),
            "mean (sd)" = ~ qwraps2::mean_sd(.data$Sepal.Width)),
       "Petal Length" =
       list("min" = ~ min(.data$Petal.Length),
            "max" = ~ max(.data$Petal.Length),
            "mean (sd)" = ~ qwraps2::mean_sd(.data$Sepal.Length)),
       "Petal Width" =
       list("min" = ~ min(.data$Petal.Width),
            "max" = ~ max(.data$Petal.Width),
            "mean (sd)" = ~ qwraps2::mean_sd(.data$Petal.Width)),
        "Species" =
       list("Setosa" = ~ qwraps2::n_perc0(.data$Species == "setosa"),
            "Versicolor"  = ~ qwraps2::n_perc0(.data$Species == "versicolor"),
            "Virginica"  = ~ qwraps2::n_perc0(.data$Species == "virginica"))
       )


bytype <- qwraps2::summary_table(dplyr::group_by(iris,Species),our_summary1)
bytype
#> 
#> 
#> |                        |Species: setosa (N = 50) |Species: versicolor (N = 50) |Species: virginica (N = 50) |
#> |:-----------------------|:------------------------|:----------------------------|:---------------------------|
#> |**Sepal Length**        |&nbsp;&nbsp;             |&nbsp;&nbsp;                 |&nbsp;&nbsp;                |
#> |&nbsp;&nbsp; min        |4.3                      |4.9                          |4.9                         |
#> |&nbsp;&nbsp; max        |5.8                      |7.0                          |7.9                         |
#> |&nbsp;&nbsp; mean (sd)  |5.01 &plusmn; 0.35       |5.94 &plusmn; 0.52           |6.59 &plusmn; 0.64          |
#> |**Sepal Width**         |&nbsp;&nbsp;             |&nbsp;&nbsp;                 |&nbsp;&nbsp;                |
#> |&nbsp;&nbsp; min        |2.3                      |2.0                          |2.2                         |
#> |&nbsp;&nbsp; median     |3.4                      |2.8                          |3.0                         |
#> |&nbsp;&nbsp; max        |4.4                      |3.4                          |3.8                         |
#> |&nbsp;&nbsp; mean (sd)  |3.43 &plusmn; 0.38       |2.77 &plusmn; 0.31           |2.97 &plusmn; 0.32          |
#> |**Petal Length**        |&nbsp;&nbsp;             |&nbsp;&nbsp;                 |&nbsp;&nbsp;                |
#> |&nbsp;&nbsp; min        |1.0                      |3.0                          |4.5                         |
#> |&nbsp;&nbsp; max        |1.9                      |5.1                          |6.9                         |
#> |&nbsp;&nbsp; mean (sd)  |5.01 &plusmn; 0.35       |5.94 &plusmn; 0.52           |6.59 &plusmn; 0.64          |
#> |**Petal Width**         |&nbsp;&nbsp;             |&nbsp;&nbsp;                 |&nbsp;&nbsp;                |
#> |&nbsp;&nbsp; min        |0.1                      |1.0                          |1.4                         |
#> |&nbsp;&nbsp; max        |0.6                      |1.8                          |2.5                         |
#> |&nbsp;&nbsp; mean (sd)  |0.25 &plusmn; 0.11       |1.33 &plusmn; 0.20           |2.03 &plusmn; 0.27          |
#> |**Species**             |&nbsp;&nbsp;             |&nbsp;&nbsp;                 |&nbsp;&nbsp;                |
#> |&nbsp;&nbsp; Setosa     |50 (100)                 |0 (0)                        |0 (0)                       |
#> |&nbsp;&nbsp; Versicolor |0 (0)                    |50 (100)                     |0 (0)                       |
#> |&nbsp;&nbsp; Virginica  |0 (0)                    |0 (0)                        |50 (100)                    |

reprex 包(v0.3.0)于 2020 年 3 月 1 日创建

在此处输入图像描述

于 2020-03-02T00:40:46.240 回答