sprintf("%03d", 2)
formatC(2, width=3, flag="0")

both give

[1] "002"

On Fri, 17 Oct 2008, Michal Figurski wrote:

Hello R-helpers,

I have a problem with formatting a single number to show leading zeros. For example, I want "2" displayed as "002".

I hope you meant that you want 2, not "2", so displayed. Otherwise you need as.numeric("2") here.

My numbers have 1 to 3 digits and I would like them all to display 3 digits for printing. I know I could use "paste" in a loop with several "if"s, but I was wondering if there is a single function that can do this.

I have tried "format", "prettyNum" and "formatC" back and forth, but these functions don't seem to be able to display leading zeros. Please help.

You will find studying the help more productive than 'tried ... back and forth'.

--
Brian D. Ripley,                  [EMAIL PROTECTED]
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 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

______________________________________________
R-help@r-project.org mailing list
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.

Reply via email to