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
opt
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
___
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)
>
> gsub("
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
opt
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,
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 Intel
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 t
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 Ligge
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
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
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", fl
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,
Try something like one of these (as documented in ?formatC)
> formatC(13, flag="0", width=10)
[1] "13"
> sprintf("%010g", 13)
[1] "13"
>
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
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]]
14 matches
Mail list logo