[R] Beginner's question: number formatting

jim holtman jholtman at gmail.com
Fri Oct 17 20:07:20 CEST 2008


?sprintf

> x <- c(1,10,11,100)
> sprintf("%03d", x)
[1] "001" "010" "011" "100"


On Fri, Oct 17, 2008 at 1:55 PM, Michal Figurski
<figurski at mail.med.upenn.edu> 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".
>
> 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.
>
>
> --
> Michal J. Figurski
>
> ______________________________________________
> R-help at 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.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?



More information about the R-help mailing list