如何在R markdown中对列进行分组? [英] How to group columns in R markdown?

查看:347
本文介绍了如何在R markdown中对列进行分组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用kable生成表并将输出保存为markdown.稍后,我将使用pandoc将markdown转换为pdf.对于我的表,我想将表列分为两组.似乎可以使用kableExtra"add_header_above"函数来实现,但是不幸的是,输出不能存储为markdown.有没有一种方法可以使用markdown对列进行分组?我在下面粘贴我的虚拟代码.感谢所有评论和建议.

I am trying to generate a table using kable and saving the output as a markdown. I am later using pandoc to convert markdown to pdf. For my table I would like to group my table columns into two groups. It seems that this could be achieved using the kableExtra "add_header_above" function, but unfortunately the output cannot be stored as markdown. Is there a way to group columns using markdown? I am pasting my dummy code below. Appreciate all comments and suggestions.

library(knitr)
library(kableExtra)
test <- head(mtcars, head=10)
test <- test[,c(1:6)]
kable(test, "markdown")

我曾尝试将以下各列进行分组,但似乎不起作用.

I have tried grouping the columns like below but it doesn't seem to work.

       |                  |     GroupA     |      GroupB     |
       |:-----------------|:---------------|:----------------|
       |                  |  mpg| cyl| disp|  hp| drat|    wt|
       |:-----------------|----:|---:|----:|---:|----:|-----:|
       |Mazda RX4         | 21.0|   6|  160| 110| 3.90| 2.620|
       |Mazda RX4 Wag     | 21.0|   6|  160| 110| 3.90| 2.875|
       |Datsun 710        | 22.8|   4|  108|  93| 3.85| 2.320|
       |Hornet 4 Drive    | 21.4|   6|  258| 110| 3.08| 3.215|
       |Hornet Sportabout | 18.7|   8|  360| 175| 3.15| 3.440|
       |Valiant           | 18.1|   6|  225| 105| 2.76| 3.460|

推荐答案

为什么要将输出存储在markdown中,然后将其存储在later using pandoc to convert markdown to pdf.中,为什么不一步一步完成?

Why would you store the output in markdown and then later using pandoc to convert markdown to pdf. Why not in one step?

library(knitr)
library(kableExtra)

test <- head(mtcars, head = 10)
test <- test[, c(1:6)]

p <- kable(test) %>%
  add_header_above(c(" " = 1, "Group 1" = 3, "Group 2" = 3))
p

根据您选择的输出,您将获得一个表格,其中包含"HTML"或"PDF"输出中的分组标题.

According to your chosen output, you would get a table with grouped headings in "HTML" or "PDF" output.

您可以使用p存储"HTML"输出,并使用pandoc二进制文件将其转换为markdown.但是二进制

You could use p to store the "HTML" output and use the pandoc binary to convert it to markdown. But the binary

pandoc本身对R一无所知.因此,除了很难从创建的输出中获取代码之外,还存在工具不匹配的问题.

pandoc itself knows nothing about R. So apart from the metaphysical difficulty of getting the code back from the output it created, you have a tool mismatch.

(引用自 Dirk Edelbuettel )

如果使用直接转换为PDF,则不需要p.

If you use the direct conversion to PDF you wouldn't need p.

这篇关于如何在R markdown中对列进行分组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