cbind() function in R programming

You can use cbind() function in R exclusively to bind or combine the multiple columns together.

Hello folks, today, our focus will be on the applications and use cases of the cbind() function in R.

cbind() function stands for column binding. If you want to bind or combine multiple columns, then no other function will serve better than cbind().

In this article, you will be witnessing many examples that clearly explain the use and benefits of cbind() function.

Let’s roll!


Syntax of the cbind() function

cbind(): The cbind() function in R is used to bind the multiple columns of the data frame.

cbind(x,x1)

Where,

  • X = The input columns of the data frame.
  • X1 = The columns of the data frame needs to be bonded.

The idea of binding the columns

The main objective of the cbind() function is to combine or to bind the multiple columns.

The columns may include vectors, data frames, or multiple columns (more than 2).

The basic idea of working of the cbind() function in R is illustrated below with the help of a diagram.

The function will take multiple inputs and binds them together. Exactly like rbind() function but it binds the columns.

Image

Implementing the cbind() function in R

We can hopefully start things with a simple example, moreover, it will act as a base for further applications of cbind() function.

Well, we are going to create simple data frames by multiple vectors.

#creating a data frame
df<-data.frame(Column_1=c(1,2,3,4,5), Column_2=c(6,7,8,9,10), Column_3=c(11,12,13,14,15))
df
Column_1  Column_2  Column_3
1        1        6       11
2        2        7       12
3        3        8       13
4        4        9       14
5        5       10       15
#creating a data frame
 
df_1<-data.frame(New_column_1=c(2,4,6,8,10))
df_1
New_column_1  
1            2            
2            4            
3            6            
4            8           
5           10 

We have created two separate data frames df and df_1. Now using cbind() function in R, let’s bind them together.

And the you can do this with a single piece of code as earlier.

#binding 2 data frames
 
cbind(df,df_1)
Column_1  Column_2  Column_3   New_column_1 
1        1        6       11            2            
2        2        7       12            4            
3        3        8       13            6            
4        4        9       14            8           
5        5       10       15           10  

1. Binding data frames using cbind() function

The cbind() function is capable of binding two entire data sets/data frames as well. In this section, we are going to bind two data sets using the cbind() function.

Let’s see how it works.

#binds the 2 data sets
 
cbind(ToothGrowth,BOD)
len  supp dose  Time  demand
1   4.2   VC  0.5    1    8.3
2  11.5   VC  0.5    2   10.3
3   7.3   VC  0.5    3   19.0
4   5.8   VC  0.5    4   16.0
5   6.4   VC  0.5    5   15.6
6  10.0   VC  0.5    7   19.8
7  11.2   VC  0.5    1    8.3
8  11.2   VC  0.5    2   10.3
9   5.2   VC  0.5    3   19.0
10  7.0   VC  0.5    4   16.0

In the above example, the column names ‘len’,’supp’ and ‘dose’ belongs to Toothgrowth dataset and the column names ‘Time’ and ‘demand’ belongs to the BOD dataset.

Just like this, you can easily bind two datasets together. Awesome right?

Note: For all the column binding in R, make sure that the row count in both data frames is equal.


2. Bind multiple columns with cbind() function.

You can definitely bind more than 2 columns using the cbind() function in R.

In this section, we are going to bind multiple columns.

Let’s see how it works.

#creates multiple columns
name<-c("Jay","Rajiv","Juke","Jenny","lara")
age<-c(23,25,23,24,26)
gender<-c("Female","Male","Male","Female","Female")
 
#binds the all 3 columns 
cbind(name,age,gender)
 name      age      gender  
[1,]   "Jay"     "23"     "Female"
[2,]   "Rajiv"   "25"     "Male"  
[3,]   "Juke"    "23"     "Male"  
[4,]   "Jenny"   "24"     "Female"
[5,]   "lara"    "26"     "Female"

Well, in this way you can easily bind as many columns as you want. I am going to introduce you to another function in the below section.


3. The bind_cols() function in R.

The R offers you another function to bind the multiple columns, bind_cols().

The bind_cols will be the part of ‘dplyr’ package. You need to import dplyr package before using this function.

Let’s see how it works.

#installing required packages
insatll.packages('dplyr')
#importing packages 
library(dplyr)
 
#creating a data frame
df<-data.frame(Column_1=c(1,2,3,4,5),Column_2=c(6,7,8,9,10),Column_3=c(11,12,13,14,15))
df
 
df_1<-data.frame(New_column_1=c(2,4,6,8,10))
df_1
 
#binds the columns 
bind_cols(df,df_1)
Column_1  Column_2  Column_3  New_column_1
1        1        6       11            2
2        2        7       12            4
3        3        8       13            6
4        4        9       14            8
5        5       10       15           10

Well, you can see that the output is same as cbind() function.

Anyway, both the functions will do the same thing i.e. column binding. But it’s my responsibility to make sure you get to know about the topic in all possible aspects.


Wrapping up

You can use cbind() function in R to bind multiple columns with ease. It is also a function having a simple syntax as well.

You can bind data frames, vectors and multiple columns using this function.

This article covers all the important factors of cbind() function and bind_cols() function and their applications.

And that’s all for now, Happy binding!!!

More read: R documentation

Ninad Pathak
Ninad Pathak
Articles: 55