[R] sprintf does not fill with "0"?!

Henrik Bengtsson hb at stat.berkeley.edu
Mon Sep 15 23:58:36 CEST 2008


On Mon, Sep 15, 2008 at 2:19 PM, Oliver Bandel
<oliver at first.in-berlin.de> wrote:
> Hello,
>
>
> please look here:
>
>
>
>
>
> =================================================================
>>
>> sprintf("%03s", as.character(1:5))
> [1] "  1" "  2" "  3" "  4" "  5"
>>
> =================================================================
>
> There should be a leading "0" isntead of space-chars?!

No, the '0' flag only applies to numeric types (e.g. 'd'), and you
specify a string.  Instead, you want to do something like:

> sprintf("%03d", 1:5)
[1] "001" "002" "003" "004" "005"

/Henrik

>
>
>
> I use  R version 2.6.2 (2008-02-08)
>
>
> Ciao,
>  Oliver
>
> ______________________________________________
> 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.
>



More information about the R-help mailing list