Row bind using rbind() in R

Rbind() function in R row binds the data frames which is a simple joining or concatenation of two or more dataframes (tables) by row wise. In other words, Rbind in R appends or combines vector, matrix or data frame by rows. bind_rows() function in dplyr package of R is also performs the row bind opearion. lets see an example of both the functions.

In this Tutorial we will look at

  • Example of row bind operation in R by using rbind() function.
  • How to perform row bind operation using bind_rows() function of dplyr package
  • Whats is difference between rbind() function and bind_rows() Function with an example.

The pictographical representation of row bind operation is shown below. It is simple concatenate of the two or more tables on row wise.

row bind in R - R bind Example

Note: Column names and the number of columns of the two dataframes needs to be same for rbind() function and it is not necessary to be same for bind_rows() function .

 

Syntax for rbind() function in R:

rbind(x1,x2)

x1,x2 can be data frame, matrix or vector.

 

Example of rbind() function in R: 

Lets see how to implement Rbind function in R with an example. First lets create two data frames

#Create two data frames

df1 = data.frame(CustomerId = c(1:6), Product = c(rep("Oven", 3), rep("Television", 3)))
df1
df2 = data.frame(CustomerId = c(4:7), Product = c(rep("Television", 2), rep("Air conditioner", 2)))
df2
  df1 will be 

row bind in r rbind 2

  df2 will be 

row bind in r rbind 3

rbind() function in R: 

Now, Row bind (rbind) these two data frames as shown below. rbind() function takes two dataframes as argument and results the appended or row binded dataframe. Column names and the number of columns of the two dataframes needs to be same for rbind() function.

# row bind the data frames with rbind function in R.

rbinded_df = rbind(df1,df2)
rbinded_df

so the resultant row binded data frame with rbind() function will be

row bind in r rbind 4

 

 

Row bind using bind_rows() function in R

bind_rows() function takes two dataframes as argument and results the appended or row binded dataframe. The number of columns of the two dataframe not necessarily needs to be same. which is not the case in rbind() function.

# row bind the data frames with bind_rows() function in R.

library(dplyr) 
binded_df = bind_rows(df1,df2)
binded_df

so the resultant row binded data frame with bind_rows() function will be

row bind in r rbind 4

 

 

Difference between rbind() function and bind_rows() function:

The number of columns of the two dataframes needs to be same for rbind() function and it is not necessary to be same for bind_rows() function. When we combine two data frames having different number of columns. rbind() throws an error  whereas bind_rows assigns “NA” to those rows of columns missing in one of the data frames where the value is not provided by the data frames.

Lets try out with an example

#Create two data frames

df1 = data.frame(CustomerId = c(1:6), Product = c(rep("Oven", 3), rep("Television", 3)))
df1
df2 = data.frame(CustomerId = c(4:7), Product = c(rep("Television", 2), rep("Air conditioner", 2)),State=c(rep("California", 2), rep("New Jersey", 2)))
df2
  df1 will be 

row bind in r rbind 2

df2 will be

row bind in r rbind 5

row bind using rbind()

lets try row binding the two dataframes using rbind() function.

 
rbind(df1,df2) 

when we bind these two columns using rbind() function it will throw the following error

row bind in r rbind 7

 

row bind using bind_rows():

lets try row binding the two dataframes using bind_rows() function.

 
library(dplyr) 
bind_rows(df1,df2)

when we bind these two columns using bind_rows() function, the two dataframes are binded with  “NA”s are assigned to those rows of columns missing as shown below.

row bind in r rbind 6

so bind_rows() perform better than rbind() considering this aspect. on further read on this difference between bind_rows() and rbind() refer to : stack overflow 

 


Other Related Topics:

rbind function in r                                                                                                         rbind in R

Author

  • Sridhar Venkatachalam

    With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark.