sprintf does not fill with "0"?!

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

sprintf does not fill with "0"?!

Oliver-3
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?!



I use  R version 2.6.2 (2008-02-08)


Ciao,
  Oliver

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: sprintf does not fill with "0"?!

Earl F. Glynn
"Oliver Bandel" <[hidden email]> wrote in message
news:1221513552.6194.5.camel@calcman...
> Hello,
>> sprintf("%03s", as.character(1:5))
> [1] "  1" "  2" "  3" "  4" "  5"

Would this be OK as an alternative?

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

efg

Earl F. Glynn
Scientific Programmer
Stowers Institute for Medical Research

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: sprintf does not fill with "0"?!

barry rowlingson
In reply to this post by Oliver-3
2008/9/15 Oliver Bandel <[hidden email]>:

>> sprintf("%03s", as.character(1:5))
> [1] "  1" "  2" "  3" "  4" "  5"
>>
> =================================================================
>
> There should be a leading "0" isntead of space-chars?!
>

 In R, sprintf is a wrapper for the C printf functions, and if you
read the C man page it says this about the 0 prefix:

0
The value should be zero padded. For d, i, o, u, x, X, a, A, e, E, f,
F, g, and G conversions, the converted value is padded on the left
with zeros rather than blanks. If the 0 and - flags both appear, the 0
flag is ignored. For other conversions, the behavior is undefined.

Interestingly R seems to pad with spaces (like yours), but my cygwin C
version pads with zeroes. Such is the nature of undefined behaviour!

Barry

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: sprintf does not fill with "0"?!

Adaikalavan Ramasamy-2
In reply to this post by Oliver-3
Try

   sprintf("%03.0f", 1:5)

   formatC(1:5, width=3, flag="0")

Regards, Adai



Oliver Bandel 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?!
>
>
>
> I use  R version 2.6.2 (2008-02-08)
>
>
> Ciao,
>   Oliver
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: sprintf does not fill with "0"?!

Henrik Bengtsson
In reply to this post by Oliver-3
On Mon, Sep 15, 2008 at 2:19 PM, Oliver Bandel
<[hidden email]> 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
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: sprintf does not fill with "0"?!

Oliver-3
Zitat von Henrik Bengtsson <[hidden email]>:

> On Mon, Sep 15, 2008 at 2:19 PM, Oliver Bandel
> <[hidden email]> 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"
[...]


Oh, well... ok.

Thanks to all. :)

Ciao,
   Oliver

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: sprintf does not fill with "0"?!

Dilara Ally
This post has NOT been accepted by the mailing list yet.
In reply to this post by Oliver-3
after sprintf
 h<-sprintf("%03s", as.character(1:5))
fill the spaces with
 gsub("\\s","0",h)