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

Ray han

Tech Leader | Stanford / Yale University
linkedin profile url

I think that they are fantastic. I attended Yale and Stanford and have worked at Honeywell,Oracle, and Arthur Andersen(Accenture) in the US. I have taken Big Data and Hadoop,NoSQL, Spark, Hadoop... Read More

Relevant Projects

Recommender System Machine Learning Project for Beginners-1
Recommender System Machine Learning Project for Beginners - Learn how to design, implement and train a rule-based recommender system in Python

Time Series Analysis with Facebook Prophet Python and Cesium
Time Series Analysis Project - Use the Facebook Prophet and Cesium Open Source Library for Time Series Forecasting in Python

Ola Bike Rides Request Demand Forecast
Given big data at taxi service (ride-hailing) i.e. OLA, you will learn multi-step time series forecasting and clustering with Mini-Batch K-means Algorithm on geospatial data to predict future ride requests for a particular region at a given time.

Build Portfolio Optimization Machine Learning Models in R
Machine Learning Project for Financial Risk Modelling and Portfolio Optimization with R- Build a machine learning model in R to develop a strategy for building a portfolio for maximized returns.

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

MLOps Project for a Mask R-CNN on GCP using uWSGI Flask
MLOps on GCP - Solved end-to-end MLOps Project to deploy a Mask RCNN Model for Image Segmentation as a Web Application using uWSGI Flask, Docker, and TensorFlow.

Build Piecewise and Spline Regression Models in Python
In this Regression Project, you will learn how to build a piecewise and spline regression model from scratch in Python to predict the points scored by a sports team.

Build a Multi Touch Attribution Machine Learning Model in Python
Identifying the ROI on marketing campaigns is an essential KPI for any business. In this ML project, you will learn to build a Multi Touch Attribution Model in Python to identify the ROI of various marketing efforts and their impact on conversions or sales..

Abstractive Text Summarization using Transformers-BART Model
Deep Learning Project to implement an Abstractive Text Summarizer using Google's Transformers-BART Model to generate news article headlines.

Loan Eligibility Prediction using Gradient Boosting Classifier
This data science in python project predicts if a loan should be given to an applicant or not. We predict if the customer is eligible for loan based on several factors like credit score and past history.