How to Merge Multiple Data Frames Into One in R
Sometimes we split one dataset into multiple sets and in the same way we merge multiple datasets into one. As a data scientist these are the common tasks in our day to day life.
I have one data frame name titanic_train_ds with 12 variables and 891 observations. Let’s see the data frame at a glance:
str(titanic_train_ds) 'data.frame': 891 obs. of 12 variables: $ PassengerId: int 1 2 3 4 5 6 7 8 9 10 ... $ Survived : int 0 1 1 1 0 0 0 0 1 1 ... $ Pclass : int 3 1 3 1 3 3 1 3 3 2 ... $ Name : chr "Braund, Mr. Owen Harris" "Cumings, Mrs. John Bradley (Florence Briggs Thayer)" "Heikkinen, Miss. Laina" "Futrelle, Mrs. Jacques Heath (Lily May Peel)" ... $ Sex : chr "male" "female" "female" "female" ... $ Age : num 22 38 26 35 35 NA 54 2 27 14 ... $ SibSp : int 1 1 0 1 0 0 0 3 0 1 ... $ Parch : int 0 0 0 0 0 0 0 1 2 0 ... $ Ticket : chr "A/5 21171" "PC 17599" "STON/O2. 3101282" "113803" ... $ Fare : num 7.25 71.28 7.92 53.1 8.05 ... $ Cabin : chr "" "C85" "" "C123" ... $ Embarked : chr "S" "C" "S" "S" ...
I have another data frame named titanic_test_ds with 12 variables and 418 observations. Let’s see the data frame at a glance
str(titanic_test_ds) 'data.frame': 418 obs. of 12 variables: $ PassengerId: int 892 893 894 895 896 897 898 899 900 901 ... $ Survived : int 0 1 1 1 0 0 0 0 1 1 ... $ Pclass : int 3 3 2 3 3 3 3 2 3 3 ... $ Name : chr "Kelly, Mr. James" "Wilkes, Mrs. James (Ellen Needs)" "Myles, Mr. Thomas Francis" "Wirz, Mr. Albert" ... $ Sex : chr "male" "female" "male" "male" ... $ Age : num 34.5 47 62 27 22 14 30 26 18 21 ... $ SibSp : int 0 1 0 0 1 0 0 1 0 2 ... $ Parch : int 0 0 0 0 1 0 0 1 0 0 ... $ Ticket : chr "330911" "363272" "240276" "315154" ... $ Fare : num 7.83 7 9.69 8.66 12.29 ... $ Cabin : chr "" "" "" "" ... $ Embarked : chr "Q" "S" "Q" "S" ...
Both data frame have same number of columns, similar number of data types but different numbers of observations. We wanted to combined these two data frames into one named titanic_full_ds. So, let’s see how to combine or merge these two data frames into one in R:
library("dplyr") titanic_full_ds<-bind_rows(titanic_train_ds,titanic_test_ds)
Hope this above code will help you so much. If you feel that you have benefited with this contribution don't forget to like our facebook page http://www.facebook.com/learningbigdataanalytics/.