[R] merge

Petr PIKAL petr.pikal at precheza.cz
Mon Apr 19 16:29:41 CEST 2010


Hi

If the columns has the same name but different values in them then you 
shall either decide which one to keep yourself or you shall keep both. If 
they have same name and same values you could select only those which 
names do not match.

names(data1) %in% names(data2)

can select which names match and you can get rid of them in one of your 
data frame before merge.

Something like that (untested)

data1[,c(1,which(!(names(data1) %in% names(data2))))]

Regards
Petr



r-help-bounces at r-project.org napsal dne 19.04.2010 15:56:34:

> What do you want to get?
> 
> And what exactly did you do?
> 
> Your question isn't very clear.
> 
> Sarah
> 
> On Mon, Apr 19, 2010 at 7:59 AM, n.vialma at libero.it <n.vialma at libero.it> 
wrote:
> > I have a problem with the merge function.
> > I have to merge two big dataframes which  look like the following 
example.The
> > problems is that I get duplicated rows.
> >
> > CODPROD       N1           N3           N4
> > 23                       3               55                 4
> > 24                       5              67                36
> > 25                      3               73                 24
> >
> >
> >
> > second data frame
> >
> >
> > CODPROD                  N1              N2
> > 30                                   34               45
> > 45                                   0                    78
> > 65                                    0                    56
> >
> >
> > The result that I get its like:
> >
> > CODPROD                 N1       N2         N3            N4      N1.1
> > 23                                 3           NA        55
> > 4             3
> > 24                                 5           NA        67
> > 36           0
> > 25                                 3           NA        73
> > 24             0
> > 30                                 34         45          NA
> > NA         0
> > 45                                  0          78          NA
> > NA           0
> > 65                                  0           56          NA
> > NA     .   0
> >
> > So N1.1 is a duplication of N1.I think I could solve the problems by
> > specifying  the same columns but I have a lot of colums which have the 
same
> > names in the two dataframe so I think its not the right way to solve 
it.
> >
> > Anyone knows how to avoid duplication??
> >
> 
> 
> -- 
> Sarah Goslee
> http://www.functionaldiversity.org
> ______________________________________________
> 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.



More information about the R-help mailing list