Basic R Syntax:
rbind(my_data, new_row)
The name of the rbind R function stands for row-bind. The rbind function can be used to combine several vectors, matrices and/or data frames by rows. Above, you can find the basic code for rbind in R.
In the following article, I’m going to provide you with 3 examples for the application of the rbind function in R. Let’s start right away.
The easiest way of using rbind in R is the combination of a vector and a data frame. First, let’s create some example data frame…
x1 <- c(7, 4, 4, 9) # Column 1 of data frame
x2 <- c(5, 2, 8, 9) # Column 2 of data frame
x3 <- c(1, 2, 3, 4) # Column 3 of data frame
data_1 <- data.frame(x1, x2, x3) # Create example data frame
…and an example vector:
vector_1 <- c(9, 8, 7) # Create example vector
Now, let’s rbind this vector to the data frame:
rbind(data_1, vector_1) # rbind vector to data frame
## x1 x2 x3
## 1 7 5 1
## 2 4 2 2
## 3 4 8 3
## 4 9 9 4
## 5 9 8 7
The above table illustrates the output of the rbind function: * The first four rows are identical to our original data frame data_1; * the fifth row is identical to our vector vector_1.
The rbind command can also be applied to two data frames. Let’s create a second data frame and row bind it to data_1 (the data frame that we created above):
x1 <- c(7, 1) # Column 1 of data frame 2
x2 <- c(4, 1) # Column 2 of data frame 2
x3 <- c(4, 3) # Column 3 of data frame 2
data_2 <- data.frame(x1, x2, x3) # Create second data frame
We can rbind these two data frames with the same R code as before:
rbind(data_1, data_2) # rbind two data frames in R
## x1 x2 x3
## 1 7 5 1
## 2 4 2 2
## 3 4 8 3
## 4 9 9 4
## 5 7 5 1
## 6 4 2 2
## 7 4 8 3
## 8 9 9 4
As in Example 1, the upper part of the rbind output consists of data_1 and the lower part of the rbind output consists of data_2.
Note: Both data frames have the same column names. When the columns of the two data frames differ, it gets a bit more complicated. In the next example, I’m going to show you how to rbind data frames with different column names.
The binding of data frames with different columns / column names is a bit more complicated with the rbind function. R usually returns the error “Error in match.names(clabs, names(xi))”, if you try to use the rbind function for data frames with different columns.
For that reason, the plyr package (be careful: it’s called plyr; not dplyr) provides the rbind.fill R function as add-on to the rbind base function. In the following example, I’ll show you how to use the plyr rbind.fill function in R.
Consider the following two example data frames:
col1 <- c(5, 1, 1, 8) # Column 1 of data frame 1 plyr example
col2 <- c(9, 7, 5, 1) # Column 2 of data frame 1 plyr example
col3 <- c(1, 1, 2, 2) # Column 3 of data frame 1 plyr example
data_plyr1 <- data.frame(col1, col2, col3) # Create plyr data frame 1
col3 <- c(5, 1, 1, 8) # Column 1 of data frame 2 plyr example
col4 <- c(9, 7, 5, 1) # Column 2 of data frame 2 plyr example
data_plyr2 <- data.frame(col3, col4) # Create plyr data frame 2
To apply rbind.fill in R, we need to load the plyr package first:
library("plyr")
Now, we can apply the rbind.fill R command to our two example data frames:
rbind.fill(data_plyr1, data_plyr2) # Apply the rbind.fill plyr function
## col1 col2 col3 col4
## 1 5 9 1 NA
## 2 1 7 1 NA
## 3 1 5 2 NA
## 4 8 1 2 NA
## 5 NA NA 5 9
## 6 NA NA 1 7
## 7 NA NA 1 5
## 8 NA NA 8 1
The above table makes it clear how rbind fill works: The function creates a column for each column name that appears either in the first or in the 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.