"Error: `x` must be a formula" with qwraps2 summary_table function
"Error: `x` must be a formula" with qwraps2 summary_table function
我正在尝试制作一个 table 摘要统计数据,看起来与此类似(但填充了 min/max/median/mean 值):
Type
Mass (g) tct tcx tht thx tct
Min
Max
Median
Mean (SD)
Length (mm)
Min
Max
Median
Mean (SD)
Width (mm)
Min
Max
Median
Mean (SD)
或者甚至像这样:(也有宽度列)
Mass (g) Length (mm)
Type Min Max Median Mean (SD) Min Max Median Mean (SD)
tct
tcx
tht
thx
tct
这是我的数据示例:
dat <- data.frame(
"id" = c(01,02,03,04,05,06,07,08,09,10),
"type" = c("tct", "tcx", "tht", "thx", "tct"),
"mass.g" = c(0.03,0.01,0.04,0.06,0.07,0.03,0.03,0.01,0.04,0.02),
"size.length" = c(8,6,5,6.5,5,5.5,6,7,4,3),
"size.width" = c(2,4,3,4,5,6,3,4,2,1),
)
这是我正在处理的代码,取自 here。
library(qwraps2)
summary <-
list("Mass (g)" =
list(
"Min" = ~ min(.data$mass.g),
"Max" = ~ max(.data$mass.g),
"Median" = ~ median(.data$mass.g)
"Mean (SD)" = ~ qwraps2::mean_sd(.data$mass.g)),
"Length (mm)" =
list(
"Min" = ~ min(.data$size.length),
"Max" = ~ median(.data$size.length),
"Median" = ~ max(.data$size.length),
"Mean (SD)" = ~ qwraps2::mean_sd(.data$size.length)),
"Width (mm)" =
list(
"Min" = ~ min(.data$size.width),
"Max" = ~ median(.data$size.width),
"Median" = ~ max(.data$size.width),
"Mean (SD)" = ~ qwraps2::mean_sd(.data$size.width)
))
summary
by_type <- summary_table(dplyr::group_by(dat, type), summary)
by_type
但我不断收到错误消息:"Error: x
must be a formula"
我的最终目标是完成一个类似于上面的 table 并将其导出为 excel 文件或 word 文档。
如有任何帮助,我们将不胜感激。
构建数据集时的一个小修正。我用 dput() 得到
对象的结构。这比使用 data.frame
更健壮
因为每一列的模式都是明确定义的。
dat <-
structure(list(id = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), type = structure(c(1L,
2L, 3L, 4L, 1L, 1L, 2L, 3L, 4L, 1L), class = "factor", .Label = c("tct",
"tcx", "tht", "thx")), mass.g = c(0.03, 0.01, 0.04, 0.06, 0.07,
0.03, 0.03, 0.01, 0.04, 0.02), size.length = c(8, 6, 5, 6.5,
5, 5.5, 6, 7, 4, 3), size.width = c(2, 4, 3, 4, 5, 6, 3, 4, 2,
1)), class = "data.frame", row.names = c(NA, -10L))
正在加载 qwraps2 包并将标记语言设置为“markdown”。
如果没有此设置,默认标记为 LaTeX。
library(qwraps2)
options(qwraps2_markup = "markdown")
与问题发布中的摘要相同,但添加了缺少的逗号
正如 Ben 的评论所述。
summary <-
list("Mass (g)" =
list(
"Min" = ~ min(mass.g),
"Max" = ~ max(mass.g),
"Median" = ~ median(mass.g),
"Mean (SD)" = ~ qwraps2::mean_sd(mass.g)),
"Length (mm)" =
list(
"Min" = ~ min(size.length),
"Max" = ~ median(size.length),
"Median" = ~ max(size.length),
"Mean (SD)" = ~ qwraps2::mean_sd(size.length)),
"Width (mm)" =
list(
"Min" = ~ min(size.width),
"Max" = ~ median(size.width),
"Median" = ~ max(size.width),
"Mean (SD)" = ~ qwraps2::mean_sd(size.width)
))
摘要 table 似乎按预期呈现:
by_type <- summary_table(x = dat, summaries = summary, by = "type")
by_type
#>
#>
#> | |tct (N = 4) |tcx (N = 2) |tht (N = 2) |thx (N = 2) |
#> |:----------------------|:------------------|:------------------|:------------------|:------------------|
#> |**Mass (g)** | | | | |
#> | Min |0.02 |0.01 |0.01 |0.04 |
#> | Max |0.07 |0.03 |0.04 |0.06 |
#> | Median |0.03 |0.02 |0.025 |0.05 |
#> | Mean (SD) |0.04 ± 0.02 |0.02 ± 0.01 |0.03 ± 0.02 |0.05 ± 0.01 |
#> |**Length (mm)** | | | | |
#> | Min |3 |6 |5 |4 |
#> | Max |5.25 |6 |6 |5.25 |
#> | Median |8 |6 |7 |6.5 |
#> | Mean (SD) |5.38 ± 2.06 |6.00 ± 0.00 |6.00 ± 1.41 |5.25 ± 1.77 |
#> |**Width (mm)** | | | | |
#> | Min |1 |3 |3 |2 |
#> | Max |3.5 |3.5 |3.5 |3 |
#> | Median |6 |4 |4 |4 |
#> | Mean (SD) |3.50 ± 2.38 |3.50 ± 0.71 |3.50 ± 0.71 |3.00 ± 1.41 |
由 reprex package (v0.3.0)
于 2020-09-15 创建
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-15
#>
#> ─ 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-09-14 [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
我正在尝试制作一个 table 摘要统计数据,看起来与此类似(但填充了 min/max/median/mean 值):
Type
Mass (g) tct tcx tht thx tct
Min
Max
Median
Mean (SD)
Length (mm)
Min
Max
Median
Mean (SD)
Width (mm)
Min
Max
Median
Mean (SD)
或者甚至像这样:(也有宽度列)
Mass (g) Length (mm)
Type Min Max Median Mean (SD) Min Max Median Mean (SD)
tct
tcx
tht
thx
tct
这是我的数据示例:
dat <- data.frame(
"id" = c(01,02,03,04,05,06,07,08,09,10),
"type" = c("tct", "tcx", "tht", "thx", "tct"),
"mass.g" = c(0.03,0.01,0.04,0.06,0.07,0.03,0.03,0.01,0.04,0.02),
"size.length" = c(8,6,5,6.5,5,5.5,6,7,4,3),
"size.width" = c(2,4,3,4,5,6,3,4,2,1),
)
这是我正在处理的代码,取自 here。
library(qwraps2)
summary <-
list("Mass (g)" =
list(
"Min" = ~ min(.data$mass.g),
"Max" = ~ max(.data$mass.g),
"Median" = ~ median(.data$mass.g)
"Mean (SD)" = ~ qwraps2::mean_sd(.data$mass.g)),
"Length (mm)" =
list(
"Min" = ~ min(.data$size.length),
"Max" = ~ median(.data$size.length),
"Median" = ~ max(.data$size.length),
"Mean (SD)" = ~ qwraps2::mean_sd(.data$size.length)),
"Width (mm)" =
list(
"Min" = ~ min(.data$size.width),
"Max" = ~ median(.data$size.width),
"Median" = ~ max(.data$size.width),
"Mean (SD)" = ~ qwraps2::mean_sd(.data$size.width)
))
summary
by_type <- summary_table(dplyr::group_by(dat, type), summary)
by_type
但我不断收到错误消息:"Error: x
must be a formula"
我的最终目标是完成一个类似于上面的 table 并将其导出为 excel 文件或 word 文档。
如有任何帮助,我们将不胜感激。
构建数据集时的一个小修正。我用 dput() 得到
对象的结构。这比使用 data.frame
更健壮
因为每一列的模式都是明确定义的。
dat <-
structure(list(id = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), type = structure(c(1L,
2L, 3L, 4L, 1L, 1L, 2L, 3L, 4L, 1L), class = "factor", .Label = c("tct",
"tcx", "tht", "thx")), mass.g = c(0.03, 0.01, 0.04, 0.06, 0.07,
0.03, 0.03, 0.01, 0.04, 0.02), size.length = c(8, 6, 5, 6.5,
5, 5.5, 6, 7, 4, 3), size.width = c(2, 4, 3, 4, 5, 6, 3, 4, 2,
1)), class = "data.frame", row.names = c(NA, -10L))
正在加载 qwraps2 包并将标记语言设置为“markdown”。 如果没有此设置,默认标记为 LaTeX。
library(qwraps2)
options(qwraps2_markup = "markdown")
与问题发布中的摘要相同,但添加了缺少的逗号 正如 Ben 的评论所述。
summary <-
list("Mass (g)" =
list(
"Min" = ~ min(mass.g),
"Max" = ~ max(mass.g),
"Median" = ~ median(mass.g),
"Mean (SD)" = ~ qwraps2::mean_sd(mass.g)),
"Length (mm)" =
list(
"Min" = ~ min(size.length),
"Max" = ~ median(size.length),
"Median" = ~ max(size.length),
"Mean (SD)" = ~ qwraps2::mean_sd(size.length)),
"Width (mm)" =
list(
"Min" = ~ min(size.width),
"Max" = ~ median(size.width),
"Median" = ~ max(size.width),
"Mean (SD)" = ~ qwraps2::mean_sd(size.width)
))
摘要 table 似乎按预期呈现:
by_type <- summary_table(x = dat, summaries = summary, by = "type")
by_type
#>
#>
#> | |tct (N = 4) |tcx (N = 2) |tht (N = 2) |thx (N = 2) |
#> |:----------------------|:------------------|:------------------|:------------------|:------------------|
#> |**Mass (g)** | | | | |
#> | Min |0.02 |0.01 |0.01 |0.04 |
#> | Max |0.07 |0.03 |0.04 |0.06 |
#> | Median |0.03 |0.02 |0.025 |0.05 |
#> | Mean (SD) |0.04 ± 0.02 |0.02 ± 0.01 |0.03 ± 0.02 |0.05 ± 0.01 |
#> |**Length (mm)** | | | | |
#> | Min |3 |6 |5 |4 |
#> | Max |5.25 |6 |6 |5.25 |
#> | Median |8 |6 |7 |6.5 |
#> | Mean (SD) |5.38 ± 2.06 |6.00 ± 0.00 |6.00 ± 1.41 |5.25 ± 1.77 |
#> |**Width (mm)** | | | | |
#> | Min |1 |3 |3 |2 |
#> | Max |3.5 |3.5 |3.5 |3 |
#> | Median |6 |4 |4 |4 |
#> | Mean (SD) |3.50 ± 2.38 |3.50 ± 0.71 |3.50 ± 0.71 |3.00 ± 1.41 |
由 reprex package (v0.3.0)
于 2020-09-15 创建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-15
#>
#> ─ 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-09-14 [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