How to Concatenate Two Columns in R

Sheeraz Gul Feb 15, 2024
  1. Concatenate Two Columns in R Using the paste() Function
  2. Concatenate Two Columns in R Using the unite() Function From the tidyr Package
  3. Concatenate Two Columns in R Using the paste0() Function
  4. Concatenate Two Columns in R Using the str_c() Function
  5. Conclusion
How to Concatenate Two Columns in R

Data manipulation is a fundamental aspect of data analysis, and concatenating columns is a common operation when working with datasets in R. Whether you’re merging textual information, creating new variables, or preparing data for analysis, having a solid understanding of the various methods available for column concatenation is important.

In this article, we will explore different techniques to concatenate two columns in R, using functions such as paste(), paste0(), str_c() from the stringr package, and the unite() function from the tidyr package. Each method has its strengths and use cases.

Concatenate Two Columns in R Using the paste() Function

Concatenating two columns in R can be achieved with the versatile paste() function, a built-in feature in base R. This function is particularly useful for combining the values of two columns within a data frame.

The paste() function in R has a flexible syntax that enables you to concatenate multiple character vectors, factors, or expressions. The basic syntax is as follows:

paste(..., sep = " ", collapse = NULL)

Where:

  • ...: Represents the vectors or expressions to be concatenated.
  • sep: Specifies the separator between the values (default is a space).
  • collapse: If present, this is inserted between input vectors and concatenated.

Consider the following example using a data frame named Delftstack:

# Creating a sample data frame
Delftstack <- data.frame(
    Name = c("Jack", "John", "Mike", "Michelle", "Jhonny"),
    LastName = c("Danials", "Cena", "Chandler", "McCool", "Nitro"),
    Id = c(101, 102, 103, 104, 105),
    Designation = c("CEO", "Project Manager", "Senior Dev", "Junior Dev", "Intern")
)

# Displaying the data frame before concatenation
print("Dataframe before concatenating columns:-")
Delftstack

# Concatenating 'Id' and 'Name' columns into a new column 'Id_Name'
Delftstack$Id_Name <- paste(Delftstack$Id, Delftstack$Name, sep = "_")

# Displaying the data frame after concatenation
print("Dataframe after concatenating columns:-")
Delftstack

In the provided code, we start by creating a sample data frame Delftstack with columns Name, LastName, Id, and Designation. Before concatenation, we display the original data frame.

The key line for concatenation is Delftstack$Id_Name <- paste(Delftstack$Id, Delftstack$Name, sep = "_"). This line creates a new column named Id_Name and uses the paste() function to concatenate the Id and Name columns with an underscore separator.

After concatenation, we display the data frame again, showcasing the newly added Id_Name column.

Output:

Concatenate Two Columns in R Using paste()

The paste() function efficiently concatenates columns in R by allowing customization of the separator and handling the merging of values seamlessly. The provided example showcases its application in creating a new column (Id_Name) by combining the Id and Name columns with an underscore separator.

Concatenate Two Columns in R Using the unite() Function From the tidyr Package

While the paste() function provides a straightforward way to concatenate columns in R, the tidyr package offers a specialized function called unite() that simplifies the process of combining columns in a data frame. The unite() function is particularly useful when dealing with tidy data and is a part of the tidyverse collection of R packages.

The unite() function in the tidyr package has the following syntax:

unite(data, col, ..., sep = "_", remove = TRUE)

Where:

  • data: The input data frame.
  • col: The name of the new column to be created.
  • ...: Columns to be concatenated.
  • sep: Separator between values (default is an underscore).
  • remove: If TRUE, removes the original columns; if FALSE, retains them.

Before using the unite() function, make sure to install and load the tidyr package. You can install the package using the following command:

install.packages("tidyr")

Once installed, load the tidyr package using:

library(tidyr)

Let’s use the same sample data frame, Delftstack for our illustration:

library(tidyr)

Delftstack <- data.frame(
    Name = c("Jack", "John", "Mike", "Michelle", "Jhonny"),
    LastName = c("Danials", "Cena", "Chandler", "McCool", "Nitro"),
    Id = c(101, 102, 103, 104, 105),
    Designation = c("CEO", "Project Manager", "Senior Dev", "Junior Dev", "Intern")
)

# Displaying the data frame before concatenation
print("Dataframe before concatenating columns:-")
Delftstack

# Using `unite()` to concatenate 'Id' and 'Name' into a new column 'Id_Name'
Delftstack <- unite(Delftstack, Id_Name, Id, Name, sep = "_")

# Displaying the data frame after concatenation
print("Dataframe after concatenating columns:-")
Delftstack

In the provided code, we begin by loading the tidyr package and creating the same sample data frame, Delftstack. Before concatenation, we display the original data frame.

The crucial line for concatenation is Delftstack <- unite(Delftstack, Id_Name, Id, Name, sep = "_"). Here, unite() is employed to create a new column Id_Name by concatenating the Id and Name columns with an underscore separator.

Following the concatenation, we display the data frame again, showcasing the addition of the Id_Name column.

Output:

Concatenate Two Columns in R Using unite()

The unite() function from the tidyr package proves to be a powerful tool for concatenating columns in R.

This example demonstrates its application by creating a new column (Id_Name) through the concatenation of the Id and Name columns with an underscore separator. The flexibility and simplicity of unite() make it an excellent choice for such operations.

