R Program to Merge Multiple Dataframes

Here,

  1. rbind() - combines data frames vertically
  2. cbind() - combines data frames horizontally

Example: Combine Dataframe Vertically Using rbind() in R

If we want to combine two data frames vertically, the column name of the two data frames must be the same. For example,

dataframe1 = data.frame(Id = c(101, 102, 103, 104, 105),
  Course = c("DSA", "Graphics", "Calculus-1", "Physics", "Network")
)

dataframe2 = data.frame(Id = c(101, 103, 105, 108, 110),
  Address = c("Nepal", "USA","Sweden", "Japan", "Norway")
)

# merge two dataframes based on Id
merge(dataframe1,dataframe2, by = "Id")

Output

  Id     Course Address
1 101        DSA   Nepal
2 103 Calculus-1     USA
3 105    Network  Sweden

In the above example, we have created two dataframes named: dataframe1 and dataframe2. We have then used the merge() function to merge two dataframes.

Here, two dataframes whose Id value is the same are merged together.

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges