Tables

Tables are a common way to summarize data in reports. Quarto can produce tables directly in Markdown or dynamically from R. Clear, readable tables make your documents more professional and easier to follow.

Text-Based Tables in Markdown

For small, static tables, you can write them directly in Markdown using pipe (|) syntax:

| Group | Mean | SD  |
|:------|-----:|:---:|
| A     | 5.2  | 1.1 |
| B     | 6.8  | 0.9 |
Group Mean SD
A 5.2 1.1
B 6.8 0.9

A --- row under the headers is required; colons control column alignment (left, right, or center).

Generating Tables from R with kable()

For tables built from your own data, knitr::kable() produces a clean, nicely formatted table.

Do: Run the code below in your own Quarto document.

data <- data.frame(
  Group = c("A", "B", "C"),
  Mean  = c(5.2, 6.8, 4.9),
  SD    = c(1.1, 0.9, 1.3)
)

data
  Group Mean  SD
1     A  5.2 1.1
2     B  6.8 0.9
3     C  4.9 1.3

See: The table gets printed as a raw R object, the data frame’s default output isn’t well suited for a report.

Predict: before looking below, predict how wrapping this same data frame in knitr::kable() will change its appearance in the rendered document.

library(knitr)
kable(data, caption = "Summary statistics by group")
Summary statistics by group
Group Mean SD
A 5.2 1.1
B 6.8 0.9
C 4.9 1.3

Explain: as predicted, kable() renders the data as a properly formatted table matching the surrounding document style, with a caption added via the caption argument, rather than R’s plain console-style printout.

For more control, such as striped rows, colored headers, and similar styling, the kableExtra package extends kable() output:

library(kableExtra)

kable(data, caption = "Summary statistics by group") |>
  kable_styling(bootstrap_options = c("striped", "hover", "condensed"), full_width = FALSE) |>
  row_spec(0, bold = TRUE, color = "white", background = "#4CAF50")
Summary statistics by group
Group Mean SD
A 5.2 1.1
B 6.8 0.9
C 4.9 1.3
External Resources
Practice Exercise

Create a data frame with two columns of your choice (at least 3 rows), then use kable() to display it with a caption.

library(knitr)

scores <- data.frame(
  Student = c("Alex", "Jordan", "Sam"),
  Score   = c(88, 92, 79)
)

kable(scores, caption = "Exam scores")
Exam scores
Student Score
Alex 88
Jordan 92
Sam 79
Back to top