bind_rows & bind_cols R Functions of dplyr Package (2 Examples)

 

In this article, I’ll explain how to merge rows and columns with the bind_rows and bind_cols functions of the dplyr package in R.

The table of content is structured like this:

Let’s dive into it…

 

Introducing Example Data

In this R tutorial, I’ll use the following three data frames:

data1 <- data.frame(x1 = 1:5,                # Create three data frames
                    x2 = letters[1:5])
data2 <- data.frame(x1 = 0,
                    x3 = 5:9)
data3 <- data.frame(x3 = 5:9,
                    x4 = letters[5:9])

Note that data1 and data2 have one variable name in common. In contrast, all variable names of data1 and data3 differ.

In order to use the bind_rows and bind_cols functions, we also need to install and load the dplyr package of the tydiverse:

install.packages("dplyr")                    # Install dplyr package
library("dplyr")                             # Load dplyr package

Now, let’s move on to the examples…

 

Example 1: Combine Rows with bind_rows Function

Example 1 shows how to use the bind_rows function to append rows to a data frame in R. Have a look at the following R code:

bind_rows(data1, data2)                      # Apply bind_rows function
#    x1   x2 x3
# 1   1    a NA
# 2   2    b NA
# 3   3    c NA
# 4   4    d NA
# 5   5    e NA
# 6   0 <NA>  5
# 7   0 <NA>  6
# 8   0 <NA>  7
# 9   0 <NA>  8
# 10  0 <NA>  9

The RStudio console shows the output of the previous R syntax. We combined our two data frames data1 and data2.

Note that the bind_rows command inserted NA values whenever a variable names was not existing in both data frames (i.e. x2 is not existing in data2 and x3 is not existing in data1).

 

Example 2: Combine Columns with bind_cols Function

Example 2 explains how to use the bind_cols function to combine the columns of two data frames (or a data frame and a vector):

bind_cols(data1, data3)                      # Apply bind_cols function
#   x1 x2 x3 x4
# 1  1  a  5  e
# 2  2  b  6  f
# 3  3  c  7  g
# 4  4  d  8  h
# 5  5  e  9  i

As you can see based on the output of the RStudio console, we combined the variables x1 and x2 of data1 with the variable x3 and x4 of data3.

Note that the number of rows of the input data frames (or tibbles) should be the same.

 

Video & Further Resources

Have a look at the following video of my YouTube channel. I’m explaining the R programming code of this article in the video.

 

 

Additionally, you may read the other tutorials of this website.

 

To summarize: In this R tutorial you learned how to join and bind rows and columns of data frames and tibbles. Let me know in the comments below, in case you have further questions. Furthermore, don’t forget to subscribe to my email newsletter in order to get regular updates on the newest articles.

 



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