[R] Loop question

Ben Bolker bolker at ufl.edu
Sat Apr 18 04:23:44 CEST 2009




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.




More information about the R-help mailing list