[R] Automatic creation of file names

(Ted Harding) Ted.Harding at nessie.mcc.ac.uk
Thu Sep 22 18:51:52 CEST 2005


On 22-Sep-05 Mike Prager wrote:
> Walter --
> 
> P.S.  The advantage of using formatC over pasting the digits (1:1000) 
> directly is that when one uses leading zeroes, as in the formatC
> example 
> shown, the resulting filenames will sort into proper order.
> 
> ...MHP
> 
> 
> You can use paste() with something like
> 
>   formatC(number,digits=0,wid=3,flag="0")
> 
> (where number is your loop index) to generate the filenames.
> 
> 
> on 9/22/2005 10:21 AM Leite,Walter said the following:
> 
>>I have a question about how to save to the hard drive the one thousand
>>datasets I generated in a simulation.
>>://www.R-project.org/posting-guide.html

For this precise question, the replies for filename creation, though
useful, have been slightly off-target. Walter may presumably want the
ilenames to be in sortable order corresponding to the numerical order
of creation, i.e. like

  file0001 file0002 ... file1000

The precise formatC specification required for this would be

  formatC(n,digits=0,wid=4,format="d",flag="0")

so that

  formatC(1,digits=0,wid=4,format="d",flag="0")
# [1] "0001" -> "file0001"

  formatC(999,digits=0,wid=4,format="d",flag="0")
# [1] "0999" -> "file0999"

  formatC(1000,digits=0,wid=4,format="d",flag="0")
# [1] "1000" -> "file1000"

The suggestions with "wid=3" would give

  formatC(999,digits=0,wid=3,format="d",flag="0")
# [1] "999" -> "file999"
  formatC(1000,digits=0,wid=3,format="d",flag="0")
# [1] "1000" -> "file1000"

which are now in the wrong order (since "file1000" sorts
alphabetically prior to "file999".

Also, if "format=d" is not specified we get things like

  formatC(100,digits=0,wid=3,flag="0")
# [1] "1e+02" -> "file1e+02"

which, while a valid filename, is on its head for sorting
(since now the exponent sorts fastest!).

Best wishes to all,
Ted.


--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 22-Sep-05                                       Time: 17:51:36
------------------------------ XFMail ------------------------------




More information about the R-help mailing list