[R] Putting the same array into a matrix

Charles C. Berry cberry at tajo.ucsd.edu
Thu Oct 14 18:03:58 CEST 2010


On Thu, 14 Oct 2010, Desmond Lim wrote:

> Hi,
>
> I have an array and I want to put in into a matrix x number of times. Currently I doing this
>
> matrix <- cbind(array, array, array).
>
> Is there a more elegant way of doing this?

Fortunately!

If 'array' really is a matrix (bad choice of names here, Bub!), then

a.matrix <- matrix( rep( array, 3 ), nc = ncol( array )* 3 )

But this will work too:

a.matrix <- do.call( cbind, rep( list( array ), 3) )

even if 'array' is a data.frame

HTH,

Chuck


>
> I've tried
>
> matrix <- cbind(rep(array, times=x)) and matrix <- rep(cbind(array), times = 5)
>
> but it didn't work.
>
> Thanks.
> ______________________________________________
> 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.
>

Charles C. Berry                            (858) 534-2098
                                             Dept of Family/Preventive Medicine
E mailto:cberry at tajo.ucsd.edu	            UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/  La Jolla, San Diego 92093-0901



More information about the R-help mailing list