# 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.

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

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:

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

The output looks as follows:

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

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

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

Output:

```R
names    status
 1  Eva            doctor 
2   John          nurse
```

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

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

The result will look as follows:

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

### 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:

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

Output:

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

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

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

Output:

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

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

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

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()`.

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

The output looks as follows:

```R
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
```

`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](https://www.ionos.com/digitalguide/websites/web-development/r-plot/) and [R paste](https://www.ionos.com/digitalguide/websites/web-development/r-paste/).


This is a markdown version of: [https://www.ionos.com/digitalguide/websites/web-development/r-rbind/](https://www.ionos.com/digitalguide/websites/web-development/r-rbind/) for AI/LLM consumption.