如何拆分表格以使其在 R markdown 中并排显示? [英] How can I split a table so that it appears side by side in R markdown?

查看:136
本文介绍了如何拆分表格以使其在 R markdown 中并排显示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 R markdown 写一个文档,我想放一张桌子.问题是这张表只有两列,占了一整页,不是很漂亮.所以我的问题是:有没有办法将这个表一分为二,并将两个子表"放在一起?并排只有一个标题?

我使用 kable 命令并尝试了这个解决方案(

请注意,如果没有零行矩阵,两部分会更接近.要增加更多的间距,请将零行矩阵的额外副本放入清单.

I'm writing a document with R markdown and I'd like to put a table. The problem is that this table only has two columns and takes a full page, which is not very beautiful. So my question is : is there a way to split this table in two and to place the two "sub-tables" side by side with only one caption ?

I use the kable command and I tried this solution (How to split kable over multiple columns?) but I could not do the cbind() command.

Here's my code to create the table :

---
title: 
author: 
date: "`r format(Sys.time(), '%d %B, %Y')`"
output: pdf_document
indent: true
header-includes:
  - \usepackage{indentfirst}
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r, echo = FALSE}
kable(aerop2, format = "markdown")
```

where aerop2 is my data frame with a list of country names in column 1 and the number of airports in each of these countries in column 2.

I have a long two-column table which is a waste of space. I would like to split this table in two sub-tables and put these sub-tables side by side with a caption that includes both of them.

解决方案

This doesn't give a lot of flexibility in spacing, but here's one way to do it. I'm using the mtcars dataset as an example because I don't have aerop2.

---
output: pdf_document
indent: true
header-includes:
  - \usepackage{indentfirst}
  - \usepackage{booktabs}
---

```{r setup, include=FALSE}
library(knitr)
opts_chunk$set(echo = TRUE)
```

The data are in Table \ref{tab:tables}, which will float to the top of the page.

```{r echo = FALSE}
rows <- seq_len(nrow(mtcars) %/% 2)
kable(list(mtcars[rows,1:2],  
           matrix(numeric(), nrow=0, ncol=1),
           mtcars[-rows, 1:2]), 
      caption = "This is the caption.",
      label = "tables", format = "latex", booktabs = TRUE) 
```

This gives:

Note that without that zero-row matrix, the two parts are closer together. To increase the spacing more, put extra copies of the zero-row matrix into the list.

这篇关于如何拆分表格以使其在 R markdown 中并排显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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