I have two data frames. Suppose the first has rows r1 r2 r3 and the second has rows R1 R2 R3 I'd like to generate the data frame: r1 R1 r1 R2 r1 R3 r2 R1 r2 R2 r2 R3 r3 R1 r3 R2 r3 R3 How would I go about doing this? I'm sure there's a clean way to do it but I find myself thinking in loops. -- Ajay Shah http://www.mayin.org/ajayshah ajayshah at mayin.org http://ajayshahblog.blogspot.com <*(:-? - wizard who doesn't know the answer.
> I have two data frames. Suppose the first has rows > > r1 > r2 > r3 > > and the second has rows > > R1 > R2 > R3 > > I'd like to generate the data frame: > > r1 R1 > r1 R2 > r1 R3 > r2 R1 > r2 R2 > r2 R3 > r3 R1 > r3 R2 > r3 R3Try: col1 <- paste("r", 1:3, sep="") col2 <- paste("R", 1:3, sep="") data.frame(rep(col1, each=3), rep(col2, times=3)) Regards, Richie. Mathematical Sciences Unit HSL ------------------------------------------------------------------------ ATTENTION: This message contains privileged and confidential inform...{{dropped:20}}
Try: expand.grid(col2, col1) On 18/03/2008, Ajay Shah <ajayshah at mayin.org> wrote:> I have two data frames. Suppose the first has rows > > r1 > r2 > r3 > > and the second has rows > > R1 > R2 > R3 > > I'd like to generate the data frame: > > r1 R1 > r1 R2 > r1 R3 > r2 R1 > r2 R2 > r2 R3 > r3 R1 > r3 R2 > r3 R3 > > How would I go about doing this? I'm sure there's a clean way to do it > but I find myself thinking in loops. > > -- > Ajay Shah http://www.mayin.org/ajayshah > ajayshah at mayin.org http://ajayshahblog.blogspot.com > <*(:-? - wizard who doesn't know the answer. > > ______________________________________________ > 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
Will something like this work? x <- data.frame(xx=c("r1", "r2", "r3"), yy=c(1,2,3)) y <- data.frame(aa=c("R1", "R2", "R3"), bb=c(10,12, 14)) merge(x,y) --- Ajay Shah <ajayshah at mayin.org> wrote:> I have two data frames. Suppose the first has rows > > r1 > r2 > r3 > > and the second has rows > > R1 > R2 > R3 > > I'd like to generate the data frame: > > r1 R1 > r1 R2 > r1 R3 > r2 R1 > r2 R2 > r2 R3 > r3 R1 > r3 R2 > r3 R3 > > How would I go about doing this? I'm sure there's a > clean way to do it > but I find myself thinking in loops. > > -- > Ajay Shah > http://www.mayin.org/ajayshah > ajayshah at mayin.org > http://ajayshahblog.blogspot.com > <*(:-? - wizard who doesn't know the answer. > > ______________________________________________ > 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. >Looking for the perfect gift? Give the gift of Flickr!