How to Combine Data Frames Using dplyr


Often you may want to combine multiple data frames together by using functions from the dplyr package in R.

There are two easy ways to do so:

Method 1: Use bind_rows() to Combine Data Frames by Rows

library(dplyr)

new_df <- bind_rows(df1, df2)

The bind_rows() function from the dplyr package can be used to combine two data frames by “binding” them together via their rows.

This particular example row-binds together the data frames named df1 and df2 into a single data frame named new_df.

This approach creates one “long” data frame.

Method 1: Use bind_cols() to Combine Data Frames by Columns

library(dplyr)

new_df <- bind_cols(df1, df2)

The bind_cols() function from the dplyr package can be used to combine two data frames by “binding” them together via their columns.

This particular example column-binds together the data frames named df1 and df2 into a single data frame named new_df.

This approach creates one “wide” data frame.

The following example shows how to use each of these methods in practice.

Note: You may need to first use the following syntax to install the dplyr package:

install.package('dplyr')

Once the dplyr package is installed, you can then use the various functions from it to combine two data frames together.

Example 1: Use bind_rows() to Combine Data Frames by Rows

Suppose we create the following two data frames that contain information about various basketball players:

#create first data frame
df1 <- data.frame(team=c('A', 'A', 'B', 'B', 'C'),
                  points=c(22, 25, 30, 43, 19))

df1

  team points
1    A     22
2    A     25
3    B     30
4    B     43
5    C     19

#create second data frame
df2 <- data.frame(team=c('D', 'D', 'E', 'F', 'F'),
                  points=c(11, 36, 29, 22, 30))

df2

  team points
1    D     11
2    D     36
3    E     29
4    F     22
5    F     30

Notice that these two data frames contain the same number of columns.

Suppose that we would like to combine these two data frames together into one long data frame.

We can use the following syntax to do so:

library(dplyr)

#row bind two data frames together
new_df <- bind_rows(df1, df2)

#view new data frame
new_df

   team points
1     A     22
2     A     25
3     B     30
4     B     43
5     C     19
6     D     11
7     D     36
8     E     29
9     F     22
10    F     30

This returns one “long” data frame named new_df that has combined the data frames named df1 and df2 into one.

Example 2: Use bind_cols() to Combine Data Frames by Columns

Suppose we create the following two data frames that contain information about various basketball players:

#create first data frame
df1 <- data.frame(team=c('A', 'A', 'B', 'B', 'C'),
                  points=c(22, 25, 30, 43, 19))

df1

  team points
1    A     22
2    A     25
3    B     30
4    B     43
5    C     19

#create second data frame
df2 <- data.frame(position=c('D', 'D', 'E', 'F', 'F'),
                  assists=c(11, 36, 29, 22, 30))

df2

  position assists
1        D      11
2        D      36
3        E      29
4        F      22
5        F      30

Notice that these two data frames contain the same number of rows.

Suppose that we would like to combine these two data frames together into one wide data frame.

We can use the following syntax to do so:

library(dplyr)

#column bind two data frames together
new_df <- bind_cols(df1, df2)

#view new data frame
new_df

  team points position assists
1    A     22        D      11
2    A     25        D      36
3    B     30        E      29
4    B     43        F      22
5    C     19        F      30

This returns one “wide” data frame named new_df that has combined the data frames named df1 and df2 into one.

Additional Resources

The following tutorials explain how to perform other common tasks in R:

How to Use slice_max() in dplyr
How to Rename Columns Using dplyr
How to Add Row to Data Frame Using dplyr
How to Use the pull() Function in dplyr

Leave a Reply

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