Concatenate Two Columns in R Using the paste0() Function

Another efficient method for concatenating two columns is using the paste0() function.

The paste0() function is a shorthand for paste(..., sep = ""), meaning it concatenates values without any separator. This makes it an excellent choice when you want a straightforward combination of two columns.

The paste0() function in R has a simple syntax:

paste0(..., collapse = NULL)

Where:

  • ...: Represents the vectors or expressions to be concatenated.
  • collapse: If present, this is inserted between input vectors and concatenated.

Let’s use the same sample data frame, Delftstack, for our illustration:

Delftstack <- data.frame(
    Name = c("Jack", "John", "Mike", "Michelle", "Jhonny"),
    LastName = c("Danials", "Cena", "Chandler", "McCool", "Nitro"),
    Id = c(101, 102, 103, 104, 105),
    Designation = c("CEO", "Project Manager", "Senior Dev", "Junior Dev", "Intern")
)

# Displaying the data frame before concatenation
print("Dataframe before concatenating columns:-")
Delftstack

# Using `paste0()` to concatenate 'Id' and 'Name' into a new column 'Id_Name'
Delftstack$Id_Name <- paste0(Delftstack$Id, Delftstack$Name)

# Displaying the data frame after concatenation
print("Dataframe after concatenating columns:-")
Delftstack

Here, we start by creating the same sample data frame, Delftstack, with columns Name, LastName, Id, and Designation. Before concatenation, we display the original data frame.

The crucial line for concatenation is Delftstack$Id_Name <- paste0(Delftstack$Id, Delftstack$Name). This line creates a new column, Id_Name, and uses the paste0() function to concatenate the Id and Name columns without any separator.

Following this, we display the data frame again, highlighting the addition of the Id_Name column.

Output:

Concatenate Two Columns in R Using paste0()

The paste0() function is a concise and effective way to concatenate columns in R, especially when a separator is not needed.

The provided example demonstrates its application in creating a new column (Id_Name) by combining the selected columns without any separation. This function’s simplicity and directness make it a valuable tool for concatenation tasks.

Concatenate Two Columns in R Using the str_c() Function

In R, the str_c() function from the stringr package provides a versatile tool for concatenating strings, including columns in a data frame. Unlike paste() and paste0(), str_c() provides more flexibility and control over the concatenation process.

The str_c() function in the stringr package has the following syntax:

str_c(..., sep = "", collapse = NULL)

Where:

  • ...: Represents the vectors or expressions to be concatenated.
  • sep: Specifies the separator between the values (default is an empty string).
  • collapse: If present, this is inserted between input vectors and concatenated.

Before utilizing the str_c() function, you need to install and load the stringr package. You can install the package with the following command:

install.packages("stringr")

Once installed, load the stringr package using:

library(stringr)

Let’s use the same sample data frame, Delftstack, for our illustration:

library(stringr)

Delftstack <- data.frame(
    Name = c("Jack", "John", "Mike", "Michelle", "Jhonny"),
    LastName = c("Danials", "Cena", "Chandler", "McCool", "Nitro"),
    Id = c(101, 102, 103, 104, 105),
    Designation = c("CEO", "Project Manager", "Senior Dev", "Junior Dev", "Intern")
)

# Displaying the data frame before concatenation
print("Dataframe before concatenating columns:-")
Delftstack

# Using `str_c()` to concatenate 'Name' and 'LastName' into a new column
# 'Full_Name'
Delftstack$Full_Name <- str_c(Delftstack$Name, Delftstack$LastName, sep = " ")

# Displaying the data frame after concatenation
print("Dataframe after concatenating columns:-")
Delftstack

In the code example above, we begin by loading the stringr package and creating a sample data frame Delftstack with columns Name, LastName, Id, and Designation. Before concatenation, we display the original data frame.

The crucial line for concatenation is Delftstack$Full_Name <- str_c(Delftstack$Name, Delftstack$LastName, sep = " "). This line creates a new column, Full_Name, and uses the str_c() function to concatenate the Name and LastName columns with a space separator.

After concatenation, we display the data frame again, highlighting the addition of the Full_Name column.

Output:

Concatenate Two Columns in R Using str_c()

The str_c() function from the stringr package provides a powerful and flexible approach for concatenating columns in R.

The example demonstrates its application in creating a new column (Full_Name) by combining the Name and LastName columns with a space separator. The stringr package’s functions, including str_c(), enhance string manipulation tasks with added functionality and simplicity.

Conclusion

In conclusion, concatenating two columns in R offers multiple approaches, providing flexibility to meet different requirements. We explored various techniques in this article, including using the base R functions paste() and paste0(), the unite() function from the tidyr package, and the str_c() function from the stringr package.

The paste() and paste0() functions in base R are straightforward and effective for concatenation, with paste0() being particularly handy when no separator is needed. The unite() function from the tidyr package streamlines the process, allowing for efficient concatenation and column removal in a single step.

Additionally, the str_c() function from the stringr package provides enhanced string manipulation capabilities, enabling users to concatenate with specified separators and added flexibility.

Whether you need a simple combination without a separator or a more intricate concatenation with customized formatting, these methods provide the flexibility and control necessary for efficient data preprocessing. By incorporating these techniques into your R workflow, you can enhance your data manipulation skills and streamline the preparation of data for meaningful analysis.

Author: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook

Related Article - R Column