简体   繁体   English

如何在 R 中创建包含合并行的表

[英]How do I create a table with merged rows in R

Is there an R package that lets me produce tables such as this -是否有一个 R package 可以让我制作这样的表格 -

在此处输入图像描述

I am using excel to show my desired output.我正在使用 excel 来显示我想要的 output。 I'm using the mtcars dataset as an example.我以 mtcars 数据集为例。 Here I group Carb sizes into small, medium and large and show the count and mean mpg by those sizes.在这里,我将碳水化合物尺寸分为小、中和大,并按这些尺寸显示计数和平均 mpg。 I've been exploring the gt package but still struggling to produce something like this exactly.我一直在探索 gt package 但仍在努力生产这样的东西。 What I can see from this tutorial is that gt table can add summary lines below each group, however I would like to add the summaries (count, mean etc) as columns, and not rows below each group.我从教程中看到的是 gt 表可以在每个组下方添加汇总行,但是我想将汇总(计数、平均值等)添加为列,而不是每个组下方的行。

Lastly when I try to replicate the the tutorial in the link above using the mtcars dataset, I get the following error -最后,当我尝试使用 mtcars 数据集复制上面链接中的教程时,出现以下错误 -

Error in rlang::eval_tidy(expr = var_expr, data = data_tbl, env = emptyenv()) : object mean.mpg not found

Here is my code -这是我的代码 -

mtcars %>% 
  select(mpg, carb) %>% 
  mutate(size = case_when(
    carb %in% c(1, 2) ~ "Large",
    carb %in% c(4, 4) ~ "Medium",
    TRUE ~ "Small"
  )) %>% 
  group_by(carb, size) %>% 
  summarise(count = n(),
            mean.mpg = mean(mpg)) %>% 
  ungroup() %>% 
  gt(groupname_col = "size") %>% 
  summary_rows(groups = T, 
               columns = mean.mpg,
               fns = list(
                 average = "mean"
               ))

I appreciate any help.我很感激任何帮助。

If you put quotes around the new column name you don't get an error in R 4.04 running gt Version: 0.2.2, so try: columns = 'mean.mpg'.如果在新列名周围加上引号,则在运行 gt 版本:0.2.2 的 R 4.04 中不会出现错误,因此请尝试:columns = 'mean.mpg'。 From the @lag_rat_kid comment, and our joint experience, I'm guessing this infelicity was remedied in the more recent version of pkg:gt.从@lag_rat_kid 评论和我们的共同经验来看,我猜这种不合理之处在更新版本的 pkg:gt 中得到了纠正。

 #quit current session; restart
 install.packages("gt")
 library(gt); library(dplyr)
 # your code now succeeds

Although I modified it a bit to fix the error in the definition of the "medium" case尽管我对其进行了一些修改以修复“中等”案例定义中的错误

I used the Rstudio Export as PNG file and was agast at the size it created... 1.8 MB, so I then saved as a jpg file and then resaved as a PNG file before including it here).我使用了 Rstudio 导出为 PNG 文件,并且对它创建的大小感到震惊...... 1.8 MB,所以我将其保存为 jpg 文件,然后在将其包含在此处之前重新保存为 PNG 文件)。 I then adopted the practice of saving as a jpg file and then converting it with an image viewing program for upload here.然后我采用了另存为jpg文件的做法,然后用图像查看程序将其转换为上传到这里。

If you drop the 'card' column from the grouping, do some different labeling and don't include the summary rows (which you've already caluclated) you get much closer to your desires:如果您从分组中删除“卡片”列,做一些不同的标签并且不包括摘要行(您已经计算过),您会更接近您的愿望:

my_gt_tbl <- mtcars %>%
select(mpg, carb) %>%
mutate(size = case_when(
carb %in% c(1, 2) ~ "Large = 1,2",
carb %in% c(3, 4) ~ "Medium = 3,4",
TRUE ~ "Small = 6,8"
)) %>%
group_by(size) %>%
summarise(count = n(),
mean.mpg = mean(mpg))  %>%
gt(groupname_col = "size") 
my_gt_tbl

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM