[R] Converting integers to chars i.e 1 to "01"

Eric Lecoutre lecoutre at stat.ucl.ac.be
Wed Jan 5 10:29:48 CET 2005


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

Eric 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




More information about the R-help mailing list