Skip to content

sprintf does not fill with "0"?!

6 messages · oliver, Earl F. Glynn, Barry Rowlingson +2 more

#
Hello,


please look here:





=================================================================
[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
#
"Oliver Bandel" <oliver at first.in-berlin.de> wrote in message 
news:1221513552.6194.5.camel at calcman...
Would this be OK as an alternative?
[1] "001" "002" "003" "004" "005"

efg

Earl F. Glynn
Scientific Programmer
Stowers Institute for Medical Research
#
2008/9/15 Oliver Bandel <oliver at first.in-berlin.de>:
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
#
Try

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

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

Regards, Adai
Oliver Bandel wrote:
#
On Mon, Sep 15, 2008 at 2:19 PM, Oliver Bandel
<oliver at first.in-berlin.de> wrote:
No, the '0' flag only applies to numeric types (e.g. 'd'), and you
specify a string.  Instead, you want to do something like:
[1] "001" "002" "003" "004" "005"

/Henrik
#
Zitat von Henrik Bengtsson <hb at stat.berkeley.edu>:
[...]


Oh, well... ok.

Thanks to all. :)

Ciao,
   Oliver