format integer numbers with leading 0

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

format integer numbers with leading 0

R help mailing list-2
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
options, but I don't find.

Thanks a lot

Marc

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.