rbind in R: How to Bind Data Frame Rows Vertically in R

To merge two data frames (datasets) horizontally, use the merge() function in the R language. To bind or combine rows in R, use the rbind() function. The rbind() stands for row binding.

rbind in R

The rbind() is a built-in R function that combines various vectors, matrices, and/or data frames by rows. To join two data frames (datasets) vertically, use the rbind() function. The two data frames must have the same variables, but they do not have to be in the same order.

Syntax

rbind(x, x1)

Parameters

The is input data.

The x1 is the data that needs to be binded.

Implementing the rbind() function in R

The rbind() function in R is used to bind the rows of different groups of data.

Let’s take an inbuilt dataset called PlantGrowth.

To use the dataset, use the data() function and pass the dataset name.

data("PlantGrowth")

To get the first 6 rows of the data frame, use the head() function.

To get the last 6 rows of the data frame, use the tail() function.

Let’s use the head() and tail() functions to create two separate data frames.

data("PlantGrowth")

df1 <- head(PlantGrowth)
df2 <- tail(PlantGrowth)

cat("First Data Frame: ", "\n")
df1
cat("Second Data Frame: ", "\n")
df2

Output

First Data Frame:
   weight  group
1  4.17    ctrl
2  5.58    ctrl
3  5.18    ctrl
4  6.11    ctrl
5  4.50    ctrl
6  4.61    ctrl
Second Data Frame:
   weight  group
25 5.37    trt2
26 5.29    trt2
27 4.92    trt2
28 6.15    trt2
29 5.80    trt2
30 5.26    trt2

Combine these two data frames vertically using the rbind() function.

Add the following code.

combinedDf <- rbind(df1, df2)
cat("Combined by rows: ", "\n")
combinedDf

Output

Combined by rows:
   weight  group
1  4.17    ctrl
2  5.58    ctrl
3  5.18    ctrl
4  6.11    ctrl
5  4.50    ctrl
6  4.61    ctrl
25 5.37    trt2
26 5.29    trt2
27 4.92    trt2
28 6.15    trt2
29 5.80    trt2
30 5.26    trt2

You can see that the first 6 and last 6 rows have been merged into one data frame using the rbind() function.

Binding two data frames of unequal length

Two data frames don’t have an equal length, as you often find data frames with unequal lengths. Let’s see what we will get in the output when we combine two unequal data frames using the rbind function in R.

employee_id <- c(101, 201, 301, 401)
employee_name <- c("Mark", "Dustin", "Eduwardo", "Chris")
df1 <- data.frame(employee_id, employee_name)
cat("First Data Frame: ", "\n")
df1

employee_id <- c(101, 201, 301, 401)
employee_name <- c("Mark", "Dustin", "Eduwardo", "Chris")
employee_age <- c(21, 21, 22, 23)
df2 <- data.frame(employee_id, employee_name, employee_age)
cat("Second Data Frame: ", "\n")
df2

combinedDf <- rbind(df1, df2)
cat("Combined by rows: ", "\n")
combinedDf

Output

First Data Frame:
   employee_id   employee_name
1    101             Mark
2    201             Dustin
3    301             Eduwardo
4    401             Chris
Second Data Frame:
   employee_id   employee_name  employee_age
1    101           Mark             21
2    201           Dustin           21
3    301           Eduwardo         22
4    401           Chris            23
Error in rbind(deparse.level, ...) :
 numbers of columns of arguments do not match
Calls: rbind -> rbind
Execution halted

In this example, we have created two data frames from vectors.

The first data frame is created from two vectors, and the second data frame is created from three vectors.

That means the first data frame has two columns and the second data frame has three columns, which causes the unequal length. In the final step, we used the rbind() function to combine by the rows, but due to the unequal length of several columns of the data frames, we got the following error.

numbers of columns of arguments do not match

This error clearly states that you have two data frames that have different columns, and that is why they don’t match.

Binding vectors to the data frame using rbind()

Create a data frame using vectors and then combine it with another vector using the rbind() function.

employee_id <- c(101, 201, 301, 401)
employee_name <- c("Mark", "Dustin", "Eduwardo", "Chris")
df1 <- data.frame(employee_id, employee_name)
cat("First Data Frame: ", "\n")
df1

other_employee <- c(501, "Peter")

combinedDf <- rbind(df1, other_employee)
cat("rbind vector to data frame: ", "\n")
combinedDf

Output

First Data Frame:
   employee_id  employee_name
1    101           Mark
2    201           Dustin
3    301           Eduwardo
4    401           Chris
rbind vector to data frame:
   employee_id  employee_name
1     101          Mark
2     201          Dustin
3     301          Eduwardo
4     401          Chris
5     501          Peter

In this example, we have defined a data frame with two columns and four rows and then combining another row vector to the data frame using the rbind() function.

rbind fill – Row Bind with Missing Columns

The binding of data frames with different column names is more complicated with the rbind() function. Therefore, R normally returns the error “Error in match.names(clabs, names(xi))”, if you try to use the rbind() function for data frames with different columns, as we have seen in one of the above sections.

For that reason, the plyr package (be careful: it’s called plyr; not dplyr) provides the rbind.fill() R function as an add-on to the rbind() base function.

library("plyr")

col1 <- c(1, 2, 3, 4)
col2 <- c(5, 6, 7, 8)
col3 <- c(9, 10, 11, 12)
df <- data.frame(col1, col2, col3)
df

col3 <- c(13, 14, 15, 16)
col4 <- c(18, 19, 20, 21)
df2 <- data.frame(col3, col4)
df2

finalDf <- rbind.fill(df, df2)
finalDf

Output

  col1 col2 col3
1  1    5    9
2  2    6   10
3  3    7   11
4  4    8   12
 col3  col4
1 13   18
2 14   19
3 15   20
4 16   21
   col1 col2 col3 col4
1   1    5    9    NA
2   2    6   10    NA
3   3    7   11    NA
4   4    8   12    NA
5   NA  NA   13    18
6   NA  NA   14    19
7   NA  NA   15    20
8   NA  NA   16    21

In this example, the rbind() function creates a column for each column name that appears either in the first or second data matrix. If a column exists in both data frames, it is row binded as usual. However, if a column is missing in one of the two data frames, the empty cells are replaced by NA.

Conclusion

The rbind() function in R is used to merge data frames by rows and is very useful when dealing with a large amount of data.

You can quickly bind two data frames of the same column count using the rbind() function.

In the same way, if the data frames have unequal column counts, use the bind_rows() function along with the dplyr package.

That is it for the rbind function in R.

Leave a Comment