0
0
R-programmingHow-ToBeginner · 3 min read

How to Use unite() in tidyr for Combining Columns in R

Use unite() from the tidyr package to combine multiple columns into a single column by specifying the new column name and the columns to unite. You can control the separator between values with the sep argument and decide whether to keep or remove the original columns with remove.
📐

Syntax

The unite() function combines multiple columns into one. It takes these main arguments:

  • data: your data frame.
  • col: the name of the new combined column.
  • ...: the columns to combine, listed by name.
  • sep: a string to separate the combined values (default is "_").
  • remove: logical, whether to remove the original columns (default is TRUE).
r
unite(data, col, ..., sep = "_", remove = TRUE)
💻

Example

This example shows how to combine the year and month columns into a new column called year_month with a dash separator.

r
library(tidyr)
library(dplyr)

data <- tibble(
  year = c(2020, 2021, 2022),
  month = c("Jan", "Feb", "Mar"),
  value = c(10, 20, 30)
)

united_data <- unite(data, year_month, year, month, sep = "-", remove = TRUE)
print(united_data)
Output
# A tibble: 3 × 2 year_month value <chr> <dbl> 1 2020-Jan 10 2 2021-Feb 20 3 2022-Mar 30
⚠️

Common Pitfalls

Common mistakes when using unite() include:

  • Forgetting to load the tidyr package.
  • Not specifying the columns correctly, which can cause errors or unexpected results.
  • Using the default separator "_" when a different separator is needed.
  • Not realizing that remove = TRUE deletes the original columns, which might be needed later.

Here is an example showing a wrong and right way:

r
library(tidyr)

library(tibble)
data <- tibble(
  a = c("x", "y"),
  b = c("1", "2")
)

# Wrong: forgetting to specify columns
# unite(data, "ab") # This will cause an error

# Right: specify columns to unite
unite(data, ab, a, b, sep = "")
Output
# A tibble: 2 × 1 ab <chr> 1 x1 2 y2
📊

Quick Reference

ArgumentDescriptionDefault
dataThe data frame or tibble to modify
colName of the new combined column (string)
...Columns to unite, listed by name
sepSeparator string between combined values"_"
removeWhether to remove original columns after unitingTRUE

Key Takeaways

Use unite() to combine multiple columns into one with a custom separator.
Specify the columns to unite explicitly to avoid errors.
Set remove = FALSE if you want to keep the original columns.
Load the tidyr package before using unite().
The default separator is an underscore, but you can change it with sep.