[英]summary_table in qwraps2 with group_by in R
我正在試用 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
沒有做它應該做的事?
我盡我所能發布了輸出,抱歉並感謝您的理解。
在聲明變量時嘗試使用 .data$ 而不是 iris$ 。 我有同樣的問題,這解決了它。
group_by
調用似乎沒有做任何事情的原因是因為數據代詞.data
沒有在摘要定義中使用。 正如所寫的,匯總表是基於整個iris
數據集構建的,而不管任何分組或子集。 需要.data
代詞,以便summary_table
后面的tidyverse
工具使用正確的范圍。
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** | | | |
#> | min |4.3 |4.9 |4.9 |
#> | max |5.8 |7.0 |7.9 |
#> | mean (sd) |5.01 ± 0.35 |5.94 ± 0.52 |6.59 ± 0.64 |
#> |**Sepal Width** | | | |
#> | min |2.3 |2.0 |2.2 |
#> | median |3.4 |2.8 |3.0 |
#> | max |4.4 |3.4 |3.8 |
#> | mean (sd) |3.43 ± 0.38 |2.77 ± 0.31 |2.97 ± 0.32 |
#> |**Petal Length** | | | |
#> | min |1.0 |3.0 |4.5 |
#> | max |1.9 |5.1 |6.9 |
#> | mean (sd) |5.01 ± 0.35 |5.94 ± 0.52 |6.59 ± 0.64 |
#> |**Petal Width** | | | |
#> | min |0.1 |1.0 |1.4 |
#> | max |0.6 |1.8 |2.5 |
#> | mean (sd) |0.25 ± 0.11 |1.33 ± 0.20 |2.03 ± 0.27 |
#> |**Species** | | | |
#> | Setosa |50 (100) |0 (0) |0 (0) |
#> | Versicolor |0 (0) |50 (100) |0 (0) |
#> | Virginica |0 (0) |0 (0) |50 (100) |
由reprex 包(v0.3.0) 於 2020 年 3 月 1 日創建
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.