[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.

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


so that

# [1] "0001" -> "file0001"

# [1] "0999" -> "file0999"

# [1] "1000" -> "file1000"

The suggestions with "wid=3" would give

# [1] "999" -> "file999"
# [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

# [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,

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