Hi,
I think it's because " " is used to pad strings, while 0 is used to
pad numbers*. If your values are always numeric, but stored as
strings, you could use:
> x <- "123"
> sprintf("%05d", as.numeric(x))
[1] "00123"
* From ?sprintf:
‘0’ For numbers, pad to the field width with leading zeros.
I think some language implementations allow for specifying different
pad characters, but R's doesn't seem to.
Sarah
On Tue, May 22, 2012 at 2:41 PM, Hui Du <[email protected]> wrote:
> Dear All,
>
> This question sounds very simple but I don't know where I am wrong. I just
> want to pad leading zeros in some string, for example, "123" becomes "00123".
> What is wrong if I do following?
>
>> sprintf("%05s", "123")
> [1] " 123"
>
>
> It didn't return "00123", instead it padded with 'blank'.
>
>
> Thank you for your help in advance.
>
> HXD
>
>
--
Sarah Goslee
http://www.functionaldiversity.org
______________________________________________
[email protected] 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.