Hi Gregor, There still exist simple functions to achive that goal: Look at: > x=1:111 > formatC(format="d",x,flag="0",width=ceiling(log10(max(x)))) [1] "001" "002" "003" "004" "005" "006" "007" "008" "009" "010" "011" "012" "013" "014" "015" "016" "017" "018" "019" "020" [21] "021" "022" "023" "024" "025" "026" "027" "028" "029" "030" "031" "032" "033" "034" "035" "036" "037" "038" "039" "040" [41] "041" "042" "043" "044" "045" "046" "047" "048" "049" "050" "051" "052" "053" "054" "055" "056" "057" "058" "059" "060" [61] "061" "062" "063" "064" "065" "066" "067" "068" "069" "070" "071" "072" "073" "074" "075" "076" "077" "078" "079" "080" [81] "081" "082" "083" "084" "085" "086" "087" "088" "089" "090" "091" "092" "093" "094" "095" "096" "097" "098" "099" "100" [101] "101" "102" "103" "104" "105" "106" "107" "108" "109" "110" "111" ? formatC HTH, Eric At 16:17 5/01/2005, Gregor GORJANC wrote:>Hello! > >I am producing a set of images and I would like them to be sorted by names >I give. I was able to produce my names and add integer to them. That is >easy. But my problem lies in sort of file from this process: > >figure_10.png >figure_11.png >figure_12.png >... >figure_1.png >figure_20.png >... > >So I would like to convert integers to something like 01 if upper limit for >this conert is 10 or 001 for 100. I wrote a simple function (see below), >but I do not know how this limit stuff can be imporved to work really well >with default. Any suggestions? > >int2char <- function(x, limit = max(x)) { > > # Description: > # Converts integer to character such that numbers bellow limit get 0 in > # front > # Gregor GORJANC, 2005-01-05 > > # Arguments: > # x: vector of numbers > # limit: limit up to which numbers should get 0 in front, default > # max(x) > > # Examples: > # a <- seq(0, 20, 1) > # int2char(a) # this does not work OK > # int2char(a, limit = 10) # this does work OK > > # How to: > # I would like that default would be more efficient - so it would > # recognize that let say limit 20 in example above should actually be > # 10 and so on. > > # Code: > for (i in 1:length(x)) { > if (x[i] < limit) { > n[i] <- paste("0", x[i], sep = "") > } else { > n[i] <- as.character(x[i]) > } > } > return(n) >} > > >-- >Lep pozdrav / With regards, > Gregor GORJANC > >--------------------------------------------------------------- >University of Ljubljana >Biotechnical Faculty URI: http://www.bfro.uni-lj.si >Zootechnical Department mail: gregor.gorjanc <at> bfro.uni-lj.si >Groblje 3 tel: +386 (0)1 72 17 861 >SI-1230 Domzale fax: +386 (0)1 72 17 888 >Slovenia > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide! http://www.R-project.org/posting-guide.htmlEric Lecoutre UCL / Institut de Statistique Voie du Roman Pays, 20 1348 Louvain-la-Neuve Belgium tel: (+32)(0)10473050 lecoutre at stat.ucl.ac.be http://www.stat.ucl.ac.be/ISpersonnel/lecoutre If the statistics are boring, then you've got the wrong numbers. -Edward Tufte
On 05-Jan-05 Gregor GORJANC wrote:> Hello! > > I am producing a set of images and I would like them to be > sorted by names I give. > [...] > So I would like to convert integers to something like 01 if > upper limit for this conert is 10 or 001 for 100. > [...]Hi Gregor, 'formatC' provides access to C-style formatting. For example: > formatC(5,format="d",flag="0",width=3) [1] "005" > formatC(21,format="d",flag="0",width=3) [1] "021" See "?format". Best wishes, Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk> Fax-to-email: +44 (0)870 094 0861 [NB: New number!] Date: 05-Jan-05 Time: 09:44:08 ------------------------------ XFMail ------------------------------
>>>>> "Eric" == Eric Lecoutre <lecoutre at stat.ucl.ac.be> >>>>> on Wed, 05 Jan 2005 10:29:48 +0100 writes:Eric> Hi Gregor, Eric> There still exist simple functions to achive that goal: Eric> Look at: >> x=1:111 >> formatC(format="d",x,flag="0",width=ceiling(log10(max(x)))) Eric> [1] "001" "002" "003" "004" "005" "006" "007" "008" "009" "010" "011" Eric> "012" "013" "014" "015" "016" "017" "018" "019" "020" Eric> [21] "021" "022" "023" "024" "025" "026" "027" "028" "029" "030" "031" Eric> "032" "033" "034" "035" "036" "037" "038" "039" "040" Eric> [41] "041" "042" "043" "044" "045" "046" "047" "048" "049" "050" "051" Eric> "052" "053" "054" "055" "056" "057" "058" "059" "060" Eric> [61] "061" "062" "063" "064" "065" "066" "067" "068" "069" "070" "071" Eric> "072" "073" "074" "075" "076" "077" "078" "079" "080" Eric> [81] "081" "082" "083" "084" "085" "086" "087" "088" "089" "090" "091" Eric> "092" "093" "094" "095" "096" "097" "098" "099" "100" Eric> [101] "101" "102" "103" "104" "105" "106" "107" "108" "109" "110" "111" Eric> ? formatC Yes; note also "sprintf". The folllowing shows it can even be simplified:> (nn <- c(1:12, sort(outer(10^(2:4), -1:1, "+"))))[1] 1 2 3 4 5 6 7 8 9 10 11 12 [13] 99 100 101 999 1000 1001 9999 10000 10001> formatC(nn, width=3, flag="0")[1] "001" "002" "003" "004" "005" "006" "007" "008" "009" [10] "010" "011" "012" "099" "100" "101" "999" "1000" "1001" [19] "9999" "1e+04" "1e+04"> formatC(nn, width=3, flag="0", format="fg")[1] "001" "002" "003" "004" "005" "006" "007" "008" "009" [10] "010" "011" "012" "099" "100" "101" "999" "1000" "1001" [19] "9999" "10000" "10001"> sapply(as.integer(nn), function(n) sprintf("%03d", n))[1] "001" "002" "003" "004" "005" "006" "007" "008" "009" [10] "010" "011" "012" "099" "100" "101" "999" "1000" "1001" [19] "9999" "10000" "10001" Martin
Gregor GORJANC wrote:> Hello! > > I am producing a set of images and I would like them to be sorted by > names I give. I was able to produce my names and add integer to them. > That is easy. But my problem lies in sort of file from this process: > > figure_10.png > figure_11.png > figure_12.png > ... > figure_1.png > figure_20.pngOne simple solution for sortable file names is to add a large integer to your index i: png(filename=paste("figure_", i + 1000, ".png", sep="")) Thomas P.
Hello! I am producing a set of images and I would like them to be sorted by names I give. I was able to produce my names and add integer to them. That is easy. But my problem lies in sort of file from this process: figure_10.png figure_11.png figure_12.png ... figure_1.png figure_20.png ... So I would like to convert integers to something like 01 if upper limit for this conert is 10 or 001 for 100. I wrote a simple function (see below), but I do not know how this limit stuff can be imporved to work really well with default. Any suggestions? int2char <- function(x, limit = max(x)) { # Description: # Converts integer to character such that numbers bellow limit get 0 in # front # Gregor GORJANC, 2005-01-05 # Arguments: # x: vector of numbers # limit: limit up to which numbers should get 0 in front, default # max(x) # Examples: # a <- seq(0, 20, 1) # int2char(a) # this does not work OK # int2char(a, limit = 10) # this does work OK # How to: # I would like that default would be more efficient - so it would # recognize that let say limit 20 in example above should actually be # 10 and so on. # Code: for (i in 1:length(x)) { if (x[i] < limit) { n[i] <- paste("0", x[i], sep = "") } else { n[i] <- as.character(x[i]) } } return(n) } -- Lep pozdrav / With regards, Gregor GORJANC --------------------------------------------------------------- University of Ljubljana Biotechnical Faculty URI: http://www.bfro.uni-lj.si Zootechnical Department mail: gregor.gorjanc <at> bfro.uni-lj.si Groblje 3 tel: +386 (0)1 72 17 861 SI-1230 Domzale fax: +386 (0)1 72 17 888 Slovenia
Thanks to all for valuable suggestions! -- Lep pozdrav / With regards, Gregor GORJANC --------------------------------------------------------------- University of Ljubljana Biotechnical Faculty URI: http://www.bfro.uni-lj.si Zootechnical Department mail: gregor.gorjanc <at> bfro.uni-lj.si Groblje 3 tel: +386 (0)1 72 17 861 SI-1230 Domzale fax: +386 (0)1 72 17 888 Slovenia