Skip to content
Back to formatted view

Raw Message

Message-ID: <47D962F9.30400@biostat.wisc.edu>
Date: 2008-03-13T17:23:05Z
From: Erik Iverson
Subject: Using loop numbers in write.csv
In-Reply-To: <5dedcf4c0803131007i4703d582k12bb8abf7179d660@mail.gmail.com>

See ?paste and ?assign, those will get what you want done.

At least in the second case, you might consider using a list, however. 
You can then avoid the use of 'for' loops by using functions such as lapply.

Best,
Erik Iverson

Economics Guy wrote:
> This is a question I have wanted to ask for a while but hesitated
> because I was sut sure I would find the answer on my own, but as of
> yet...no dice.
> 
> 1) Is there a way to use the loop number in naming things in R.
> Specifically I have  a simulation that has two loops. I would like to
> be able to write out the results to a csv file after each iteration.
> 
> something like:
> 
> 
>         for (i in 1:10){
>         	
>         	exampleMatrix <-  matrix(runif(25, 0, 1),5, 5)
>         	
>         	write.csv(exampleMatrix, file = "resultsMatrix_i.csv")
>         	
>         	}
> 
> Where I would get 10 csv files named resultsMatrix_1, resultsMatrix_2
> ... resultsMatrix_10.
> 
> 
> 2) On a similar note is there a way to use the loop number when naming things.
> 
> something like:
> 
> 
>         for (i in 1:10){
>         	
>         	exampleMatrix_i <-  matrix(runif(25, 0, 1),5, 5)
>         	
>         	}
> 
> Where I would then have 10 matrices in memory with the names
> exampleMatrix_1, exampleMatrix_2 ... exampleMatrix_10.
> 
> Thanks,
> 
> EG
> 
> ______________________________________________
> 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.