I have two data frame A and B adn want to cross them. A has format as: a1 a2 a3 1 2 3 2 3 1 1 3 2 ... B: b1 b2 1 2 2 1 ... the combine result shall be something like a1 a2 a3 b1 b2 1 2 3 1 2 1 2 3 2 1 2 3 1 1 2 2 3 1 2 1 1 3 2 1 2 1 3 2 2 1 .... is there a function able of doing this instead of loops? Thanks, Sun
Try this: merge(B, A) On 13/11/2007, sun <flyhyena at yahoo.com.cn> wrote:> I have two data frame A and B adn want to cross them. > A has format as: > > a1 a2 a3 > 1 2 3 > 2 3 1 > 1 3 2 > ... > > B: > > b1 b2 > 1 2 > 2 1 > ... > > the combine result shall be something like > > a1 a2 a3 b1 b2 > 1 2 3 1 2 > 1 2 3 2 1 > 2 3 1 1 2 > 2 3 1 2 1 > 1 3 2 1 2 > 1 3 2 2 1 > .... > > > is there a function able of doing this instead of loops? > > Thanks, > Sun > > ______________________________________________ > 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. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O
Try this:> A <- data.frame(a1 = c(1, 2, 1), a2 = c(2, 3, 3), a3 = c(3, 1, 2)) > B <- data.frame(b1 = 1:2, b2 = 2:1) > > library(sqldf) > sqldf("select * from A, B")a1 a2 a3 b1 b2 1 1 2 3 1 2 2 1 2 3 2 1 3 2 3 1 1 2 4 2 3 1 2 1 5 1 3 2 1 2 6 1 3 2 2 1 On Nov 13, 2007 6:49 AM, sun <flyhyena at yahoo.com.cn> wrote:> I have two data frame A and B adn want to cross them. > A has format as: > > a1 a2 a3 > 1 2 3 > 2 3 1 > 1 3 2 > ... > > B: > > b1 b2 > 1 2 > 2 1 > ... > > the combine result shall be something like > > a1 a2 a3 b1 b2 > 1 2 3 1 2 > 1 2 3 2 1 > 2 3 1 1 2 > 2 3 1 2 1 > 1 3 2 1 2 > 1 3 2 2 1 > .... > > > is there a function able of doing this instead of loops? > > Thanks, > Sun > > ______________________________________________ > 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. >
try this x<-c(1,2,3,2,3,1,1,3,2) A<-matrix(x,3,3,byrow=T) B<-matrix(c(1,2,2,1),2,2) A1<-matrix(rep(A,each=2),6,3) B1<-rbind(B,B,B) data.frame(A1,B1) X1 X2 X3 X1.1 X2.1 1 1 2 3 1 2 2 1 2 3 2 1 3 2 3 1 1 2 4 2 3 1 2 1 5 1 3 2 1 2 6 1 3 2 2 1 2007/11/13, sun <flyhyena at yahoo.com.cn>:> I have two data frame A and B adn want to cross them. > A has format as: > > a1 a2 a3 > 1 2 3 > 2 3 1 > 1 3 2 > ... > > B: > > b1 b2 > 1 2 > 2 1 > ... > > the combine result shall be something like > > a1 a2 a3 b1 b2 > 1 2 3 1 2 > 1 2 3 2 1 > 2 3 1 1 2 > 2 3 1 2 1 > 1 3 2 1 2 > 1 3 2 2 1 > .... > > > is there a function able of doing this instead of loops? > > Thanks, > Sun > > ______________________________________________ > 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. >
Thanks all for the answers. Both Merge and sqldf works perfectly for me. Well, I feel sqldf run a littile bit slower. And I failed to install this package (sqldf ) on my linux box. Denver, your approach also works, but in my case, data frame A has much more rows then B, so B has to be duplicated many many times. kind regards, Sun ----- Original Message ----- From: "Gabor Grothendieck" <ggrothendieck at gmail.com> To: "sun" <flyhyena at yahoo.com.cn> Cc: <r-help at stat.math.ethz.ch> Sent: Tuesday, November 13, 2007 6:07 PM Subject: Re: [R] combine two dataframe> Try this: > >> A <- data.frame(a1 = c(1, 2, 1), a2 = c(2, 3, 3), a3 = c(3, 1, 2)) >> B <- data.frame(b1 = 1:2, b2 = 2:1) >> >> library(sqldf) >> sqldf("select * from A, B") > a1 a2 a3 b1 b2 > 1 1 2 3 1 2 > 2 1 2 3 2 1 > 3 2 3 1 1 2 > 4 2 3 1 2 1 > 5 1 3 2 1 2 > 6 1 3 2 2 1 > > > On Nov 13, 2007 6:49 AM, sun <flyhyena at yahoo.com.cn> wrote: >> I have two data frame A and B adn want to cross them. >> A has format as: >> >> a1 a2 a3 >> 1 2 3 >> 2 3 1 >> 1 3 2 >> ... >> >> B: >> >> b1 b2 >> 1 2 >> 2 1 >> ... >> >> the combine result shall be something like >> >> a1 a2 a3 b1 b2 >> 1 2 3 1 2 >> 1 2 3 2 1 >> 2 3 1 1 2 >> 2 3 1 2 1 >> 1 3 2 1 2 >> 1 3 2 2 1 >> .... >> >> >> is there a function able of doing this instead of loops? >> >> Thanks, >> Sun >> >> ______________________________________________ >> 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. >>
On Nov 14, 2007 4:59 AM, sun <flyhyena at yahoo.com.cn> wrote:> Thanks all for the answers. Both Merge and sqldf works perfectly for me. > Well, I feel sqldf run a littile bit slower. And I failed to install this > package (sqldf ) on my linux box.Have never heard of anyone not being able to install sqldf on Linux before. sqldf is written in 100% R so it should run on all platforms R runs on and for which its dependencies work, mainly RSQLite (or RMySQL). As mentioned on the home page, http://sqldf.googlecode.com, sqldf is optimized for convenience, not speed, so I would not think it would be the fastest.> > Denver, your approach also works, but in my case, data frame A has much more > rows then B, so B has to be duplicated many many times. > > kind regards, > Sun > > > ----- Original Message ----- > From: "Gabor Grothendieck" <ggrothendieck at gmail.com> > To: "sun" <flyhyena at yahoo.com.cn> > Cc: <r-help at stat.math.ethz.ch> > Sent: Tuesday, November 13, 2007 6:07 PM > Subject: Re: [R] combine two dataframe > > > > Try this: > > > >> A <- data.frame(a1 = c(1, 2, 1), a2 = c(2, 3, 3), a3 = c(3, 1, 2)) > >> B <- data.frame(b1 = 1:2, b2 = 2:1) > >> > >> library(sqldf) > >> sqldf("select * from A, B") > > a1 a2 a3 b1 b2 > > 1 1 2 3 1 2 > > 2 1 2 3 2 1 > > 3 2 3 1 1 2 > > 4 2 3 1 2 1 > > 5 1 3 2 1 2 > > 6 1 3 2 2 1 > > > > > > On Nov 13, 2007 6:49 AM, sun <flyhyena at yahoo.com.cn> wrote: > >> I have two data frame A and B adn want to cross them. > >> A has format as: > >> > >> a1 a2 a3 > >> 1 2 3 > >> 2 3 1 > >> 1 3 2 > >> ... > >> > >> B: > >> > >> b1 b2 > >> 1 2 > >> 2 1 > >> ... > >> > >> the combine result shall be something like > >> > >> a1 a2 a3 b1 b2 > >> 1 2 3 1 2 > >> 1 2 3 2 1 > >> 2 3 1 1 2 > >> 2 3 1 2 1 > >> 1 3 2 1 2 > >> 1 3 2 2 1 > >> .... > >> > >> > >> is there a function able of doing this instead of loops? > >> > >> Thanks, > >> Sun > >> > >> ______________________________________________ > >> 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. > >> > > ______________________________________________ > 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. >
Well, I admit I did not make it very clear. it was actually not sqldf that failed to be installed, that fact is that when I install sqdf, R did not find RSQLite(I have to check ) package which sqldf depends on on my linux box. I am using R-2.5/linux. Not sure if upgrading to 2.6 may help or not. and, sqldf is quite convenient when I can still remember some basic SQL syntax. :) "Gabor Grothendieck" <ggrothendieck at gmail.com> wrote in message news:971536df0711140445l12471274w4150d4894d357f63 at mail.gmail.com...> On Nov 14, 2007 4:59 AM, sun <flyhyena at yahoo.com.cn> wrote: >> Thanks all for the answers. Both Merge and sqldf works perfectly for me. >> Well, I feel sqldf run a littile bit slower. And I failed to install this >> package (sqldf ) on my linux box. > > Have never heard of anyone not being able to install sqldf on Linux > before. sqldf > is written in 100% R so it should run on all platforms R runs on and for > which its dependencies work, mainly RSQLite (or RMySQL). As mentioned on > the > home page, http://sqldf.googlecode.com, sqldf is optimized for > convenience, > not speed, so I would not think it would be the fastest. > >> >> Denver, your approach also works, but in my case, data frame A has much >> more >> rows then B, so B has to be duplicated many many times. >> >> kind regards, >> Sun >> >> >> ----- Original Message ----- >> From: "Gabor Grothendieck" <ggrothendieck at gmail.com> >> To: "sun" <flyhyena at yahoo.com.cn> >> Cc: <r-help at stat.math.ethz.ch> >> Sent: Tuesday, November 13, 2007 6:07 PM >> Subject: Re: [R] combine two dataframe >> >> >> > Try this: >> > >> >> A <- data.frame(a1 = c(1, 2, 1), a2 = c(2, 3, 3), a3 = c(3, 1, 2)) >> >> B <- data.frame(b1 = 1:2, b2 = 2:1) >> >> >> >> library(sqldf) >> >> sqldf("select * from A, B") >> > a1 a2 a3 b1 b2 >> > 1 1 2 3 1 2 >> > 2 1 2 3 2 1 >> > 3 2 3 1 1 2 >> > 4 2 3 1 2 1 >> > 5 1 3 2 1 2 >> > 6 1 3 2 2 1 >> > >> > >> > On Nov 13, 2007 6:49 AM, sun <flyhyena at yahoo.com.cn> wrote: >> >> I have two data frame A and B adn want to cross them. >> >> A has format as: >> >> >> >> a1 a2 a3 >> >> 1 2 3 >> >> 2 3 1 >> >> 1 3 2 >> >> ... >> >> >> >> B: >> >> >> >> b1 b2 >> >> 1 2 >> >> 2 1 >> >> ... >> >> >> >> the combine result shall be something like >> >> >> >> a1 a2 a3 b1 b2 >> >> 1 2 3 1 2 >> >> 1 2 3 2 1 >> >> 2 3 1 1 2 >> >> 2 3 1 2 1 >> >> 1 3 2 1 2 >> >> 1 3 2 2 1 >> >> .... >> >> >> >> >> >> is there a function able of doing this instead of loops? >> >> >> >> Thanks, >> >> Sun >> >> >> >> ______________________________________________ >> >> 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. >> >> >> >> ______________________________________________ >> 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. >> > > ______________________________________________ > 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. >
Here is another approach:> tmp <- expand.grid( 1:nrow(B), 1:nrow(A) ) > out <- cbind( A[tmp[,2],], B[tmp[,1],] )Hope this helps, -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at intermountainmail.org (801) 408-8111> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of sun > Sent: Tuesday, November 13, 2007 4:50 AM > To: r-help at stat.math.ethz.ch > Subject: [R] combine two dataframe > > I have two data frame A and B adn want to cross them. > A has format as: > > a1 a2 a3 > 1 2 3 > 2 3 1 > 1 3 2 > ... > > B: > > b1 b2 > 1 2 > 2 1 > ... > > the combine result shall be something like > > a1 a2 a3 b1 b2 > 1 2 3 1 2 > 1 2 3 2 1 > 2 3 1 1 2 > 2 3 1 2 1 > 1 3 2 1 2 > 1 3 2 2 1 > .... > > > is there a function able of doing this instead of loops? > > Thanks, > Sun > > ______________________________________________ > 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. >