R Bootcamp - Class 7

Tidyverse odds & ends

Author

Jay Hesselberth

Published

September 6, 2025

Class 7 outline

  • String manipulation with stringr
  • Factor operations with forcats
  • Join functions with dplyr
  • Advanced plotting with ggplot2

Setup

String operations

Combining strings with str_c()


str_c() is similar to paste and paste0 but the behavior is more consistent.

Detecting patterns with str_detect()


Splitting strings with str_split()


Factor operations

Counting factor levels with fct_count()

Reordering factors with fct_reorder()


Lumping infrequent levels with fct_lump()



Do your numbers look different? sample() is not reproducible by default.

Aside on sample() and reproducibility


this also applies to rnorm(), runif(), and other random number generation functions.

Join operations

Setup

Open up the tidyexplain page.

Understanding joins

Joins combine data from two tables based on matching keys.

left_join() - keep all rows from left table

Most common join - keeps all observations from the “primary” table.

inner_join() - keep only matching rows

Only keeps rows that exist in both tables.

full_join() - keep all rows from both tables

Keeps everything, filling missing values with NA.

Advanced plotting

Setup

scale functions in ggplot2

  • scale_color_brewer() and scale_fill_brewer() control color and fill aesthetics.
  • See available ggplot2 brewer palettes


Combining multiple plots into a figure?

Use the {patchwork} package.

Saving plots

Saves last plot as 5’ x 5’ file named plot_final.png in working directory.

Matches file type to file extension (*.png, *.jpeg, *.pdf).

Displaying data in tables

We’ll use a couple of approaches to display data in tables instead of graphs, which can be useful for reports or presentations.

Using knitr::kable()

Using gt