[R] repeating rows or columns within a matrix

Uwe Ligges ligges at amadeus.statistik.uni-dortmund.de
Thu Mar 21 12:01:18 CET 2002

```Juhana Vartiainen wrote:
>
> Hello
>
> Spse I have a matrix, say
>
> 1 2 3
> 4 5 6
> 7 8 9
>
> and I would like to expand it by repeating rows within the matrix, to
> get, if the repeating factor is 2, say:
>
> 123
> 123
> 456
> 456
> 789
> 789
>
> (or columnwise as well) . There must be a smart way of doing that?
>
> Many thanks

An interesting exercise! I don't know if it is the smartest way, but the
following line should do the trick for a matrix m:

matrix(apply(m, 1, function(x) rep(x, 2)), , 4, byrow=TRUE)

Uwe Ligges
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

```