FAQ
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

Juhana Vartiainen
juhana.vartiainen at labour.fi

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

## Search Discussions

•  at Mar 21, 2002 at 10:45 am ⇧

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?
You could do something like that:

R> mymatrix <- matrix(1:9, ncol=3)
R> myindex <- rep(1:nrow(mymatrix), rep(2, nrow(mymatrix)))
R> mymatrix[myindex,]
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 1 4 7
[3,] 2 5 8
[4,] 2 5 8
[5,] 3 6 9
[6,] 3 6 9

Best,
Z
Many thanks

Juhana Vartiainen
juhana.vartiainen at labour.fi

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
•  at Mar 22, 2002 at 10:23 am ⇧
m2<-matrix(t(cbind(mymatrix,mymatrix)),ncol=dim(mymatrix)[2],byrow=T)
seems a bit faster than mymatrix[rep(1:nrow(mymatrix), rep(2,
nrow(mymatrix))),]
(not that it matters here at all), and
m2<-mymatrix[ceiling(1:(2*nrow(mymatrix))/2),]
much faster still. It seems rep( , rep()) is relatively expensive for this
job (though the difference is tiny in any sensible terms). I'm now even more
impressed at how cleverly R passes parameters.

Mike.

mymatrix <- matrix(1:9, ncol=3)
date()
[1] "Thu Mar 21 17:03:29 2002"
for (i in 1:50000) {
+ m2<-mymatrix[rep(1:nrow(mymatrix), rep(2, nrow(mymatrix))),]
+ }
m2
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 1 4 7
[3,] 2 5 8
[4,] 2 5 8
[5,] 3 6 9
[6,] 3 6 9
date()
[1] "Thu Mar 21 17:03:46 2002"
nr <- nrow(mymatrix)
for (i in 1:50000) {
+ m2<-mymatrix[rep(1:nr, rep(2, nr)),]
+ }
m2
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 1 4 7
[3,] 2 5 8
[4,] 2 5 8
[5,] 3 6 9
[6,] 3 6 9
date()
[1] "Thu Mar 21 17:04:01 2002"
for (i in 1:50000)
+ m2<-matrix(t(cbind(mymatrix,mymatrix)),ncol=dim(mymatrix)[2],byrow=T)
m2
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 1 4 7
[3,] 2 5 8
[4,] 2 5 8
[5,] 3 6 9
[6,] 3 6 9
date()
[1] "Thu Mar 21 17:04:14 2002"
for (i in 1:50000) {
+ myindex <- ceiling(1:(2*nrow(mymatrix))/2)
+ m2<-mymatrix[myindex,]
+ }
m2
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 1 4 7
[3,] 2 5 8
[4,] 2 5 8
[5,] 3 6 9
[6,] 3 6 9
date()
[1] "Thu Mar 21 17:04:19 2002"

-----Original Message-----
From: owner-r-help at stat.math.ethz.ch
[mailto:owner-r-help at stat.math.ethz.ch]On Behalf Of Achim Zeileis
Sent: 21 March 2002 10:45
To: Juhana Vartiainen
Cc: r-help at stat.math.ethz.ch
Subject: Re: [R] repeating rows or columns within a matrix
>
>
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?
>
You could do something like that: >
R> mymatrix <- matrix(1:9, ncol=3)
R> myindex <- rep(1:nrow(mymatrix), rep(2, nrow(mymatrix)))
R> mymatrix[myindex,]
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 1 4 7
[3,] 2 5 8
[4,] 2 5 8
[5,] 3 6 9
[6,] 3 6 9 >
Best,
Z
>
Many thanks
> >
Juhana Vartiainen
juhana.vartiainen at labour.fi
> >
> >
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._.
_._
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._.
_._

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
•  at Mar 21, 2002 at 10:48 am ⇧

Juhana Vartiainen wrote:
Spse I have a matrix, say ...
and I would like to expand it by repeating rows within the matrix, to
get, if the repeating factor is 2, say: ...
(or columnwise as well) . There must be a smart way of doing that?
For your particular example the following works

apply(matrix(1:9,ncol=3,byrow=T), 2, rep, rep(2,3))

You may also want to look at

?apply
?rep

Sincerely,

Diego Kuonen

--
Dr. ?s sc. Diego Kuonen CEO & Applied Statistician
Statoo Consulting, PO Box 107, 1015 Lausanne, Switzerland
+ Have you ever been Statooed? http://www.statoo.info +
+ Free Statooed newsletters: http://lists.statoo.com +
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
•  at Mar 21, 2002 at 11:01 am ⇧

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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
•  at Mar 21, 2002 at 1:21 pm ⇧

On Thu, Mar 21, 2002 at 12:19:37PM +0200, 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

Juhana Vartiainen
juhana.vartiainen at labour.fi

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

I do not know if it is a smart way, but that's a way:

m <- matrix(1:9, 3, 3, byrow=T)
rep.fact <- 2 # repeating factor, say 2
mm <- matrix(rep(m, rep(rep.fact, length(m))), nrow(m)*rep.fact, ncol(m))

Hopin' it helps,

Laurent

--
--------------------------------------------------------------
Laurent Gautier CBS, Building 208, DTU
PhD. Student D-2800 Lyngby,Denmark
tel: +45 45 25 24 85 http://www.cbs.dtu.dk/laurent
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
•  at Mar 21, 2002 at 10:30 pm ⇧
"Juhana" == Juhana Vartiainen <juhana.vartiainen@labour.fi> writes:
Juhana> Hello
Juhana> Spse I have a matrix, say

Juhana> 1 2 3
Juhana> 4 5 6
Juhana> 7 8 9

Juhana> and I would like to expand it by repeating rows within the matrix, to
Juhana> get, if the repeating factor is 2, say:

Juhana> 123
Juhana> 123
Juhana> 456
Juhana> 456
Juhana> 789
Juhana> 789

Juhana> (or columnwise as well) . There must be a smart way of doing that?

Juhana,

you could try the kronecker product
z <- matrix(1:9,ncol=3,byrow=T)
z
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
kronecker(z, c(1,1))
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 1 2 3
[3,] 4 5 6
[4,] 4 5 6
[5,] 7 8 9
[6,] 7 8 9

Cheers,

Andreas

--
Andreas Kiermeier
Senior Biometrician
GPO Box 397, Adelaide, SA 5001, Australia
Ph: +61 8 8303 6819
Fax: +61 8 8303 6761

-----------------------------------------------------------
This email message is intended only for the addressee(s)
and contains information which may be confidential and/or
do not read, save, forward, disclose, or copy the contents
of this email. If this email has been sent to you in error,
email and any copies or links to this email completely and
that this email is free of viruses. Virus scanning is
recommended and is the responsibility of the recipient.

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

## Related Discussions

Discussion Overview
 group r-help categories r posted Mar 21, '02 at 10:19a active Mar 22, '02 at 10:23a posts 7 users 7 website r-project.org irc #r

### 7 users in discussion

Content

People

Support

Translate

site design / logo © 2018 Grokbase