Hi guys I have vectors x <- c(1,2,3,4) and y <- c(4,3,9) and would like to generate a matrix which has 3 rows (length(y)) and 4 columns (length(x)), and each row is the corresponding y element repeated length(x) times. 4,4,4,4 3,3,3,3 9,9,9,9 Thanks. Fernando ?lvarez
mat <- matrix(ncol = length(x), nrow = length(y)) for(i in 1:length(x)) { mat[,i] = y} HTH, Samuel -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of fernando.cabrera at nordea.com Sent: 05 October 2011 17:11 To: r-help at r-project.org Subject: [R] Populate a matrix Hi guys I have vectors x <- c(1,2,3,4) and y <- c(4,3,9) and would like to generate a matrix which has 3 rows (length(y)) and 4 columns (length(x)), and each row is the corresponding y element repeated length(x) times. 4,4,4,4 3,3,3,3 9,9,9,9 Thanks. Fernando ?lvarez ______________________________________________ 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. __________ Information from ESET NOD32 Antivirus, version of virus signature database 6275 (20110707) __________ The message was checked by ESET NOD32 Antivirus. http://www.eset.com __________ Information from ESET NOD32 Antivirus, version of virus signature database 6275 (20110707) __________ The message was checked by ESET NOD32 Antivirus. http://www.eset.com
matrix(rep(y, each=length(x)), nrow=length(y), byrow=TRUE) or less explicitly matrix(y, nrow=length(y),ncol=length(x)) Michael On Wed, Oct 5, 2011 at 12:11 PM, <fernando.cabrera at nordea.com> wrote:> Hi guys > > I have vectors x <- c(1,2,3,4) and y <- c(4,3,9) and would like to generate a matrix which has 3 rows (length(y)) and 4 columns (length(x)), and each row is the corresponding y element repeated length(x) times. > > 4,4,4,4 > 3,3,3,3 > 9,9,9,9 > > Thanks. > > Fernando ?lvarez > > ______________________________________________ > 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. >
One more version: somewhere in the middle of the explicitness scale, matrix(rep(y, times = length(x)), nrow=length(y)) On Wed, Oct 5, 2011 at 12:17 PM, R. Michael Weylandt <michael.weylandt at gmail.com> wrote:> matrix(rep(y, each=length(x)), nrow=length(y), byrow=TRUE) > or less explicitly > ?matrix(y, nrow=length(y),ncol=length(x)) > Michael > > On Wed, Oct 5, 2011 at 12:11 PM, ?<fernando.cabrera at nordea.com> wrote: >> Hi guys >> >> I have vectors x <- c(1,2,3,4) and y <- c(4,3,9) and would like to generate a matrix which has 3 rows (length(y)) and 4 columns (length(x)), and each row is the corresponding y element repeated length(x) times. >> >> 4,4,4,4 >> 3,3,3,3 >> 9,9,9,9 >> >> Thanks. >> >> Fernando ?lvarez >> >> ______________________________________________ >> 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. >> >
m <- matrix( rep( y, length( x ) ), length( y ), length( x ) ) On Wednesday 05 October 2011 18:11:18 fernando.cabrera at nordea.com wrote:> Hi guys > > I have vectors x <- c(1,2,3,4) and y <- c(4,3,9) and would like to generate a matrix which has 3 rows (length(y)) and 4columns (length(x)), and each row is the corresponding y element repeated length(x) times.> > 4,4,4,4 > 3,3,3,3 > 9,9,9,9 > > Thanks. > > Fernando ?lvarez > > ______________________________________________ > 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.
This last solution is what I was looking for, I was trying to avoid loops. Thanks! -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Rainer Schuermann Sent: 5. oktober 2011 18:29 To: r-help at r-project.org Subject: Re: [R] Populate a matrix m <- matrix( rep( y, length( x ) ), length( y ), length( x ) ) On Wednesday 05 October 2011 18:11:18 fernando.cabrera at nordea.com wrote:> Hi guys > > I have vectors x <- c(1,2,3,4) and y <- c(4,3,9) and would like to generate a matrix which has 3 rows (length(y)) and 4columns (length(x)), and each row is the corresponding y element repeated length(x) times.> > 4,4,4,4 > 3,3,3,3 > 9,9,9,9 > > Thanks. > > Fernando ?lvarez > > ______________________________________________ > 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.______________________________________________ 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.