Skip to content

Save output

4 messages · Frank Schmid, vincent@7d4.com, Michael Lawrence +1 more

#
Frank Schmid a ??crit :
as a toy example :

for (i in 1:nbfiles)
  {
  fullname = paste("myfile_",i, sep="");
  write.table(intable, fullname);
  }

see
?paste
hih
#
I think this is what you're looking for:

file_name_root = "myfile"

for(i in 1:10)

	result<- of_some_function

	out_file<-paste(file_name_root, i, ".txt", sep = "")

	write.table(result, file = out_file)

}

the above code produces 10 files ("myfile1.txt" to "myfile10.txt"), each
containing whatever was calculated on that loop. If you have loops 
within loops
and you want to write during the innermost loop, remember to take this into
account like so:

file_name_root = "myfile"

for (i in 1:10)

	for(q in 1:10)

		result<- of_some_function

		out_file<-paste(file_name_root, i, q, ".txt", sep = "")

		write.table(result, file = out_file)

	}

}

That way, when "i" iterates and when "q" resets to 1, you don't write 
over your
previous output.


Quoting Frank Schmid <frank.schmid at vwi.unibe.ch>:

  
    
#
Le 03.10.2005 12:30, Frank Schmid a ??crit :
?save
?sprintf

for(i in 1:12){
 tmp <- rnorm(10) # change it with your computations
 save(tmp, file=sprintf('file%02d.RData',i))
}

Romain