How to concatenate 2 dataframe in R?

How to concatenate 2 dataframe in R?

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

Relevant Projects

Ecommerce product reviews - Pairwise ranking and sentiment analysis
This project analyzes a dataset containing ecommerce product reviews. The goal is to use machine learning models to perform sentiment analysis on product reviews and rank them based on relevance. Reviews play a key role in product recommendation systems.

Machine Learning project for Retail Price Optimization
In this machine learning pricing project, we implement a retail price optimization algorithm using regression trees. This is one of the first steps to building a dynamic pricing model.

Data Science Project on Wine Quality Prediction in R
In this R data science project, we will explore wine dataset to assess red wine quality. The objective of this data science project is to explore which chemical properties will influence the quality of red wines.

Forecast Inventory demand using historical sales data in R
In this machine learning project, you will develop a machine learning model to accurately forecast inventory demand based on historical sales data.

PySpark Tutorial - Learn to use Apache Spark with Python
PySpark Project-Get a handle on using Python with Spark through this hands-on data processing spark python tutorial.

Loan Eligibility Prediction in Python using
In this loan prediction project you will build predictive models in Python using to predict if an applicant is able to repay the loan or not.

Walmart Sales Forecasting Data Science Project
Data Science Project in R-Predict the sales for each department using historical markdown data from the Walmart dataset containing data of 45 Walmart stores.

Build a Music Recommendation Algorithm using KKBox's Dataset
Music Recommendation Project using Machine Learning - Use the KKBox dataset to predict the chances of a user listening to a song again after their very first noticeable listening event.

Census Income Data Set Project - Predict Adult Census Income
Use the Adult Income dataset to predict whether income exceeds 50K yr based on census data.

Build a Similar Images Finder with Python, Keras, and Tensorflow
Build your own image similarity application using Python to search and find images of products that are similar to any given product. You will implement the K-Nearest Neighbor algorithm to find products with maximum similarity.