Paste Multiple Columns Together in R (2 Examples)

 

In this tutorial, I’ll explain how to merge multiple columns into one single variable in the R programming language.

The article will consist of two examples for the combination of several variables. More precisely, the post contains the following content blocks:

Let’s just jump right in…

 

Creating Exemplifying Data

First, we’ll have to define some example data.

data <- data.frame(x = 1:5,                             # Example data
                   y = LETTERS[1:5],
                   z = 9,
                   other = c(4, 1, 7, 6, 5))
data                                                    # Print example data
#   x y z other
# 1 1 A 9     4
# 2 2 B 9     1
# 3 3 C 9     7
# 4 4 D 9     6
# 5 5 E 9     5

Have a look at the previous output of the RStudio console. It shows that our example data contains four columns. The variables x, z, and other are numeric and the variable z is a character column.

 

Example 1: Pasting Multiple Columns Together

Example 1 explains how to merge many columns into a single new column in R. In this example, we are combining the variables x, y, and z. First, we need to define a vector containing the column names of the variables we want to unite:

my_cols <- c("x", "y", "z")                             # Define variables for merge

Now, we can use a combination of the do.call and paste functions to create a new column containing the merged values of x, y, and z:

data$xyz <- do.call(paste, c(data[my_cols], sep = ""))  # Apply do.call & paste
data                                                    # Print updated data
#   x y z other xyz
# 1 1 A 9     4 1A9
# 2 2 B 9     1 2B9
# 3 3 C 9     7 3C9
# 4 4 D 9     6 4D9
# 5 5 E 9     5 5E9

As you can see based on the previous output of the RStudio console, our updated data frame consists of the same values as the original data matrix plus a new column called xyz.

 

Example 2: Remove Combined Variables After Pasting Together

Let’s assume that we want to remove the variables from our data, which we have combined in the new column xyz. Then we can use the colnames function and the %in% operator as shown below:

data <- data[ , ! colnames(data) %in% my_cols]          # Remove columns
data                                                    # Print updated data
#   other xyz
# 1     4 1A9
# 2     1 2B9
# 3     7 3C9
# 4     6 4D9
# 5     5 5E9

Our final data consists of the other variable, which we have not touched during the whole tutorial, and the united variable xyz.

 

Video, Further Resources & Summary

I have recently released a video on my YouTube channel, which explains the R programming code of this article. You can find the video below.

 

Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.

YouTube Content Consent Button Thumbnail

YouTube privacy policy

If you accept this notice, your choice will be saved and the page will refresh.

 

Furthermore, I can recommend to read the other articles on this website. You can find some tutorials here.

 

To summarize: At this point of the tutorial you should know how to concatenate and paste two or more columns in R programming. In case you have any further questions, don’t hesitate to let me know in the comments.

 

Subscribe to my free statistics newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top