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" <[EMAIL PROTECTED]> To: "sun" <[EMAIL PROTECTED]> Cc: <[EMAIL PROTECTED]> 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 <[EMAIL PROTECTED]> 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@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@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.