[R] Merge

arun smartpink111 at yahoo.com
Wed Apr 17 07:15:55 CEST 2013


Hi Farnoosh,
YOu can use either ?merge() or ?join()
DataA<- read.table(text="
ID     v1     
1     10
2     1 
3     22
4     15
5     3 
6     6 
7     8 
",sep="",header=TRUE)

DataB<- read.table(text="
ID v2
2 yes
5 no
7 yes
",sep="",header=TRUE,stringsAsFactors=FALSE)

merge(DataA,DataB,by="ID",all.x=TRUE)
#  ID v1   v2
#1  1 10 <NA>
#2  2  1  yes
#3  3 22 <NA>
#4  4 15 <NA>
#5  5  3   no
#6  6  6 <NA>
#7  7  8  yes
 library(plyr)
 join(DataA,DataB,by="ID",type="left")
#  ID v1   v2
#1  1 10 <NA>
#2  2  1  yes
#3  3 22 <NA>
#4  4 15 <NA>
#5  5  3   no
#6  6  6 <NA>
#7  7  8  yes
A.K.





________________________________
 From: farnoosh sheikhi <farnoosh_81 at yahoo.com>
To: "smartpink111 at yahoo.com" <smartpink111 at yahoo.com> 
Sent: Wednesday, April 17, 2013 12:52 AM
Subject: Merge
 


Hi Arun,

I want to merge a data set with another data frame with 2 columns and keep the sample size of the DataA.

DataA  DataB  DataCombine 
ID v1  ID V2  ID v1 v2 
1 10  2 yes  1 10 NA 
2 1  5 no  2 1 yes 
3 22  7 yes  3 22 NA 
4 15     4 15 NA 
5 3     5 3 no 
6 6     6 6 NA 
7 8     7 8 yes 


Thanks a lot for your help and time.



More information about the R-help mailing list