separate() & unite()
Learn to split one column into many and combine many columns into one using the tidyr package in R.
What is Tidy Data?
In R's tidyverse, tidy data follows three rules: each variable has its own column, each observation has its own row, and each value has its own cell. But real-world data is messy: sometimes two variables are crammed into one column, or one variable is spread across several.
The tidyr Package
tidyr is part of the tidyverse: a collection of R packages designed for data science. It provides functions to reshape and tidy data frames. Two of the most useful are:
library(tidyr)
Why Separate and Unite?
Real datasets from surveys, APIs, and spreadsheets often pack multiple pieces of information into a single cell. Before you can filter, group, or visualize, you need to isolate each variable. Conversely, sometimes you need to create a composite key by joining columns together.
The separate() Function
The unite() Function
separate() vs unite(): Visual
sep: Character vs Position
The sep argument in separate() can take two types of values, and they behave very differently:
| Character sep | Numeric sep | |
|---|---|---|
| Type | String or regex | Integer(s) |
| How it works | Splits at the delimiter | Splits at character position(s) |
| Example | sep = "-" | sep = c(4, 6) |
| Input: "20240315" | Cannot split (no dash) | "2024", "03", "15" |
| Input: "2024-03-15" | "2024", "03", "15" | "2024", "-0", "3-15" |
How to Apply These Functions
Example 1: Splitting Dates
Given a data frame with a combined "date" column, split it into year, month, and day:
df <- tibble(date = c("2024-03-15", "2023-11-02"))
# Separate the date column
df %>%
separate(col = date,
into = c("year", "month", "day"),
sep = "-")
| year | month | day |
|---|---|---|
| 2024 | 03 | 15 |
| 2023 | 11 | 02 |
Example 2: Combining Names
Given separate first and last name columns, create a single "full_name" column:
df <- tibble(first = c("Ada", "Alan"),
last = c("Lovelace", "Turing"))
# Unite first and last into full_name
df %>%
unite(col = "full_name",
first, last,
sep = " ")
| full_name |
|---|
| Ada Lovelace |
| Alan Turing |
Example 3: convert and remove
Split a "score" column and keep the original, while converting the results to proper types:
df %>%
separate(col = score,
into = c("subject", "points"),
sep = "-",
remove = FALSE,
convert = TRUE)
| score | subject | points |
|---|---|---|
| math-95 | math | 95 |
| reading-88 | reading | 88 |
Example 4: Chaining Both Together
You can separate and then unite in a single pipeline to restructure data:
# Split date, then recombine as month/year
df %>%
separate(date, c("year", "month", "day"), "-") %>%
unite("month_year", month, year, sep = "/")
| month_year | day |
|---|---|
| 03/2024 | 15 |
| 11/2023 | 02 |
Common Mistakes
Summary Comparison
| separate() | unite() | |
|---|---|---|
| Direction | 1 column → many | Many columns → 1 |
| Key argument | into (new names) | col (new name) |
| Default sep | Any non-alphanumeric | "_" |
| convert arg? | Yes | No |
| remove default | TRUE | TRUE |
| Inverse of | unite() | separate() |
Question 1 of 5
What does separate() do to a data frame?
Question 2 of 5
Which argument in separate() provides the names for the new columns?
Question 3 of 5
What is the default separator used by unite()?
Question 4 of 5
You run: df %>% separate(score, c("subject", "points"), sep = "-", convert = TRUE). If "score" contains "math-95", what type will the "points" column be?
Question 5 of 5
You have columns city and state. Which code correctly creates a "location" column with values like "Austin, TX"?