Hi everyone, I am trying to accomplish a small task that is giving me quite a headache. I would like to automatically generate a series of matrices and give them successive names. Here is what I thought at first: t1<-matrix(0, nrow=250, ncol=1) for(i in 1:10){ t1[i]<-rnorm(250) } What I intended was that the loop would create 10 different matrices with a single column of 250 values randomly selected from a normal distribution, and that they would be labeled t11, t12, t13, t14 etc. Can anyone steer me in the right direction with this one? Thanks! Brendan
Dear Brendan, One way could be either bigt <- sapply(1:10,function(x) rnorm(250)) colnames(bigt) <- paste('t',1:10,sep="") bigt or bigt2 <- NULL for(i in 1:10) bigt2 <- cbind( bigt2, rnorm(250) ) colnames(bigt2) <- paste('t',1:10,sep="") bigt2 or bigt3 <- matrix(rnorm(250*10),ncol=10) colnames(bigt3) <- paste('t',1:10,sep="") bigt3 HTH, Jorge On Fri, Apr 17, 2009 at 10:12 PM, Brendan Morse <morse.brendan@gmail.com>wrote:> Hi everyone, I am trying to accomplish a small task that is giving me quite > a headache. I would like to automatically generate a series of matrices and > give them successive names. Here is what I thought at first: > > t1<-matrix(0, nrow=250, ncol=1) > > for(i in 1:10){ > t1[i]<-rnorm(250) > } > > What I intended was that the loop would create 10 different matrices with a > single column of 250 values randomly selected from a normal distribution, > and that they would be labeled t11, t12, t13, t14 etc. > > Can anyone steer me in the right direction with this one? > > Thanks! > Brendan > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]
Brendan Morse wrote:> > Hi everyone, I am trying to accomplish a small task that is giving me > quite a headache. I would like to automatically generate a series of > matrices and give them successive names. Here is what I thought at > first: > > t1<-matrix(0, nrow=250, ncol=1) > > for(i in 1:10){ > t1[i]<-rnorm(250) > } > > What I intended was that the loop would create 10 different matrices > with a single column of 250 values randomly selected from a normal > distribution, and that they would be labeled t11, t12, t13, t14 etc. >I think you're basically looking for http://cran.r-project.org/doc/FAQ/R-FAQ.html#How-can-I-turn-a-string-into-a-variable_003f or http://wiki.r-project.org/rwiki/doku.php?id=tips:data-misc:create_var_names but see the comments in both places that indicate why it may be easier to do this as a list. for(i in 1:10){ assign(paste("t1",i,sep=""),matrix(rnorm(250))) } -- View this message in context: http://www.nabble.com/Loop-question-tp23108752p23108788.html Sent from the R help mailing list archive at Nabble.com.
Brendan, Matrix is atomic. Once you define t1 in matrix, t1[1]=0 rather than the whole column. I would just convert t1 to a data frame, which is a special list, by adding t1<- data.frame(t1). Now t1[1] represents the whole column. Then you can use your loop to add more columns. Jun On Fri, Apr 17, 2009 at 9:12 PM, Brendan Morse <morse.brendan@gmail.com>wrote:> Hi everyone, I am trying to accomplish a small task that is giving me quite > a headache. I would like to automatically generate a series of matrices and > give them successive names. Here is what I thought at first: > > t1<-matrix(0, nrow=250, ncol=1) > > for(i in 1:10){ > t1[i]<-rnorm(250) > } > > What I intended was that the loop would create 10 different matrices with a > single column of 250 values randomly selected from a normal distribution, > and that they would be labeled t11, t12, t13, t14 etc. > > Can anyone steer me in the right direction with this one? > > Thanks! > Brendan > > ______________________________________________ > R-help@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. >-- Jun Shen PhD PK/PD Scientist BioPharma Services Millipore Corporation 15 Research Park Dr. St Charles, MO 63304 Direct: 636-720-1589 [[alternative HTML version deleted]]
On Fri, Apr 17, 2009 at 10:12 PM, Brendan Morse <morse.brendan at gmail.com> wrote:> ...I would like to automatically generate a series of matrices and > give them successive names. Here is what I thought at first: > > t1<-matrix(0, nrow=250, ncol=1) > > for(i in 1:10){ > ? ? ? ?t1[i]<-rnorm(250) > } > > What I intended was that the loop would create 10 different matrices with a > single column of 250 values randomly selected from a normal distribution, > and that they would be labeled t11, t12, t13, t14 etc.Very close. But since you've started out with a *matrix* t1, your assignments to t1[i] will assign to parts of the matrix. To correct this, all you need to do is initialize t1 as a *list of matrices* or (even better) as an *empty list*, like this: t1 <- list() and then assign to *elements* of the list (using [[ ]] notation), not to *sublists* of the list (which is what [ ] notation means in R), like this: for(i in 1:10){ t1[[i]] <- rnorm(250) } Is that what you had in mind? -s