[R] rbind for matrices - rep argument

Carlos J. Gil Bellosta cgb at datanalytics.com
Wed Jan 7 17:13:13 CET 2009


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.




More information about the R-help mailing list