How to merge multiple DataFrames in R ?

30 Apr, 2021
In this article, we will discuss how to merge multiple dataframes in R Programming Language. Dataframes can be merged both row and column wise, we can merge the columns by using cbind() function and rows by using rbind() function
cbind() is used to combine the dataframes by columns.
Syntax:
cbind(data1,data2,..............,data n)
Parameters:
where data1 and data 2 are the data frames.
Example 1:
R
# vector with student detailsnames1=c("sravan","bobby","ojaswi")  # vector with marksmarks1=c(90,89,78)  # pass these vectors to the # dataframe 1data1=data.frame(names1=names1,marks1=marks1)print(data1)  # vector with student detailsnames2=c("gnanesh","rohith","divya")  # vector with marksmarks2=c(68,99,79)  # pass these vectors to the dataframe 2data2=data.frame(names2=names2,marks2=marks2)print(data2)  print("-------------------------------\-------------------------------")  # merging these two dataframes using cbindprint(cbind(data1,data2))
Output:
Example 2:
We can also merge specific columns in each dataframe by using $ operator we can access the dataframe column
Syntax:
dataframe_name$columnname
R
# vector with student detailsnames1=c("sravan","bobby","ojaswi")  # vector with marksmarks1=c(90,89,78)  # pass these vectors to the# dataframe 1data1=data.frame(names1=names1,marks1=marks1)print(data1)  # vector with student detailsnames2=c("gnanesh","rohith","divya")  # vector with marksmarks2=c(68,99,79)  # pass these vectors to the dataframe 2data2=data.frame(names2=names2,marks2=marks2)print(data2)  print("---------------------------------\-----------------------------")  # merging these two data frames marks# column using cbindprint(cbind(data1$marks1,data2$marks2))
Output:
Example 3:
R
# vector with student detailsnames1=c("sravan","bobby","ojaswi")  # vector with marksmarks1=c(90,89,78)  # pass these vectors to the # dataframe 1data1=data.frame(names1=names1,marks1=marks1)print(data1)  # vector with student detailsnames2=c("gnanesh","rohith","divya")  # vector with marksmarks2=c(68,99,79)  # pass these vectors to the dataframe 2data2=data.frame(names2=names2,marks2=marks2)print(data2)  # vector with student detailsnames3=c("bhavya","harsha","navya")  # vector with marksmarks3=c(68,99,79)  # pass these vectors to the dataframe 3data3=data.frame(names3=names3,marks3=marks3)print(data3)  print("-----------------------------------\---------------------------")  # merging these three data framesprint(cbind(data1,data2,data3))
Output:
We can merge the rows between the dataframes using rbind() function.
Syntax:
rbind(dataframe1,dataframe2)
Example 1:
R
# create vectorsx=c(1,2,3,4,3,4,5)y=c(4,5,6,7,2,3,4)  # pass these vectors to the # input of dataframe1(a)a=data.frame(x,y)  # create vectorsx=c(10,20,30,40,50,60,70)y=c(40,50,60,40,50,60,70)  # pass these vectors to the # input of dataframe1(a)b=data.frame(x,y)  # apply rbind function to # merge rowsprint(rbind(a,b))
Output:
Example 2:
R
# create vectorsx=c(1,2,3,4,3,4,5)y=c(4,5,6,7,2,3,4)  # pass these vectors to the # input of dataframe1(a)a=data.frame(x,y)  # display dataframeprint(a)print("---------------------")  # create vectorsx=c("sravan","bobby")y=c("Eswar","sai")  # pass these vectors to the # input of dataframe1(a)b=data.frame(x,y)  # display dataframeprint(b)  # apply rbind function to merge rowsprint(rbind(a,b))
Output:
Picked
R DataFrame-Programs
R-DataFrame
R Language
R Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Filter data by multiple conditions in R using Dplyr
Change Color of Bars in Barchart using ggplot2 in R
Loops in R (for, while, repeat)
How to Split Column Into Multiple Columns in R DataFrame?
Group by function in R using Dplyr
How to Split Column Into Multiple Columns in R DataFrame?
How to change Row Names of DataFrame in R ?
How to filter R DataFrame by values in a column?
Replace Specific Characters in String in R
Remove rows with NA in one column of R DataFrame