We provide effective and economically affordable training courses for R and Python, click here for more details and course registration !

R provides two functions which make combine different datasets easy and feasible. rbind() is used when two datasets have same column variables and data observations are combined and expanded, whereas cbind() combines two datasets when they have the same number of rows. And merge() is used when two datasets have common column variables. The following example shows application of merge().

#show first observations of data frame 'grade'
head(grade)
#output
  StudentID    First    Last Gender Country Age Math Physics
1         1    James   Zhang   Male      US  23   73      70
2         2   Wilson      Li   Male      UK  26   95     999
3         3  Richard Nuan Ye   Male      UK  35   77      83
4         4     Mary    Deng Female      US  21   60      99
5         5    Jason  Wilson   Male      UK  19   77      89
6         6 Jennifer  Hopkin Female      UK  43   79      64
  Chemistry     Date
1        87 10/31/08
2        83 03/16/08
3        92 05/22/08
4        84 01/24/09
5        93 07/30/09
6        83 04/05/09
#create a vector
Biology<-c(72,87,92,67,99,100,68,85,74,79,34,49,58,84,89,67,78,59,97,48)
#create another data frame mdata
mdata<-data.frame(grade$StudentID, Biology)
#change names
names(mdata)<-c("StudentID","Biology")
#merge two datasets using common column 'StudentID'
test<-merge(grade, mdata, by="StudentID")
#result data frame
head(test)
#output 
StudentID    First   Last Gender Country Age Math Physics
1         1    James  Zhang   Male      US  23   73      70
2        10  Steinar Hansen   Male      UK  25   66      93
3        11  Michael   Chen   Male      UK  42   83      90
4        12    Josef Curton   Male      US  32   71      63
5        13 Jennifer  Jones   Male      US  27   79      76
6        14     Gary  Grant Female      UK  35   90      78
  Chemistry     Date Biology
1        87 10/31/08      72
2       999 08/01/08      79
3        77 10/24/08      34
4        96 11/08/09      49
5        82 10/29/08      58
6        92 10/24/08      84

Then we show an example of horizontal concatenation with cbind().

#create a vector
English<-Biology
#create a data frame
hdata<-data.frame(Biology, English)
#set names
names(hdata)<-c("Biology", "English")
#horizontally combine datasets using cbind()
test<-cbind(grade, hdata)
head(test)
#output
StudentID    First    Last Gender Country Age Math Physics
1         1    James   Zhang   Male      US  23   73      70
2         2   Wilson      Li   Male      UK  26   95     999
3         3  Richard Nuan Ye   Male      UK  35   77      83
4         4     Mary    Deng Female      US  21   60      99
5         5    Jason  Wilson   Male      UK  19   77      89
6         6 Jennifer  Hopkin Female      UK  43   79      64
  Chemistry     Date Biology English
1        87 10/31/08      72      72
2        83 03/16/08      87      87
3        92 05/22/08      92      92
4        84 01/24/09      67      67
5        93 07/30/09      99      99
6        83 04/05/09     100     100

The third example show how to vertically combine datasets using rbind()

#create a data frame
rdata<-grade[1:5,]
#vertically combine datasets using rbind()
test<-rbind(grade,rdata)
test
#output 
  StudentID    First     Last Gender Country Age Math Physics
1          1    James    Zhang   Male      US  23   73      70
2          2   Wilson       Li   Male      UK  26   95     999
3          3  Richard  Nuan Ye   Male      UK  35   77      83
4          4     Mary     Deng Female      US  21   60      99
5          5    Jason   Wilson   Male      UK  19   77      89
6          6 Jennifer   Hopkin Female      UK  43   79      64
7          7     Kari  Gjendem Female      US  37   87      99
8          8   Wenche     Dale Female      US  28   95      87
9          9     Jane   Larsen Female      US  19   73      92
10        10  Steinar   Hansen   Male      UK  25   66      93
11        11  Michael     Chen   Male      UK  42   83      90
12        12    Josef   Curton   Male      US  32   71      63
13        13 Jennifer    Jones   Male      US  27   79      76
14        14     Gary    Grant Female      UK  35   90      78
15        15     Phil      Yao   Male      UK  21   69      69
16        16     Nora   Spears Female      US  29   79      83
17        17    Goril Nordmann Female      UK  36   91      79
18        18     Lisa  Bondvik Female      US  39   65      73
19        19     Guri    Olsen Female      US  24   87      72
20        20   Martin    Jones   Male      US  25   82      73
21         1    James    Zhang   Male      US  23   73      70
22         2   Wilson       Li   Male      UK  26   95     999
23         3  Richard  Nuan Ye   Male      UK  35   77      83
24         4     Mary     Deng Female      US  21   60      99
25         5    Jason   Wilson   Male      UK  19   77      89
   Chemistry     Date
1         87 10/31/08
2         83 03/16/08
3         92 05/22/08
4         84 01/24/09
5         93 07/30/09
6         83 04/05/09
7         67 11/24/08
8         93 10/02/08
9         84 06/05/09
10       999 08/01/08
11        77 10/24/08
12        96 11/08/09
13        82 10/29/08
14        92 10/24/08
15        83 10/15/08
16        76 03/11/09
17        69 05/24/08
18        87 07/09/09
19        89 08/12/09
20        62      999
21        87 10/31/08
22        83 03/16/08
23        92 05/22/08
24        84 01/24/09
25        93 07/30/09
> 

For getting more knowledge of R and a preview of our training course, you can watch R tutorial videos on our YouTube channel !


0 Comments

Leave a Reply

Avatar placeholder