R

How to Perform Rbind() and Cbind() Functions in R

In this R tutorial, we will see how to perform the row binding and column binding in vectors, lists, dataframes, and matrices. 

Rbind() Function

The rbind() function is used to perform the row binding that binds the data by row.

In a Vector
Rbind() is used to perform the row binding that binds the multiple vectors by row.

Syntax

rbind(vector_object1,vector_object2,...........)

Where vector_object is the vector.

Example
In this example, we perform the row binding on three vectors.

#create three vectors for fruits with 2 elements each.
apples=c(34,45)
mangoes=c(14,35)
guava=c(12,34)
 
 #perform row bind on these three vectors
print(rbind(apples,mangoes,guava))

Result

We can see that the vectors are combined by row.

In a List
Rbind() is used to perform the row binding that binds the multiple lists by row.

Syntax

rbind(list_object1,list_object2,...........)

Where list_object is the list.

Example
In this example, we perform the row binding on three lists.

#create three lists  for fruits with 2 elements each.
apples=list(34,45)
mangoes=list(14,35)
guava=list(12,34)
 
 #perform row  bind on these three lists
print(rbind(apples,mangoes,guava))

Result

We can see that the lists are combined by row.

In a Dataframe
Rbind() is used to perform the row binding that binds the multiple dataframes by row.

Syntax

rbind(dataframe_object1,dataframe_object2,...........)

Where dataframe_object is the dataframe.

Example 1
In this example, we perform the row binding on the market_id column in three dataframes.

#create three dataframes  for fruits with 1 column each
apples=data.frame(market_id=c(1,2,3),market_name=c('M1','M2','M3'))
mangoes=data.frame(market_id=c(4,5,6),market_name=c('M1','M2','M3'))
guava=data.frame(market_id=c(7,8,9),market_name=c('M1','M2','M3'))
 
 #perform row bind on market_id columns
print(rbind(apples$market_id,mangoes$market_id,guava$market_id))

Result

We can see that the market_id column in three dataframes is combined by row.

Example 2
In this example, we perform the row binding on three dataframes.

#create three dataframes  for fruits with 1 column each
apples=data.frame(market_id=c(1,2,3),market_name=c('M1','M2','M3'))
mangoes=data.frame(market_id=c(4,5,6),market_name=c('M1','M2','M3'))
guava=data.frame(market_id=c(7,8,9),market_name=c('M1','M2','M3'))
 
 #perform row bind on all columns
print(rbind(apples,mangoes,guava))

Result

We can see that all columns in three dataframes is combined by row.

In a Matrix
Rbind() is used to perform the row binding that binds the multiple matrices by row.

Syntax

rbind(matrix_object1,matrix_object2,...........)

Where matrix_object is the matrix.

Example
In this example, we perform the row binding on two matrices.

#create vector for fruits
fruits=c(23,4,43,3,4,5,6,7,8,967,65,43,5,56,78,67,45,44,67,665)
 
#pass the vector to a matrix with 5 rows and 4 columns
matrix_object1=matrix(fruits,nrow=5,ncol=4)
 
#create vector for vegetables
vegetables=c(23,4,43,3,4,5,6,7,8,967,65,43,5,56,78,67,87,90,87,0)
 
#pass the vector to a matrix with 4 rows
matrix_object2=matrix(vegetables,nrow=5,ncol=4)

 #perform row bind
print(rbind(matrix_object1,matrix_object2))

Result

We can see that all rows and columns in two matrices are combined by row.

Cbind() Function

The cbind() function is used to perform the column binding that binds the data by column.

In a Vector
Cbind() is used to perform the column binding that binds the multiple vectors by column.

Syntax

cbind(vector_object1,vector_object2,...........)

Where vector_object is the vector.

Example
In this example, we perform the column binding on three vectors.

#create three vectors for fruits with 2 elements each.
apples=c(34,45)
mangoes=c(14,35)
guava=c(12,34)
 
 #perfrom column binding on these three vectors
print(cbind(apples,mangoes,guava))

Result

We can see that the vectors are combined by column.

In a List
Cbind() is used to perform the column binding that binds the multiple lists by column.

Syntax

cbind(list_object1,list_object2,...........)

Where list_object is the list. 

Example
In this example, we perform the column binding on three lists.

#create three vectors for fruits with 2 elements each.
apples=c(34,45)
mangoes=c(14,35)
guava=c(12,34)
 
 #perfrom column binding on these three vectors
print(cbind(apples,mangoes,guava))

Result

We can see that the lists are combined by column.

In a Dataframe
Cbind() is used to perform the column binding that binds the multiple dataframes by column.

Syntax

cbind(dataframe_object1,dataframe_object2,...........)

Where dataframe_object is the dataframe.

Example 1
In this example, we perform the column binding on the market_id column in three dataframes.

#create three lists  for fruits with 2 elements each.
apples=list(34,45)
mangoes=list(14,35)
guava=list(12,34)
 
 #perform column  binding on these three lists
print(cbind(apples,mangoes,guava))

Result

We can see that the market_id column in three dataframes is combined by column.

Example 2
In this example, we perform the column binding on three dataframes.

#create three dataframes  for fruits with 1 column each
apples=data.frame(market_id=c(1,2,3),market_name=c('M1','M2','M3'))
mangoes=data.frame(market_id=c(4,5,6),market_name=c('M1','M2','M3'))
guava=data.frame(market_id=c(7,8,9),market_name=c('M1','M2','M3'))
 
 #perform column binding on market_id columns
print(cbind(apples$market_id,mangoes$market_id,guava$market_id))

Result

We can see that all columns in the three dataframes are combined by column. 

In a Matrix
Cbind() is used to perform the column binding that binds the multiple matrices by column.

Syntax

cbind(matrix_object1,matrix_object2,...........)

Where matrix_object is the matrix. 

Example
In this example, we perform the column binding on two matrices.

#create vector for fruits
fruits=c(23,4,43,3,4,5,6,7,8,967,65,43,5,56,78,67,45,44,67,665)
 
#pass the vector to a matrix with 5 rows and 4 columns
matrix_object1=matrix(fruits,nrow=5,ncol=4)
 
#create vector for vegetables
vegetables=c(23,4,43,3,4,5,6,7,8,967,65,43,5,56,78,67,87,90,87,0)
 
#pass the vector to a matrix with 4 rows
matrix_object2=matrix(vegetables,nrow=5,ncol=4)
 
 #perform column binding
print(cbind(matrix_object1,matrix_object2))

Result

We can see that all rows and columns in two matrices are combined by column.

Conclusion

In this R tutorial, we learned how to perform the row binding and column binding on vectors, lists, dataframes, and matrices using the rbind() and cbind() functions.

About the author

Sireesha Lavu

This is Sireesha Lavu from Gogulamudi, Andhra Pradesh, India 522015.
I am currently working as a teacher and interested in writing technical articles on computer science.