In this article we will work on merging two columns in R using paste() command.


Theory

Did you ever come across a problem of merging values from two columns in a single cell?

I’m sure you did! The most simple example of such an issue is when we keep track of our clients or employees across the spreadsheets. And most often we have two separate columns: “first_name” and “last_name”.

The issue then becomes, what do we do when we decided to include their names on mailing lists? Or greeting cards?

There are numerous occasions when we really need that combined value to either generate reports, or address something formally.

Below I will show an example of the usage of popular R base command paste().



Application

Below are the steps we are going to take to make sure we do master the skill of merging two columns in R:

  1. Basic paste() command description
  2. Loading the training_names.csv file into R
  3. Merging two columns (“first_name” and “last_name)”



Part 1. Basic paste() command description

The short theoretical explanation of the function is the following:

paste(objects, sep=" ")

Here, “objects” refers to any R objects you will be merging (in our case it will be two columns).

The “sep= ” “” allows you to input a character that will separate the values in the new column. It can be empty, include spaces, symbols, letters, numbers, or any combinations of them.



Part 2. Loading the training_names.csv file into R

For the purpose of this tutorial, I created a sample .csv dataset that you can use to practice merging two columns in R.

The file can be downloaded here: training_names.csv

Now, let’s go ahead and load it into R using the following command:


mydata<-read.csv("/Users/DataSharkie/Desktop/training_names.csv")

Note: remember that your location of the file will be different from mine, so adjust the code accordingly!

If you would like to learn more on how to import .csv files into R you can find an article here.

Once the dataset is imported, we can view the table using the following command:


View(mydata)

You will see three columns: "id", "first_name", and "last_name".



Part 3. Merging two columns ("first_name" and "last_name")

Our goal now is to create a new column "full_name", which will contain the persons first name and last name with the space in between them.

We start by identifying the R objects we are going to be merging: in our case it's column "first_name" and column "last_name".

Since we want to have a space between the first name and last name, we need to set the separator accordingly: sep=" ".

At this point we are all set to merge two columns in R.

You can do it using the following code:


mydata$fullname<-paste(mydata$first_name, mydata$last_name, sep=" ")

You can take a look at the edited table by using this command:


View(mydata)



If you are interested to learn more about data manipulation in R, you can find more articles here.