The following code produces 2 tables on top of each other. How would I set it to have them aligned side by side, e.g. 3 to a row?
--- title: "sample" output: pdf_document --- ```{r global_options, R.options=knitr::opts_chunk$set(warning=FALSE, message=FALSE)} ``` ```{r sample, echo=FALSE, results='asis'} library(knitr) t1 <- head(mtcars)[1:3] t2 <- head(mtcars)[4:6] print(kable(t1)) print(kable(t2)) ```
Output looks like this:
You only need to float the first table. Then you simply add margin-right: 50px; (for example) to it and the second one will be next to it, 50px away. If you want to center them both, put them in a div with some width and add margin: 0 auto; .
Just put two data frames in a list, e.g.
t1 <- head(mtcars)[1:3] t2 <- head(mtcars)[4:6] knitr::kable(list(t1, t2))
Note this requires knitr >= 1.13.
I used this Align two data.frames next to each other with knitr? which shows how to do it in html
and this https://tex.stackexchange.com/questions/2832/how-can-i-have-two-tables-side-by-side to align 2 Latex tables next to each other. It seems that you cannot freely adjust the lines of the table as you can do it with xtable
(does anybody know more about this?). With format = Latex
you get a horizontal line after each row. But the documentation shows two examples for other formats. One using the longtable
package (additional argument: longtable = TRUE
) and the other using the booktabs
package (booktabs = TRUE
).
--- title: "sample" output: pdf_document header-includes: - \usepackage{booktabs} --- ```{r global_options, R.options=knitr::opts_chunk$set(warning=FALSE, message=FALSE)} ``` ```{r sample, echo=FALSE, results='asis'} library(knitr) library(xtable) t1 <- kable(head(mtcars)[1:3], format = "latex", booktabs = TRUE) t2 <- kable(head(mtcars)[4:6], format = "latex", booktabs = TRUE) cat(c("\\begin{table}[!htb] \\begin{minipage}{.5\\linewidth} \\caption{} \\centering", t1, "\\end{minipage}% \\begin{minipage}{.5\\linewidth} \\centering \\caption{}", t2, "\\end{minipage} \\end{table}" )) ```
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With