I am trying out the qwraps2 package and some of its functions. In particular I am interested in the summary_table tool for output. I am using the iris data set for practice, but I noticed something strange when using group_by in the summary_table:
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
The output i get is: output from the above code
This doesnt make sense, it says that the statistics on different variables across different flower species are the same, which they are not. I cross checked this by doing:
aggregate(iris[1:4], list(iris$Species), mean)
which shows that for example the mean of the different variables varies across species.
Why is dplyr::group_by not doing what it should?
i posted the output as best I could, sorry and thank you for the comprehension.
The reason the
group_bycall does not appear to do anything is because the data pronoun.datais not being used in the summary definition. As written, the summary table is constructed based on the wholeirisdata set, regardless of any grouping or subsetting. The.datapronoun is needed so that the thetidyversetools behindsummary_tableuse the correct scoping.Created on 2020-03-01 by the reprex package (v0.3.0)