The unite() function in R is “used to merge two or more columns into a single column or variable.”
Syntax
unite(data, col, ..., sep = ",", remove = TRUE)
Parameters
- data: It is a table or data frame.
- col: Name of a new column that is to be added.
- …: Names of columns that are to be united.
- sep: How to join the data in the columns.
- remove: Removes input columns from the output data frame; default = TRUE.
Return value
The unite() method returns a copy of the data frame with new columns.
Example 1: Unite Two Columns into One Column
library(tidyr)
# Sample data frame
df <- data.frame(
first_name = c("John", "Jane", "Jim"),
last_name = c("Doe", "Smith", "Brown"),
age = c(30, 25, 27)
)
# Using unite() to combine first_name and last_name into a single column
df_united <- unite(df, "full_name", first_name, last_name, sep = " ")
print(df_united)
Output
full_name age
1 John Doe 30
2 Jane Smith 25
3 Jim Brown 27
Example 2: Unite More Than Two Columns
Here’s an example where we unite three columns:
library(tidyr)
# Sample data frame
df <- data.frame(
first_name = c("John", "Jane", "Jim"),
middle_name = c("Robert", "Marie", "Albert"),
last_name = c("Doe", "Smith", "Brown"),
age = c(30, 25, 27)
)
# Using unite() to combine first_name, middle_name,
# and last_name into a single column
df_united <- unite(df, "full_name", first_name, middle_name,
last_name,
sep = " "
)
print(df_united)
Output
full_name age
1 John Robert Doe 30
2 Jane Marie Smith 25
3 Jim Albert Brown 27
That’s it!
Related posts

Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.