Skip to content
Prev 139412 / 398506 Next

Using loop numbers in write.csv

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,