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

Jingwei Li

Graduate Research assistance at Stony Brook University
linkedin profile url

ProjectPro is an awesome platform that helps me learn much hands-on industrial experience with a step-by-step walkthrough of projects. There are two primary paths to learn: Data Science and Big Data.... Read More

Relevant Projects

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.

Learn to Build an End-to-End Machine Learning Pipeline - Part 2
In this Machine Learning Project, you will learn how to build an end-to-end machine learning pipeline for predicting truck delays, incorporating Hopsworks' feature store and Weights and Biases for model experimentation.

Azure Text Analytics for Medical Search Engine Deployment
Microsoft Azure Project - Use Azure text analytics cognitive service to deploy a machine learning model into Azure Databricks

AWS Project to Build and Deploy LSTM Model with Sagemaker
In this AWS Sagemaker Project, you will learn to build a LSTM model on Sagemaker for sales forecasting while analyzing the impact of weather conditions on Sales.

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.

Expedia Hotel Recommendations Data Science Project
In this data science project, you will contextualize customer data and predict the likelihood a customer will stay at 100 different hotel groups.

Learn to Build a Siamese Neural Network for Image Similarity
In this Deep Learning Project, you will learn how to build a siamese neural network with Keras and Tensorflow for Image Similarity.

Build an AI Chatbot from Scratch using Keras Sequential Model
In this NLP Project, you will learn how to build an AI Chatbot from Scratch using Keras Sequential Model.

Detectron2 Object Detection and Segmentation Example Python
Object Detection using Detectron2 - Build a Dectectron2 model to detect the zones and inhibitions in antibiogram images.

Time Series Classification Project for Elevator Failure Prediction
In this Time Series Project, you will predict the failure of elevators using IoT sensor data as a time series classification machine learning problem.