Overview
This lesson shows you how to:
- Create Table 1 (descriptive statistics)
- Create Table 2 (regression results)
- Use Quarto chunk options to control code and output
- Write cleaner, more impactful reports
Setup
# install.packages(c("dplyr", "knitr", "broom"))
library(dplyr)
Warning: package 'dplyr' was built under R version 4.5.1
Attaching package: 'dplyr'
The following objects are masked from 'package:stats':
filter, lag
The following objects are masked from 'package:base':
intersect, setdiff, setequal, union
library(knitr)
library(broom)
Example Dataset
data(starwars)
df <- starwars %>%
select(sex, height, mass) %>%
filter(!is.na(sex))
Table 1: Descriptive Statistics
Simple Summary Table
table1 <- df %>%
group_by(sex) %>%
summarise(
n = n(),
mean_height = mean(height, na.rm = TRUE),
mean_mass = mean(mass, na.rm = TRUE)
)
kable(table1, caption = "Table 1. Descriptive Statistics by Sex")
Table 1. Descriptive Statistics by Sex
| female |
16 |
171.5714 |
54.68889 |
| hermaphroditic |
1 |
175.0000 |
1358.00000 |
| male |
60 |
179.1228 |
80.21905 |
| none |
6 |
131.2000 |
69.75000 |
Table 2: Regression Results
model <- lm(mass ~ height, data = df)
table2 <- tidy(model)
kable(table2, digits = 3, caption = "Table 2. Linear Regression Results")
Table 2. Linear Regression Results
| (Intercept) |
-11.248 |
114.345 |
-0.098 |
0.922 |
| height |
0.629 |
0.643 |
0.977 |
0.333 |
Quarto Tips: Controlling Code & Output
REMEMBER - The code goes INSIDE of the {r}. Make sure to use a comma after each new command.
1. Hide Code, Show Output
Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
66.0 167.0 180.0 174.6 191.0 264.0 6
The code: echo=FALSE
2. Show Code, Hide Output
The code: results='hide'
3. Hide Both Code and Output
The code: include=FALSE
4. Clean Tables (No Messages or Warnings)
kable(head(df), caption = "Clean Table Example")
Clean Table Example
| male |
172 |
77 |
| none |
167 |
75 |
| none |
96 |
32 |
| male |
202 |
136 |
| female |
150 |
49 |
| male |
178 |
120 |
The code: message=FALSE, warning=FALSE
Writing Tips for Quarto Reports
- Keep text short and clear
- Always interpret your results
- Use table captions to explain what the reader is seeing
- Avoid dumping raw output — format it into tables
- Use headings to organize your report
Example Interpretation
The results suggest that height is positively associated with mass, indicating that taller individuals tend to have higher body mass on average.
Key Takeaways
- Use
kable() for clean, simple tables
- Always structure Table 1 (descriptive) and Table 2 (model results)
- Control what your audience sees using chunk options
- Focus on interpretation, not just output