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ò [[alternative HTML version deleted]]
you can use indexing, e.g., mat <- matrix(rnorm(20), 4, 5) mat mat[rep(1:nrow(mat), 3), ] I hope it helps. Best, Dimitris 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? > > [[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.-- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014
Try this: m[rep(seq_len(nrow(m)), 3),] On Wed, Jan 7, 2009 at 1:22 PM, Niccolò Bassani <biostatistica@gmail.com>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ò > > [[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. > >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
On Wed, 2009-01-07 at 16:22 +0100, 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!Hello, If your matrix is kk <- matrix( 1:16, 4, 4) You can do kkk <- lapply( 1:5, function(x) kk ) do.call(rbind, kkk) You can write your code in a single line, though. I used 5 here as a matter of example. You can build a function on these lines with an arbitrary argument if need be. Carlos J. Gil Bellosta http://www.datanalytics.com> Niccol > > [[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.
Dimitris Rizopoulos <d.rizopoulos <at> erasmusmc.nl> writes:> > you can use indexing, e.g., > > mat <- matrix(rnorm(20), 4, 5) > > mat > mat[rep(1:nrow(mat), 3), ] > > I hope it helps. > > Best, > Dimitrisor matrix trickery: z <- as.data.frame(matrix(sample(1:9,20,replace=TRUE),nrow=4)) matrix(rep(as.matrix(z),3),ncol=ncol(z),byrow=TRUE) I do sometimes wish there were a more generic Rep() defined as Rep <- function(x,n) { result <- list() for (i in 1:n) { result[[i]] <- x } result } then one could say do.call("rbind",Rep(x,3))
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.Harding at manchester.ac.uk> Fax-to-email: +44 (0)870 094 0861 Date: 07-Jan-09 Time: 18:08:14 ------------------------------ XFMail ------------------------------