Re: [R] Using loop numbers in write.csv

From: Economics Guy <economics.guy_at_gmail.com>
Date: Thu, 13 Mar 2008 13:29:03 -0400

Thanks!

Double thanks to Phil, I used your guide to learn LaTeX many moons ago.

On Thu, Mar 13, 2008 at 1:26 PM, Greg Snow <Greg.Snow_at_imail.org> wrote:
>
> For filenames you can do something like:
>
> file = paste("resultsMatrix_', i, sep='')
>
> For naming objects in the workspace, there is a way, but you really
> don't want to do that. It is better to store them in a list, for
> example:
>
> resultList <- list()
>
> for( i in 1:10){
> resultList[[i]] <- matrix(runif(25, 0, 1),5, 5)
> }
>
> If you want the elements of the list named you can do:
>
> names( resultList ) <- paste('resultMatrix_',1:10, sep='')
>
> Now you have 10 matricies all grouped together in a list, you can access
> a single matrix like resultList[[1]] or resultList$resultMatrix_1, or
> you can do something to all of them using lapply or sapply. You are
> much less likely to overwrite data by accident, you can save, copy,
> load, delete, etc the whole set in 1 step rather than using a loop.
>
> Hope this helps,
>
> --
> Gregory (Greg) L. Snow Ph.D.
> Statistical Data Center
> Intermountain Healthcare
> greg.snow_at_imail.org
> (801) 408-8111
>
>
>
>
>
> > -----Original Message-----
> > From: r-help-bounces_at_r-project.org
> > [mailto:r-help-bounces_at_r-project.org] On Behalf Of Economics Guy
> > Sent: Thursday, March 13, 2008 11:07 AM
> > To: r-help_at_stat.math.ethz.ch
> > Subject: [R] Using loop numbers in write.csv
> >
> > 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.
> >
>
>

-- 
Robi Ragan
Graduate Student
Department of Economics
Department of Political Science
The University of Georgia
robi.myweb.uga.edu

______________________________________________
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.
Received on Thu 13 Mar 2008 - 17:34:56 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 13 Mar 2008 - 18:30:21 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive