3

I am trying to use sprintf function to add leading "0" to character, and make all charaters the same length. However what I get is leading space.

My code:

a <- c("12","123", "1234")
sprintf("%04s",a)
[1] "  12" " 123" "1234"

What I tried to get:

[1] "0012" "0123" "1234"

The sprintf manual says: "For characters, this zero-pads on some platforms and is ignored on others."

My version: platform x86_64-w64-mingw32
arch x86_64
os mingw32
system x86_64, mingw32
status
major 3
minor 1.0
year 2014
month 04
day 10
svn rev 65387
language R
version.string R version 3.1.0 (2014-04-10) nickname Spring Dance

Peter Pan
  • 65
  • 5
  • 4
    You need more examples before I know what you're trying to do. Sometimes this is ignored on other systems, as the manual says. Perhaps `sprintf("%07d",125)` – Blue Magister Jun 05 '14 at 15:09
  • Does this question help: http://stackoverflow.com/questions/8266915/format-number-as-fixed-width-with-leading-zeros/8267036#8267036 – Blue Magister Jun 05 '14 at 15:11
  • See also [this answer](http://stackoverflow.com/questions/24043162/r-excel-leading-zeroes/24044769#24044769) – Jaap Jun 05 '14 at 15:17
  • It works fine with me (R 3.0.2 on OSX 10.8.4). Did you try to convert your character vector to a numeric vector? – Jaap Jun 05 '14 at 15:26
  • Couldn't you just paste 0 and your string together? – talat Jun 05 '14 at 15:28
  • As @beginneR said, `paste0("0", "125")` works fine on characters – Rich Scriven Jun 05 '14 at 15:34
  • 1
    @beginneR @RichardScriven When all the elements of a vector are of the same length, that might be a solution. However, when the elements have different lengths and when the OP wants to convert all elements to the same length with leading zeros, than `paste` (or `paste0`) won't give the desired solution. – Jaap Jun 05 '14 at 15:38
  • If your vector only ever has numerals, then I think @BlueMagister's comment above should be the solution. Note that the [manual states](http://stat.ethz.ch/R-manual/R-devel/library/base/html/sprintf.html): "For numbers, pad to the field width with leading zeros. For characters, this zero-pads on some platforms and is ignored on others". Just coerce the vector to to numeric first. – jbaums Jun 06 '14 at 23:32

1 Answers1

2

If you are on a platform which only inserts spaces, regex is your friend. Something like

foo<- gsub('^[ ]{1,}','0',bar)

That will replace all leading spaces with a 0 . I know regex can be told to replace N spaces with N zero-chars, but I forget exactly how.

EDIT: to those paste0 naysayers, how about:

wantlength <- 12 # the desired final string size, fully zero padded
paste0( paste0(rep('0',wantlength-nchar(foo)),collapse='') ,foo)
Carl Witthoft
  • 20,573
  • 9
  • 43
  • 73
  • 1
    Slightly more simply, `gsub('^ +', '0', bar)`, or `gsub('^\\s+', '0', bar)` to match all leading white space characters. But it's probably safe to assume that the leading white spaces are the only white spaces in the strings (are we padding numbers?), so we can just `gsub('\\s', '0', bar)`. – jbaums Jun 05 '14 at 15:49
  • Thank you, guys. I think jbaums's function: gsub('^\\s+', '0', bar) solved my problem. – Peter Pan Jun 06 '14 at 19:31