Hi, R-listers, Failed, when I tried to merge df1 and df2 by "codetot" in df1 and "codetoto" in df2. I want to know the reason and how to merge them together. Data frames and codes I have used were listed as followed. Thanks a lot in advance. df1: popcode codetot p3need BCPy01-01 BCPy01-01-1 100.0000 BCPy01-01 BCPy01-01-2 100.0000 BCPy01-01 BCPy01-01-3 100.0000 BCPy01-02 BCPy01-02-1 92.5926 BCPy01-02 BCPy01-02-1 100.0000 BCPy01-02 BCPy01-02-2 92.5926 BCPy01-02 BCPy01-02-2 100.0000 BCPy01-02 BCPy01-02-3 92.5926 BCPy01-02 BCPy01-02-3 100.0000 BCPy01-03 BCPy01-03-1 100.0000 df2: popcode codetoto areasec BCPy01-01 BCPy01-01-1 0.5089434 BCPy01-01 BCPy01-01-2 0.6246381 BCPy01-01 BCPy01-01-3 0.4370059 BCPy01-02 BCPy01-02-1 0.9253052 BCPy01-02 BCPy01-02-1 0.6011810 BCPy01-02 BCPy01-02-2 0.6710850 BCPy01-02 BCPy01-02-2 0.7145839 BCPy01-02 BCPy01-02-3 0.9515256 BCPy01-02 BCPy01-02-3 0.6555006 BCPy01-03 BCPy01-03-1 0.8683875 code: new1<-merge(df1,df2,by=c("popcode","popcode"),all=T) # It is processed well. But, it is not what I want. new2<-merge(df1,df2,by=c("codetot","codetoto"),all=T) # this one failed. I exactly want to do that. Mao J-F IBCAS, AC [[alternative HTML version deleted]]
try this:> merge(df1, df2, by.x='codetot',by.y='codetoto', all=TRUE)codetot popcode.x p3need popcode.y areasec 1 BCPy01-01-1 BCPy01-01 100.0000 BCPy01-01 0.5089434 2 BCPy01-01-2 BCPy01-01 100.0000 BCPy01-01 0.6246381 3 BCPy01-01-3 BCPy01-01 100.0000 BCPy01-01 0.4370059 4 BCPy01-02-1 BCPy01-02 92.5926 BCPy01-02 0.9253052 5 BCPy01-02-1 BCPy01-02 92.5926 BCPy01-02 0.6011810 6 BCPy01-02-1 BCPy01-02 100.0000 BCPy01-02 0.9253052 7 BCPy01-02-1 BCPy01-02 100.0000 BCPy01-02 0.6011810 8 BCPy01-02-2 BCPy01-02 92.5926 BCPy01-02 0.6710850 9 BCPy01-02-2 BCPy01-02 92.5926 BCPy01-02 0.7145839 10 BCPy01-02-2 BCPy01-02 100.0000 BCPy01-02 0.6710850 11 BCPy01-02-2 BCPy01-02 100.0000 BCPy01-02 0.7145839 12 BCPy01-02-3 BCPy01-02 92.5926 BCPy01-02 0.9515256 13 BCPy01-02-3 BCPy01-02 92.5926 BCPy01-02 0.6555006 14 BCPy01-02-3 BCPy01-02 100.0000 BCPy01-02 0.9515256 15 BCPy01-02-3 BCPy01-02 100.0000 BCPy01-02 0.6555006 16 BCPy01-03-1 BCPy01-03 100.0000 BCPy01-03 0.8683875>On Thu, Apr 9, 2009 at 7:19 AM, Mao Jianfeng <jianfeng.mao at gmail.com> wrote:> Hi, R-listers, > > Failed, when I tried to merge df1 and df2 by "codetot" in df1 and "codetoto" > in df2. I want to know the reason and how to merge them together. Data > frames and codes I have used were listed as followed. Thanks a lot in > advance. > > df1: > popcode ? ? codetot ? p3need > BCPy01-01 BCPy01-01-1 100.0000 > BCPy01-01 BCPy01-01-2 100.0000 > BCPy01-01 BCPy01-01-3 100.0000 > BCPy01-02 BCPy01-02-1 ?92.5926 > BCPy01-02 BCPy01-02-1 100.0000 > BCPy01-02 BCPy01-02-2 ?92.5926 > BCPy01-02 BCPy01-02-2 100.0000 > BCPy01-02 BCPy01-02-3 ?92.5926 > BCPy01-02 BCPy01-02-3 100.0000 > BCPy01-03 BCPy01-03-1 100.0000 > > df2: > popcode ? ?codetoto ? areasec > BCPy01-01 BCPy01-01-1 0.5089434 > BCPy01-01 BCPy01-01-2 0.6246381 > BCPy01-01 BCPy01-01-3 0.4370059 > BCPy01-02 BCPy01-02-1 0.9253052 > BCPy01-02 BCPy01-02-1 0.6011810 > BCPy01-02 BCPy01-02-2 0.6710850 > BCPy01-02 BCPy01-02-2 0.7145839 > BCPy01-02 BCPy01-02-3 0.9515256 > BCPy01-02 BCPy01-02-3 0.6555006 > BCPy01-03 BCPy01-03-1 0.8683875 > > code: > new1<-merge(df1,df2,by=c("popcode","popcode"),all=T) # It is processed well. > But, it is not what I want. > new2<-merge(df1,df2,by=c("codetot","codetoto"),all=T) # this one failed. I > exactly want to do that. > > Mao J-F > IBCAS, AC > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
I would suggest use the same column names for the common columns. Say, use "codetot" for both df1 and df2. Then you don't even have to specify by.x or by.y, merge will do it automatically. merge(df1,df2, all=TRUE). Otherwise you have to merge(df1,df2,by.x=c('popcode','codetot'),by.y=c('popcode','codetoto'),all=TRUE) They both produce popcode codetot p3need areasec 1 BCPy01-01 BCPy01-01-1 100 0.5089434 2 BCPy01-01 BCPy01-01-2 100 0.6246381 3 BCPy01-01 BCPy01-01-3 100 0.4370059 4 BCPy01-02 BCPy01-02-1 92.5926 0.9253052 5 BCPy01-02 BCPy01-02-1 92.5926 0.6011810 6 BCPy01-02 BCPy01-02-1 100 0.9253052 7 BCPy01-02 BCPy01-02-1 100 0.6011810 8 BCPy01-02 BCPy01-02-2 92.5926 0.6710850 9 BCPy01-02 BCPy01-02-2 92.5926 0.7145839 10 BCPy01-02 BCPy01-02-2 100 0.6710850 11 BCPy01-02 BCPy01-02-2 100 0.7145839 12 BCPy01-02 BCPy01-02-3 92.5926 0.9515256 13 BCPy01-02 BCPy01-02-3 92.5926 0.6555006 14 BCPy01-02 BCPy01-02-3 100 0.9515256 15 BCPy01-02 BCPy01-02-3 100 0.6555006 16 BCPy01-03 BCPy01-03-1 100 0.8683875 On Thu, Apr 9, 2009 at 6:19 AM, Mao Jianfeng <jianfeng.mao@gmail.com> wrote:> Hi, R-listers, > > Failed, when I tried to merge df1 and df2 by "codetot" in df1 and > "codetoto" > in df2. I want to know the reason and how to merge them together. Data > frames and codes I have used were listed as followed. Thanks a lot in > advance. > > df1: > popcode codetot p3need > BCPy01-01 BCPy01-01-1 100.0000 > BCPy01-01 BCPy01-01-2 100.0000 > BCPy01-01 BCPy01-01-3 100.0000 > BCPy01-02 BCPy01-02-1 92.5926 > BCPy01-02 BCPy01-02-1 100.0000 > BCPy01-02 BCPy01-02-2 92.5926 > BCPy01-02 BCPy01-02-2 100.0000 > BCPy01-02 BCPy01-02-3 92.5926 > BCPy01-02 BCPy01-02-3 100.0000 > BCPy01-03 BCPy01-03-1 100.0000 > > df2: > popcode codetoto areasec > BCPy01-01 BCPy01-01-1 0.5089434 > BCPy01-01 BCPy01-01-2 0.6246381 > BCPy01-01 BCPy01-01-3 0.4370059 > BCPy01-02 BCPy01-02-1 0.9253052 > BCPy01-02 BCPy01-02-1 0.6011810 > BCPy01-02 BCPy01-02-2 0.6710850 > BCPy01-02 BCPy01-02-2 0.7145839 > BCPy01-02 BCPy01-02-3 0.9515256 > BCPy01-02 BCPy01-02-3 0.6555006 > BCPy01-03 BCPy01-03-1 0.8683875 > > code: > new1<-merge(df1,df2,by=c("popcode","popcode"),all=T) # It is processed > well. > But, it is not what I want. > new2<-merge(df1,df2,by=c("codetot","codetoto"),all=T) # this one failed. I > exactly want to do that. > > Mao J-F > IBCAS, AC > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@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. >-- Jun Shen PhD PK/PD Scientist BioPharma Services Millipore Corporation 15 Research Park Dr. St Charles, MO 63304 Direct: 636-720-1589 [[alternative HTML version deleted]]