Creating Tables for Rmd Files

Author
Affiliation

Alex Kaizer

University of Colorado-Anschutz Medical Campus

This page is part of the University of Colorado-Anschutz Medical Campus’ BIOS 6618 Recitation collection. To view other questions, you can view the BIOS 6618 Recitation collection page or use the search bar to look for keywords.

Creating Tables for Rmd Files

Like anything in R, there are many ways you could approach creating tables. Some of the approaches play more nicely with a specific type of output type (e.g., PDF versus HTML), but I wanted to provide some code examples so you can play around with different options to see what might work for your own work.

Markdown Style Tables

One approach to creating tables in Rmd is to use Markdown-specific syntax. The Tables Generator website includes online tools where you can enter your information and copy the code.

The code to create the table of length of stay data from Homework 6 might look something like:

| **Hospital (Treatment)** | **LOS (days)**                                    |
|:-------------------------|:--------------------------------------------------|
| Cauchy General (New)     | 3, 3, 4, 5, 5, 5, 6, 7, 7, 8, 9, 15               |
| Skellam Memorial (SOC)   | 6, 7, 7, 7, 8, 8, 8, 9, 9, 10, 10, 11, 13, 13, 15 |

which produces

Hospital (Treatment) LOS (days)
Cauchy General (New) 3, 3, 4, 5, 5, 5, 6, 7, 7, 8, 9, 15
Skellam Memorial (SOC) 6, 7, 7, 7, 8, 8, 8, 9, 9, 10, 10, 11, 13, 13, 15

A limitation of this table type is that you may have “wider” tables or columns than you want and it is hard to change.

Latex Style Tables

If you are creating a PDF file in Rmd, you can enter the Latex syntax directly:

\begin{table}[h]
\centering
\begin{tabular}{|l|l|}
\hline
\textbf{Hospital (Treatment)} & \textbf{LOS (days)} \\ \hline
Cauchy General (New)          & 3, 3, 4, 5, 5, 5, 6, 7, 7, 8, 9, 15  \\ \hline
Skellam Memorial (SOC)        & 6, 7, 7, 7, 8, 8, 8, 9, 9, 10, 10, 11, 13, 13, 15 \\ \hline
\end{tabular}
\end{table}

Since this document is an HTML, it won’t generate here. In some cases, you also need to add {=latex} to the code chunk.

Use kableExtra Package

We can create latex-esque table using various packages. One older package is known as kableExtra. We can create our table by first making a data frame to plot:

Code
library(kableExtra)

my_table <- data.frame(
  'Hospital (Treatment)' = c('Cauchy General (New)','Skellam Memorial (SOC)'),
  'LOS (Days)' = c('3, 3, 4, 5, 5, 5, 6, 7, 7, 8, 9, 15' , '6, 7, 7, 7, 8, 8, 8, 9, 9, 10, 10, 11, 13, 13, 15')
)

my_table # check what is in data frame
    Hospital..Treatment.                                        LOS..Days.
1   Cauchy General (New)               3, 3, 4, 5, 5, 5, 6, 7, 7, 8, 9, 15
2 Skellam Memorial (SOC) 6, 7, 7, 7, 8, 8, 8, 9, 9, 10, 10, 11, 13, 13, 15
Code
my_table %>%
  kbl(col.names=c('Hospital (Treatment)','LOS (Days)')) %>%
  kable_styling() %>%
  kable_styling(full_width = F)
Hospital (Treatment) LOS (Days)
Cauchy General (New) 3, 3, 4, 5, 5, 5, 6, 7, 7, 8, 9, 15
Skellam Memorial (SOC) 6, 7, 7, 7, 8, 8, 8, 9, 9, 10, 10, 11, 13, 13, 15

You can also change the styling very easily:

Code
my_table %>%
  kbl(col.names=c('Hospital (Treatment)','LOS (Days)')) %>%
  kable_classic_2() %>%
  kable_styling(full_width = F)
Hospital (Treatment) LOS (Days)
Cauchy General (New) 3, 3, 4, 5, 5, 5, 6, 7, 7, 8, 9, 15
Skellam Memorial (SOC) 6, 7, 7, 7, 8, 8, 8, 9, 9, 10, 10, 11, 13, 13, 15

kableExtra supports tables in HTML (with extra functionality, check the link out!), PDF, and Word.