How to concatenate 2 dataframe in R?

This recipe helps you concatenate 2 dataframe in R

Recipe Objective

Binding or concatenating rows or columns of two different dataframes is an important task to perform in data manipulation. we use rbind() and cbind() function to carry out this task on rows and columns respectively. ​

This recipe demonstrates the concatenate 2 dataframes using rbind() and cbind() functions.

  1. rbind() function combines the rows of two dataframes of equal length.
  2. cbind() function combines the columns of two dataframes of equal length.

Step 1: Creating two Dataframes

We use data.frame() function to create DataFrames

Name = c("Ram", "Fredo", "Geeta", "Jessica") rank = c(3,4,1,2) marks = c(50, 45, 95, 80) dataframe1 = data.frame(Name,rank,marks) print(dataframe1)

     Name rank marks
1     Ram    3    50
2   Fredo    4    45
3   Geeta    1    95
4 Jessica    2    80

Name = c("Suresh", "Ramesh") rank = c(6,5) marks = c(40,43) dataframe2 = data.frame(Name,rank,marks) print(dataframe2)

    Name rank marks
1 Suresh    6    40
2 Ramesh    5    43

Step 2: Using rbind() to concatenate 1 and 2

Syntax: rbind(dataframe1,dataframe2) ​

# combining the rows of the two dataframes dataframe3 = rbind(dataframe1,dataframe2) print(dataframe3)

     Name rank marks
1     Ram    3    50
2   Fredo    4    45
3   Geeta    1    95
4 Jessica    2    80
5  Suresh    6    40
6  Ramesh    5    43

Step 3: Using cbind() to concatenate 1 and 2

Syntax: cbind(dataframe1,dataframe2) ​

# combining the columns of the two dataframes dataframe4 = cbind(dataframe1,dataframe2) print(dataframe4)

     Name rank marks   Name rank marks
1     Ram    3    50 Suresh    6    40
2   Fredo    4    45 Ramesh    5    43
3   Geeta    1    95 Suresh    6    40
4 Jessica    2    80 Ramesh    5    43

What Users are saying..

profile image

Abhinav Agarwal

Graduate Student at Northwestern University
linkedin profile url

I come from Northwestern University, which is ranked 9th in the US. Although the high-quality academics at school taught me all the basics I needed, obtaining practical experience was a challenge.... Read More

Relevant Projects

Skip Gram Model Python Implementation for Word Embeddings
Skip-Gram Model word2vec Example -Learn how to implement the skip gram algorithm in NLP for word embeddings on a set of documents.

NLP Project for Beginners on Text Processing and Classification
This Project Explains the Basic Text Preprocessing and How to Build a Classification Model in Python

NLP Project on LDA Topic Modelling Python using RACE Dataset
Use the RACE dataset to extract a dominant topic from each document and perform LDA topic modeling in python.

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.

AWS MLOps Project to Deploy Multiple Linear Regression Model
Build and Deploy a Multiple Linear Regression Model in Python on AWS

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.

Build Regression Models in Python for House Price Prediction
In this Machine Learning Regression project, you will build and evaluate various regression models in Python for house price prediction.

FEAST Feature Store Example for Scaling Machine Learning
FEAST Feature Store Example- Learn to use FEAST Feature Store to manage, store, and discover features for customer churn prediction machine learning project.

Learn Object Tracking (SOT, MOT) using OpenCV and Python
Get Started with Object Tracking using OpenCV and Python - Learn to implement Multiple Instance Learning Tracker (MIL) algorithm, Generic Object Tracking Using Regression Networks Tracker (GOTURN) algorithm, Kernelized Correlation Filters Tracker (KCF) algorithm, Tracking, Learning, Detection Tracker (TLD) algorithm for single and multiple object tracking from various video clips.

End-to-End Speech Emotion Recognition Project using ANN
Speech Emotion Recognition using RAVDESS Audio Dataset - Build an Artificial Neural Network Model to Classify Audio Data into various Emotions like Sad, Happy, Angry, and Neutral