[R] Merge and join data

Peter Alspach Peter.Alspach at plantandfood.co.nz
Wed Jan 20 22:31:05 CET 2010


Tena koe Sean

I suspect the apply() and merge() functions are working, but they may
not be doing what you expect :-)  You could try rbind() and aggregate():

> data.frame1$HAD <- as.numeric(NA)
> data.both <- rbind(data.frame1, data.frame2)
> aggregate(data.both[,-(1:3)], data.both[,1:3], sum, na.rm=T)
  Season   Gear Area COD POLL HAD
1 winter dredge  515 113  174  18

This assumes COD, POLL and HAD are numeric.

HTH ....

Peter Alspach  

> -----Original Message-----
> From: r-help-bounces at r-project.org 
> [mailto:r-help-bounces at r-project.org] On Behalf Of Sean M. Lucey
> Sent: Thursday, 21 January 2010 9:20 a.m.
> To: r-help at r-project.org
> Subject: [R] Merge and join data
> 
> Hi,
> 
> I'm looking to combine two data frames.  Several of the 
> columns are in common while the others need to be summed up.  
> The apply functions and the merge functions don't seem to be 
> working.  I've included a basic example of what I'm trying to 
> do below.  Thanks!
> 
> Sean
> 
> 
> data.frame1<-as.data.frame(matrix(c('winter','dredge','515',10
> 0,150),1,5))
> names(data.frame1)<-c('Season','Gear','Area','COD','POLL')
> 
> data.frame2<-as.data.frame(matrix(c('winter','dredge','515',13
> ,24,18),1,6))
> names(data.frame2)<-c('Season','Gear','Area','COD','POLL','HAD')
> 
> 
> I'd like to end up with something that looks like this:
> 
> Season   Gear   Area   COD   POLL   HAD
> winter   dredge   515    113      174      18
> 
> Thanks,
> Sean
> 
> 



More information about the R-help mailing list