“Error: `x` must be a formula” with qwraps2 summary_table function

时光总嘲笑我的痴心妄想 提交于 2020-06-29 03:46:15

问题


I am trying to make a table with summary statistics that looks similar to this (but with min/max/median/mean values filled in):

                            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)

Or even like this: (with a width column too)

        Mass (g)                         Length (mm)      
Type    Min   Max   Median   Mean (SD)   Min   Max   Median   Mean (SD)
tct
tcx
tht
thx
tct

Here is an example of my data:

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),
)

This is the code I am working on, taken from 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

But I keep getting the error message: "Error: x must be a formula"

My end goal is to complete a table that resembles the above and export it as an excel file or word doc.

Any help would be appreciated.


回答1:


A minor correction when building the data set. I used dput() to get the structure of the object. This is a little more robust than using data.frame as the mode of each column is explicitly defined.

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))

loading the qwraps package and setting the markup language to "markdown". Without this setting the default markup is LaTeX.

library(qwraps2)
options(qwraps2_markup = "markdown")

The same summary as in the question posting, but with the missing comma added as noted in Ben's comment. the .data pronoun is needed so the scoping within summary_table is correct.

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)
              ))

The summary table appears to render as expected:

by_type <- summary_table(dplyr::group_by(dat, type), summary)
by_type
#> 
#> 
#> |                       |type: tct (N = 4)  |type: tcx (N = 2)  |type: tht (N = 2)  |type: thx (N = 2)  |
#> |:----------------------|:------------------|:------------------|:------------------|:------------------|
#> |**Mass (g)**           |&nbsp;&nbsp;       |&nbsp;&nbsp;       |&nbsp;&nbsp;       |&nbsp;&nbsp;       |
#> |&nbsp;&nbsp; Min       |0.02               |0.01               |0.01               |0.04               |
#> |&nbsp;&nbsp; Max       |0.07               |0.03               |0.04               |0.06               |
#> |&nbsp;&nbsp; Median    |0.030              |0.020              |0.025              |0.050              |
#> |&nbsp;&nbsp; Mean (SD) |0.04 &plusmn; 0.02 |0.02 &plusmn; 0.01 |0.03 &plusmn; 0.02 |0.05 &plusmn; 0.01 |
#> |**Length (mm)**        |&nbsp;&nbsp;       |&nbsp;&nbsp;       |&nbsp;&nbsp;       |&nbsp;&nbsp;       |
#> |&nbsp;&nbsp; Min       |3                  |6                  |5                  |4                  |
#> |&nbsp;&nbsp; Max       |5.25               |6.00               |6.00               |5.25               |
#> |&nbsp;&nbsp; Median    |8.0                |6.0                |7.0                |6.5                |
#> |&nbsp;&nbsp; Mean (SD) |5.38 &plusmn; 2.06 |6.00 &plusmn; 0.00 |6.00 &plusmn; 1.41 |5.25 &plusmn; 1.77 |
#> |**Width (mm)**         |&nbsp;&nbsp;       |&nbsp;&nbsp;       |&nbsp;&nbsp;       |&nbsp;&nbsp;       |
#> |&nbsp;&nbsp; Min       |1                  |3                  |3                  |2                  |
#> |&nbsp;&nbsp; Max       |3.5                |3.5                |3.5                |3.0                |
#> |&nbsp;&nbsp; Median    |6                  |4                  |4                  |4                  |
#> |&nbsp;&nbsp; Mean (SD) |3.50 &plusmn; 2.38 |3.50 &plusmn; 0.71 |3.50 &plusmn; 0.71 |3.00 &plusmn; 1.41 |

Created on 2020-03-01 by the reprex package (v0.3.0)



来源:https://stackoverflow.com/questions/59847819/error-x-must-be-a-formula-with-qwraps2-summary-table-function

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!