[R] handle a sequence of strings with for loop

Uwe Ligges ligges at statistik.uni-dortmund.de
Mon Jan 28 21:40:13 CET 2002



jonathan_li at agilent.com wrote:
> 
> Hi all,
> 
> I have a string sequence:
> "my00", "my01", "my02",...,"my99".
> I would like to process them one by one use a for loop like:
> 
> for(i in 0:99){
>         str <- paste("my", i, sep="")
>         print(str)
> }
> 
> But this only works with string "my10" to "my99".
> In Python, this problem can be solved by string formating zero fill such as
> print "%02d", i;
> which will give
> 00, 01, ...,09,10,...,99
> 
> I wonder if R has similar solutions?


Details in ?formatC

In your case:
  formatC(i, width=2, flag=0)

Uwe
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list