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 in­for­ma­tion 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 fa­cil­i­tate a more thorough analysis or to bring together different parts of a data set. Note that rbind() works ef­fi­cient­ly for smaller data sets, but packages like dplyr offer better per­for­mance 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
text

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
text

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
text

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

Below we’ll demon­strate 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
text

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
text

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
text

bind_rows() suc­cess­ful­ly combines the two data frames. Empty fields are marked with <NA>. The function is a good al­ter­na­tive to rbind() in R if you need to combine data frames with differing numbers of columns.

Tip

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

Web hosting
Get your site online fast without breaking the bank
  • Fast page loads while cutting costs by 60%
  • Keep your site open 24/7 with 99.99% uptime
  • Never run out of space with unlimited storage

Reviewers

Go to Main Menu