0

我已经试用了看起来很有希望的 qwraps2 包。

但是,当我写了这样的表时:

set.seed(42)
library(magrittr)
library(qwraps2)
library(dplyr)


# define the markup language we are working in.
# options(qwraps2_markup = "latex") is also supported.
options(qwraps2_markup = "markdown")

data(mtcars)

mtcars2 <-
  dplyr::mutate(mtcars,
                cyl_factor = factor(cyl,
                                    levels = c(6, 4, 8),
                                    labels = paste(c(6, 4, 8), "cylinders")),
                cyl_character = paste(cyl, "cylinders"))

str(mtcars2)
with(mtcars2, table(cyl_factor, cyl_character))
with(mtcars2, all.equal(factor(cyl_character), cyl_factor))
mean_sd(mtcars2$mpg)
args(summary_table)

our_summary1 <-
  list("Miles Per Gallon" =
         list("min" = ~ min(.data$mpg),
              "max" = ~ max(.data$mpg),
              "mean (sd)" = ~ qwraps2::mean_sd(.data$mpg)),
       "Displacement" =
         list("min" = ~ min(.data$disp),
              "median" = ~ median(.data$disp),
              "max" = ~ max(.data$disp),
              "mean (sd)" = ~ qwraps2::mean_sd(.data$disp)),
       "Weight (1000 lbs)" =
         list("min" = ~ min(.data$wt),
              "max" = ~ max(.data$wt),
              "mean (sd)" = ~ qwraps2::mean_sd(.data$wt)),
       "Forward Gears" =
         list("Three" = ~ qwraps2::n_perc0(.data$gear == 3),
              "Four"  = ~ qwraps2::n_perc0(.data$gear == 4),
              "Five"  = ~ qwraps2::n_perc0(.data$gear == 5))
  )
whole <- summary_table(mtcars2 %>% dplyr::group_by(., cyl), our_summary1)
whole

在不允许我输出示例的服务器上,group_by 发生了一件我无法弄清楚的奇怪事情:

当我使用上面的 group_by 函数时,它会在列标题中执行 group_by (因此,在上面的示例中,我将数字 macing cyl = 4,6 和 8(n = 11,7 和 14)作为单独的列。(group_by来自 dplyr)

但是,下面各行的所有统计信息在每一列中都是相同的,并且是所有统计信息的总和(因此未分组)。这真的很奇怪,它不会引发错误,有人对如何进行有任何建议吗?

很抱歉,但我无法提供可重复的示例。

4

1 回答 1

1

我弄清楚我做错了什么。

list("Miles Per Gallon" =
         list("min" = ~ min(.data$mpg),
              "max" = ~ max(.data$mpg),
              "mean (sd)" = ~ qwraps2::mean_sd(.data$mpg))

而不是写"min" = ~ min(.data$cyl == 4)我写的"min" = ~ min(data$cyl == 4),因此每次都引用整个数据。当我解决这个问题时它可以工作: "min" = ~ min(cyl == 4)

于 2019-01-24T22:26:48.783 回答