jonathan_li@agilent.com
2002-Jan-28 20:13 UTC
[R] handle a sequence of strings with for loop
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? Regards, Jonathan -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On Mon, 28 Jan 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??formatC. As in> formatC(9, width=2, flag="0")[1] "09" -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272860 (secr) Oxford OX1 3TG, UK Fax: +44 1865 272595 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._