What is cbind in R?

This recipe explains what is cbind in R.

Recipe Objective: What is cbind in R?

This recipe shows how to use cbind in R with the help of an example.

How To Bind Columns in R?

In R, Cbind () — column bind function is used for merging two data frames, given that the number of rows in both the data frames are equal. cbind can append vectors, matrices, or any data frame by columns. 

Steps Showing How to Use cbind Function R

The following steps will show you how to use the R cbind function. 

Step 1- Define two dataframes

df1 <- data.frame(name = c('A','B','C','D','E','F'), age = c(22,25,28,19,15,23)) print(df1)

"df1 is":

  name age

1    A  22

2    B  25

3    C  28

4    D  19

5    E  15

6    F  23

df2 <- data.frame(gender = c('Male','Male','Female','Male','Female','Female')) print(df2)

"df2 is": 

  gender

1   Male

2   Male

3 Female

4   Male

5 Female

6 Female

Step 2 - Apply cbind() for Column Bind R

final_data <- cbind(df1,df2) print(final_data)

"Output of code is": 

  name age gender

1    A  22   Male

2    B  25   Male

3    C  28 Female

4    D  19   Male

5    E  15 Female

6    F  23 Female

 

What Users are saying..

profile image

Ed Godalle

Director Data Analytics at EY / EY Tech
linkedin profile url

I am the Director of Data Analytics with over 10+ years of IT experience. I have a background in SQL, Python, and Big Data working with Accenture, IBM, and Infosys. I am looking to enhance my skills... Read More

Relevant Projects

Create Your First Chatbot with RASA NLU Model and Python
Learn the basic aspects of chatbot development and open source conversational AI RASA to create a simple AI powered chatbot on your own.

Linear Regression Model Project in Python for Beginners Part 2
Machine Learning Linear Regression Project for Beginners in Python to Build a Multiple Linear Regression Model on Soccer Player Dataset.

Recommender System Machine Learning Project for Beginners-2
Recommender System Machine Learning Project for Beginners Part 2- Learn how to build a recommender system for market basket analysis using association rule mining.

BERT Text Classification using DistilBERT and ALBERT Models
This Project Explains how to perform Text Classification using ALBERT and DistilBERT

Time Series Forecasting with LSTM Neural Network Python
Deep Learning Project- Learn to apply deep learning paradigm to forecast univariate time series data.

Demand prediction of driver availability using multistep time series analysis
In this supervised learning machine learning project, you will predict the availability of a driver in a specific area by using multi step time series analysis.

Natural language processing Chatbot application using NLTK for text classification
In this NLP AI application, we build the core conversational engine for a chatbot. We use the popular NLTK text classification library to achieve this.

Build a Credit Default Risk Prediction Model with LightGBM
In this Machine Learning Project, you will build a classification model for default prediction with LightGBM.

Hands-On Approach to Master PyTorch Tensors with Examples
In this deep learning project, you will learn how to perform various operations on the building block of PyTorch : Tensors.

Build a Multi-Class Classification Model in Python on Saturn Cloud
In this machine learning classification project, you will build a multi-class classification model in Python on Saturn Cloud to predict the license status of a business.