Skip to content
R for the Rest of Us Logo

R in 3 Months (Spring 2025)

Tables

Transcript

Click on the transcript to go to that point in the video. Please note that transcripts are auto generated and may contain minor inaccuracies.

Your Turn

  1. Copy the starter code below into RStudio and use it to make a table using flextable or gt.

  2. Work on adjusting your column names, column width, number formatting, and anything else you might want to try out.

---
title: "Oregon Department of Education Diversity Report"
format: html
---

```{r}
library(tidyverse)
```

![](ode-logo.jpg)

This is a report for the [Oregon Department of Education](https://www.oregon.gov/ode/pages/default.aspx) on diversity in Oregon school districts.

> The Oregon Department of Education fosters equity and excellence for every learner through collaboration with educators, partners, and communities.^[https://www.oregon.gov/ode/about-us/Pages/default.aspx]



```{r}
# The code below will bring in the enrollment by race/ethnicity data
# It will then filter to only include one school district (Baker SD 5J)
# It then use select() to drop a few columns that we don't need

baker_enrollment_by_race_ethnicity <-
  read_rds("https://github.com/rfortherestofus/going-deeper-v2/raw/main/data/enrollment_by_race_ethnicity.rds") |> 
  select(-district_institution_id)  |> 
  select(year, district, everything()) |> 
  mutate(year = case_when(
    year == "School 2021-22" ~ "2021-2022",
    year == "School 2022-23" ~ "2022-2023",
  )) |> 
  filter(district == "Baker SD 5J") |> 
  select(-c(district, number_of_students)) 
```

```{r}
# Use pivot_wider() to make the race/ethnicity groups into the columns (keep year in rows)
# Then create a table with the flextable or gt package
# Work on adjusting your column names, column width, number formatting, 
# and anything else you might want to try out.

# YOUR CODE HERE
```

Learn More

If you want to learn more about pivot_wider(), see Chapter 5 of R for Data Science.

If you really want to go deep on making good tables, check out the course Making Beautiful Tables with R or read Chapter 5 of my book R Without Statistics.

To learn more about the flextable package, visit its documentation page. There is also a book that David Gohel, the package author, has created to show how flextable works. And finally, there is a flextable gallery, where you can see examples of nice tables made with the package (and the code used to make them).

To learn more about the gt package, check out its documentation page.

Have any questions? Put them below and we will help you out!

You need to be signed-in to comment on this post. Login.

Course Content

127 Lessons