[R] format integer numbers with leading 0

2018-01-04 Thread Marc Girondot via R-help
Dear R-er, I would like format integer number as characters with leading 0 for a fixed width, for example: 1 shoud be "01" 2 shoud be "02" 20 should be "20" Now I use: x <- c(1, 2, 20) gsub(" ", "0", format(x, width=2)) But I suspect more elegant way could be done directly with format

Re: [R] format integer numbers with leading 0

2018-01-04 Thread Lasse Kliemann
Marc Girondot via R-help writes: > I would like format integer number as characters with leading 0 for a > fixed width, for example: > > 1 shoud be "01" > 2 shoud be "02" > 20 should be "20" formatC(x, width=2, flag="0") signature.asc Description: PGP signature

Re: [R] format integer numbers with leading 0

2018-01-04 Thread David Wolfskill
On Thu, Jan 04, 2018 at 09:12:12PM +0100, Marc Girondot via R-help wrote: > Dear R-er, > > I would like format integer number as characters with leading 0 for a > fixed width, for example: > > 1 shoud be "01" > 2 shoud be "02" > 20 should be "20" > > Now I use: > > x <- c(1, 2, 20) > >

[R] format integer numbers with leading 0

2018-01-04 Thread Marc Girondot via R-help
Dear R-er, I would like format integer number as characters with leading 0 for a fixed width, for example: 1 shoud be "01" 2 shoud be "02" 20 should be "20" Now I use: x <- c(1, 2, 20) gsub(" ", "0", format(x, width=2)) But I suspect more elegant way could be done directly with format

Re: [R] Format integer

2008-05-13 Thread Prof Brian Ripley
This is one of those problems where the fine details matter. 1) The version of R. I optimized sprintf() for long inputs and a single format in R 2.7.0 -- the differences are mainly for multiple inputs and where coercion is needed. See also below. 2) The system. My home system with an

Re: [R] Format integer

2008-05-13 Thread Esmail Bonakdarian
Anh Tran wrote: Hi, What's one way to convert an integer to a string with preceding 0's? such that '13' becomes '013' to be put into a string I've tried formatC, but they removes all the zeros and replace it with blanks Hi, try sprintf: i=13 cat(sprintf(%05d\n, i)) 00013 HTH,

[R] Format integer

2008-05-12 Thread Anh Tran
Hi, What's one way to convert an integer to a string with preceding 0's? such that '13' becomes '013' to be put into a string I've tried formatC, but they removes all the zeros and replace it with blanks Thanks -- Regards, Anh Tran [[alternative HTML version deleted]]

Re: [R] Format integer

2008-05-12 Thread Anh Tran
Thanks. formatC(flag) works. But it's awefully slow. I try to do that for 65000 numbers (generating ID for each item) and it seems like forever. Is there any faster way? Thank all. Anh Tran On Mon, May 12, 2008 at 2:36 PM, Uwe Ligges [EMAIL PROTECTED] wrote: Anh Tran wrote: Hi,

Re: [R] Format integer

2008-05-12 Thread Charilaos Skiadas
On May 12, 2008, at 5:22 PM, Anh Tran wrote: Hi, What's one way to convert an integer to a string with preceding 0's? such that '13' becomes '013' to be put into a string I've tried formatC, but they removes all the zeros and replace it with blanks formatC(13, width=10, format=d,

Re: [R] Format integer

2008-05-12 Thread Uwe Ligges
Anh Tran wrote: Thanks. formatC(flag) works. But it's awefully slow. I try to do that for 65000 numbers (generating ID for each item) and it seems like forever. On my not that recent laptop: system.time(formatC(1:65000, width=10, flag=0)) user system elapsed 1.920.001.94

Re: [R] Format integer

2008-05-12 Thread Uwe Ligges
Anh Tran wrote: Hi, What's one way to convert an integer to a string with preceding 0's? such that '13' becomes '013' to be put into a string I've tried formatC, but they removes all the zeros and replace it with blanks Not so for me: formatC(13, digits=10, flag=0) Uwe LIgges

Re: [R] Format integer

2008-05-12 Thread Anh Tran
Yea, thanks all. I checked back and I got a few things mistyped. The array is 650,000 and it took 25 seconds :p. It's acceptable. Just that I had too many variable at the time I ran it. Also, seems like sprintf is a little faster. Thanks all. Anh Tran On Mon, May 12, 2008 at 2:55 PM, Uwe

Re: [R] Format integer

2008-05-12 Thread Phil Spector
I guess little means different things to different people: x = sample(1:100,65,replace=TRUE) system.time(a-formatC(x,digits=10,flag='0')) user system elapsed 32.854 0.444 34.813 system.time(b-sprintf(%011d,x)) user system elapsed 0.352 0.012 0.363 If you look at the