[R] merging data frames

Yasir Kaheil kaheil at gmail.com
Sat May 17 04:03:15 CEST 2008


> DF1<- data.frame(Name=as.factor(c("A","B","C")), Age= c(21,45,30))
> DF2<- data.frame(Name=as.factor(c("A","B","X")), Age= c(50,20,10))
> DF3<- data.frame(Name=as.factor(c("B","Y","K")), Age= c(40,21,30))
> 
> merge(merge(DF1,DF2, by.x= "Name", by.y="Name",
> all=TRUE),DF3,by.x="Name",by.y="Name", all=TRUE);
  Name Age.x Age.y Age
1    A    21    50  NA
2    B    45    20  40
3    C    30    NA  NA
4    X    NA    10  NA
5    K    NA    NA  30
6    Y    NA    NA  21

thanks
y

Srinivas Iyyer wrote:
> 
> Dear group, 
> I have 3 different data frames. I want to merge all 3
> data frames for which there is intersection.
> 
> Say DF 1 and DF2 has 100 common elements in Column 1. 
> 
> DF3 does not have many intersection either with DF1 or
> with DF2. 
> 
> For names in column 1 not present in DF3 I want to
> introduce NA. 
> DF1:
> Name   Age
> A       21
> B       45
> C       30
> 
> DF2:
> Name   Age
> A       50
> B       20
> X       10
> 
> DF3:
> Name   Age
> B       40
> Y       21
> K       30
> 
> I want to merge all 3 into one:
> 
> 
> Df4:
> 
> Name.1    Age.1  Age.2  Age.3
> A         21       50     NA  
> B         45       20     40
> C         30       NA     NA
> K         NA       NA     30
> X         NA       10     NA
> Y         NA       NA     21
> 
> 
> Could any one help me how can I merge 3 dataframes. 
> 
> appreciate your help. Thank you. 
> 
> srini
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
> 
> 


-----
Yasir H. Kaheil
Catchment Research Facility
The University of Western Ontario 

-- 
View this message in context: http://www.nabble.com/merging-data-frames-tp17286503p17287302.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list