If you’ve ever written code that generates a whole whack of files, you may have came across the following problem when processing them. Using a naming convention wherein files are numbered will gum up any ordering which is based on string sorting (ls, for example). What you end up with is something like this:
results10.txt results11.txt results12.txt results1.txt results2.txt ...
Which is just no good, no good at all. A solution to this is to pad the file number with zeros, like so:
results0001.txt results0002.txt ... results0010.txt
I wrote a little function to make this easy:
pad_int<-function(n,scale){ out_string<-paste(10*scale + n,sep='') out_string<-substr(out_string,2,nchar(out_string)) return(out_string) }
*EDIT: Very soon after posting this, ggplot creator and general rstats rockstar Hadley Wickham noted on twitter that you can do this in one line using:
sprintf(“%03d”, 1)
Then, simply pass this function your file number (n) and the number of zeros that you’d like to pad it with (scale). This should be the order of magnitude of the number of files you’re creating. For example:
for(i in 1:1000){ padded<-pad_int(i,1000) file_name<-paste('results_',padded,'.txt',sep='') print(file_name) }
This little bit of code came in quite handy in generating this video of dispersal on a discrete lattice. Enjoy!
Very soon after posting this, Hadley Wickham noted on twitter that you can do this in one line using:
sprintf(“%03d”, 1)
Looks like I reinvented the wheel on this one!
Me too!
http://stevencarlislewalker.wordpress.com/2012/08/13/create-numbered-names-that-will-be-alphanumerically-sorted-as-expected-in-r/
Ha! And that was recent, too. I must have missed it. An example of multiple (re)disovery? http://en.wikipedia.org/wiki/Multiple_discovery
sprintf(‘name_%02d’, 1:15) also works
I also used this technique but had multiple sections to the video (and did voice over that required some timing). To keep the sections straight (and allow each section to be updated) I’d append a letter in front.
So the first section was sprintf(“plots/a%03d.png”, i)
second section was sprintf(“plots/b%03d.png”, i)
(the “i” counter can continue or restart too)
I ended up with 5 sections and I could independently rerun any of the sections if I wanted to tweak (or add frames) to that section. Made it so I didn’t have to recreate all the slides if I wanted to extend or modify a section.
Output is here (with multiple sections):
Very cool. I particularly like the instructive intro animation part which prompts the viewer with what to expect. Thanks for sharing!
Is this how this section is supposed to work:
pad_int<-function(n,scale){
+ out_string<-paste(10*scale + n,sep='')
+ out_string pad_int(87,3)
[1] “17”