What is R’s rbind() function?

The R function rbind() can be used to combine data sets that have the same number of columns.

What is the function of rbind() in R?

The R function rbind() is useful for combining data line by line. It’s often used to add new information to an existing data frame. This comes in handy if you regularly update your data and need to integrate it into an existing data set.

R’s rbind() is also used to combine two data frames with the same structure, either to facilitate a more thorough analysis or to bring together different parts of a data set. Note that rbind() works efficiently for smaller data sets, but packages like dplyr offer better performance for larger data sets.

What is the syntax of R’s rbind()?

You can enter as many data frames as you want into rbind() and have them combined. Just make sure that all the data frames have the same number of columns and the same column names.

rbind(data.frame1, data.frame2, ...)
R

The arguments data.frame1, data.frame2 and so on stand for the data frames or lists of data frames that will be linked line by line.

Examples of how to use rbind() in R

Below we’ll look at some examples of how to use R’s rbind() function. First we’ll create a data frame with two columns:

#creating data frame 1
names<-c("Deborah","Tom","Matt","Laura","Rebecca")
status<-c("nurse","doctor","nurse","doctor","nurse")
df1<-data.frame(names, status)
df1
R

The output looks as follows:

names status
1  Deborah     nurse
2  Tom   doctor
3  Matt      nurse
4  Laura   doctor
5  Rebecca     nurse
R

Next we’ll define a second data frame with the same amount of columns:

#creating data frame 2
names<-c("Eva","John")
status<-c("doctor","nurse")
df2<-data.frame(names, status)
df2
R

Output:

names    status
 1  Eva            doctor 
2   John          nurse
R

Now we can combine the two data frames with each other:

#binding rows of df1 and df2
rbind(df1,df2)
R

The result will look as follows:

names     status
1  Deborah  nurse
2  Tom           doctor    
3  Matt           nurse      
4  Laura        doctor    
5  Rebecca  nurse     
6  Eva             doctor   
7  John           nurse
R

What happens if data frames have a different number of columns?

Below we’ll demonstrate what happens when you try to combine two data frames that have a different number of columns.

First let’s once again create a data frame with two columns:

#creating data frame 1
names<-c("Deborah","Tom","Matt","Laura","Rebecca")
status<-c("nurse","doctor","nurse","doctor","nurse")
df1<-data.frame(names, status)
df1
R

Output:

names     status
1  Deborah nurse
2  Tom         doctor
3  Matt         nurse
4  Laura        doctor
5 Rebecca nurse
R

Now we’ll create a data frame with three columns:

#creating data frame 2
names<-c("Eva","John")
status<-c("doctor","nurse")
age<-c("52","38")
df2<-data.frame(names, status, age)
df2
R

Output:

names    status    age
1  Eva        doctor    52
2  John      nurse      38
R

When we use rbind() to try to combine the two data frames, we’ll get the following error message:

rbind(df1,df2)
Error in rbind(deparse.level, ...) :
    numbers of columns of arguments do not match
R

The error shows that we can’t use R’s rbind() to combine the two data frames, as they have differing numbers of columns. However, we can use bind_rows() from the dplr package.

How to combine data frames with a different number of columns

The data frames from the above example can easily be combined with bind_rows().

#install dplyr
install.packages('dplyr')
#import libraries
library(dplyr)
#bind rows
bind_rows(df1,df2)
R

The output looks as follows:

names    status            age
1  Deborah nurse      <NA>
2  Tom          doctor     <NA>
3  Matt         nurse       <NA>
4  Laura        doctor     <NA>
5  Rebecca   nurse       <NA>
6  Eva            doctor     52
7  John         nurse       38
R

bind_rows() successfully combines the two data frames. Empty fields are marked with <NA>. The function is a good alternative to rbind() in R if you need to combine data frames with differing numbers of columns.

Tip

Want to learn more about displaying and editing data sets in R? Take a look at our tutorials on R plot and R paste.

Web Hosting
Secure, reliable hosting for your website
  • 99.9% uptime and super-fast loading
  • Advanced security features
  • Domain and email included
Was this article helpful?
Page top