9  Combine datasets in dplyr

How to use mutating joins to merge data

Author

Shane McCarty, PhD

Published

10.23.2025

Abstract

This chapter teaches researchers how to combine datasets.

Keywords

dplyr, merge data

```{r}
library(readxl)
library(dplyr)
# source: https://dplyr.tidyverse.org/reference/mutate-joins.html

# Read your Excel files
primary_data <- read_excel("primary_file.xlsx")
secondary_data <- read_excel("secondary_file.xlsx")

# Select only the variables you need from secondary dataset, then join
combined <- primary_data %>%
  left_join(
    secondary_data %>% select(PASSWORD, SLEEPSCORE1, SLEEPSCORE2, SLEEPSCORE3),
    by = "PASSWORD"
  )
  
```