For matrices you can use kronecker: > kronecker(rep(1, 6), data.matrix(Xdf)) [,1] [,2] [,3] [,4] [1,] 1.1 2.1 3.1 4.1 [2,] 1.2 2.2 3.2 4.2 [3,] 1.1 2.1 3.1 4.1 [4,] 1.2 2.2 3.2 4.2 [5,] 1.1 2.1 3.1 4.1 [6,] 1.2 2.2 3.2 4.2 [7,] 1.1 2.1 3.1 4.1 [8,] 1.2 2.2 3.2 4.2 [9,] 1.1 2.1 3.1 4.1 [10,] 1.2 2.2 3.2 4.2 [11,] 1.1 2.1 3.1 4.1 [12,] 1.2 2.2 3.2 4.2
On Wed, Jan 7, 2009 at 1:08 PM, Ted Harding <ted.hard...@manchester.ac.uk> wrote: > On 07-Jan-09 15:22:57, Niccolò Bassani wrote: >> Dear R users,I'm facing a trivial problem, but I really can't solve it. >> I've tried a dozen of codes, but I can't get the result I want. >> The question is: I have a dataframe like this one >> >> [,1] [,2] [,3] [,4] [,5] >> [1,] 1 2 3 4 5 >> [2,] 2 5 5 4 9 >> [3,] 1 6 8 1 2 >> [4,] 8 6 4 1 5 >> >> made up of decimal numbers, of course. >> I want to append this dataframe to itself a number x of times, i.e. 3. >> That is I want a dataframe like this >> >> [,1] [,2] [,3] [,4] [,5] >> [1,] 1 2 3 4 5 >> [2,] 2 5 5 4 9 >> [3,] 1 6 8 1 2 >> [4,] 8 6 4 1 5 >> [5,] 1 2 3 4 5 >> [6,] 2 5 5 4 9 >> [7,] 1 6 8 1 2 >> [8,] 8 6 4 1 5 >> [9,] 1 2 3 4 5 >> [10,] 2 5 5 4 9 >> [11,] 1 6 8 1 2 >> [12,] 8 6 4 1 5 >> >> I'm searching for an "authomatic" way to do this (I've already used the >> rbind re-writing x times the name of the frame...), as it must enter a >> function where one argument is exactly the number x of times to repeat >> this frame. >> >> Any ideas?? >> Thanks in advance! >> Niccolò > > I don't know whether there is anywhere a ready-made function which > will implement a "rep" paramater for an rbind, but the following ad-hoc > function will do it for you efficiently (i.e. with the minimum number > of applications of the rbind() function). > > To produce a result which consists of k replicates of x, row-bound: > > > Krbind <- function(x,k){ > y <- x > if(k==1) return(x) > p <- floor(log2(k)) > for(i in (1:p)){ > z <- rbind(y,y) > y <- z > } > k <- (k - 2^p) > if(k==0) return(y) else return(rbind(y,Krbind(x,k))) > } > > ## Example: > > Xdf <- data.frame(X1=c(1.1,1.2),X2=c(2.1,2.2), > X3=c(3.1,3.2),X4=c(4.1,4.2)) > > Krbind(Xdf,6) > # X1 X2 X3 X4 > # 1 1.1 2.1 3.1 4.1 > # 2 1.2 2.2 3.2 4.2 > # 3 1.1 2.1 3.1 4.1 > # 4 1.2 2.2 3.2 4.2 > # 5 1.1 2.1 3.1 4.1 > # 6 1.2 2.2 3.2 4.2 > # 7 1.1 2.1 3.1 4.1 > # 8 1.2 2.2 3.2 4.2 > # 9 1.1 2.1 3.1 4.1 > # 10 1.2 2.2 3.2 4.2 > # 11 1.1 2.1 3.1 4.1 > # 12 1.2 2.2 3.2 4.2 > > Of course, if you're not worried by efficiency, then the simple loop > > y <- x > for(i in (1:(k-1))){y <- rbind(y,x)} > > will do it! > > Hoping this helps, > Ted. > > -------------------------------------------------------------------- > E-Mail: (Ted Harding) <ted.hard...@manchester.ac.uk> > Fax-to-email: +44 (0)870 094 0861 > Date: 07-Jan-09 Time: 18:08:14 > ------------------------------ XFMail ------------------------------ > > ______________________________________________ > 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.