I have a problem with the merge function: I need to merge the data.frames that you will find as arrachmente...I try all the possible combinations....but none seems to work properly.... Does anyone knows how to do it?? thanks
I have a problem with the merge command. I have to merge two dataframe that looks like the following example: 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 should be: CODPROD N1 N2 N3 N4 23 3 NA 55 4 24 5 NA 67 36 25 3 NA 73 24 30 34 45 NA NA 45 0 78 NA NA 65 0 56 NA NA Anyone knows how to do it??
Hi r-help-bounces at r-project.org napsal dne 16.04.2010 14:00:09:> I have a problem with the merge command. > I have to merge two dataframe that looks like the following example: > > 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 should be: > > CODPROD N1 N2 N3 N4 > 23 3 NA 55 4 > 24 5 NA 67 36 > 25 3 NA 73 24 > 30 34 45 NA NA > 45 0 78 NA NA > 65 0 56 NA NAmerge(data1, data2, by="CODPROD", all=T) should work. So what does not work in your case? Regards Petr> > Anyone knows how to do it?? > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
Try this: library(plyr) rbind.fill(DF1, DF2) On Fri, Apr 16, 2010 at 8:00 AM, n.vialma at libero.it <n.vialma at libero.it> wrote:> I have a problem with the merge command. > I have to merge two dataframe that looks like the following example: > > 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 should be: > > CODPROD ? ? ? ? ? ? ? ? N1 ? ? ? N2 ? ? ? ? N3 ? ? ? ? ? ?N4 > 23 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 3 ? ? ? ? ? NA ? ? ? ?55 ? ? ? ? ? ?4 > 24 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 5 ? ? ? ? ? NA ? ? ? ?67 ? ? ? ? ? 36 > 25 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 3 ? ? ? ? ? NA ? ? ? ?73 ? ? ? ? ? 24 > 30 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 34 ? ? ? ? 45 ? ? ? ? ?NA ? ? ? ? ? NA > 45 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?0 ? ? ? ? ?78 ? ? ? ? ?NA ? ? ? ? ? NA > 65 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?0 ? ? ? ? ? 56 ? ? ? ? ?NA ? ? ? ? ? ?NA > > Anyone knows how to do it?? > > ______________________________________________ > 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. >
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??
Maybe this what you are looking for .... lines1 <- "CODPROD N1 N3 N4 23 3 55 4 24 5 67 36 25 3 73 24" df1 <- read.table(textConnection(lines1),header=TRUE) lines2 <-"CODPROD N1 N2 30 34 45 45 0 78 65 0 56" df2 <- read.table(textConnection(lines2),header=TRUE) merge(df1, df2, by = intersect(names(df1),names(df2)), all=TRUE) HTH Pete -- View this message in context: http://n4.nabble.com/merge-tp2015796p2015966.html Sent from the R help mailing list archive at Nabble.com.