How to Combine Data in R Studio using c/r bind() and merge() Functions?

How to Combine Data in R Studio using c/r bind() and merge() Functions?


I will post a method for combining the columns and rows of two datasets in R. I have created two datasets simply for this purpose.

name=c("Jack","Kate","John","Mark","Rut")
math=c(90,85,95,75,80)
eng=c(85,90,90,88,95)
avg=c(87.5,87.5,92.55,81.5,87.5)

grade=data.frame(name,math,eng,avg)
country=c("USA","Spain","France","Germany","Korea")
gender=c("Male","Female","Male","Male","Female")

info=data.frame(country,gender)

And now, let’s combine these two datasets.

newinfo=cbind(grade,info)

I have combined these two tables into one. In fact, combining columns is simple because you can just put them side by side. However, when combining rows, it is important to check if the names of each column are the same before merging to prevent data from being mixed up.

I have created two simple data tables again.

name=c("Jack","Kate","John","Mark","Rut")
math=c(90,85,95,75,80)
eng=c(85,90,90,88,95)
avg=c(87.5,87.5,92.55,81.5,87.5)

grade1=data.frame(name,math,eng,avg)
name=c("Min","Hoon","Yoon","Kim","Park")
math=c(100,80,90,88,90)
eng=c(70,95,88,92,85)
avg=c(85,87.5,89,90,87.5)

grade2=data.frame(name,math,eng,avg)

The columns in these two data tables have the same column names. Now, I want to combine the rows of these two tables to create one table. For this, I will use the rbind()

newgrade=rbind (grade1,grade2)


Now let’s learn about the merge() function. The merge() has a similar nature to the rbind(), but it combines only the overlapping results when combining rows.

Once again, I will create two simple tables.

name=c("Jack","Kate","John","Mark","Rut")
math=c(90,85,95,75,80)
eng=c(85,90,90,88,95)
avg=c(87.5,87.5,92.55,81.5,87.5)
grade=data.frame(name,math,eng,avg)

name=c("Jack","Kate","John")
country=c("USA","Spain","France")
gender=c("Male","Female","Male")
info=data.frame(name, country,gender)

In this case, there are only three people, Jack, Kate, and John, who have additional information about their country and gender. If we want to combine data for only these three people, we can use the merge()

newinfo=merge(grade,info)

Of course, in this case, we can also use the rbind() to combine the data, but a warning message will be displayed.

newinfo=rbind(grade,info)

If we also want to combine the data for Mark and Rut, we cannot do it using rbind(). In this case, we need to use the merge() with all=TRUE

newinfo=merge(grade,info, all=TRUE)

Leave a Reply

If you include a website address in the comment section, I cannot see your comment as it will be automatically deleted and will not be posted. Please refrain from including website addresses.