I have a four-digit string I want to convert to five digits. Take the
following frame:
zip
2108
60321
60321
22030
91910
I need row 1 to read '02108'. This forum directed me to formatC
previously
(thanks!) That usually works but, for some reason, it's not in this
instance. Neither of the syntaxes below change '2108' to
'02108.' The values
in cand_receipts[,1] are of type 'character.'
cand_receipts[,1] <- formatC(cand_receipts[,1], width = 5, format =
's',
flag = '0')
cand_receipts[,1] <- sprintf("%05s", cand_receipts[,1])
Any thoughts?
Thanks,
Zack
--
View this message in context:
http://r.789695.n4.nabble.com/I-m-sure-I-m-missing-something-with-formatC-or-sprintf-tp4414905p4414905.html
Sent from the R help mailing list archive at Nabble.com.
William Dunlap
2012-Feb-23 19:49 UTC
[R] I'm sure I'm missing something with formatC() or sprintf()
sprintf's "%<number>s" format descriptor ignores initial
0's in <number>,
in C's sprintf and in R's. Here are 2 ways to do it:
> z <- c("5", "45", "345",
"2345", "12345")
> sprintf("%05d", as.integer(z))
[1] "00005" "00045" "00345" "02345"
"12345"
> gsub(" ", "0", sprintf("%5s", z))
[1] "00005" "00045" "00345" "02345"
"12345"
Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com > -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at
r-project.org] On Behalf Of z2.0
> Sent: Thursday, February 23, 2012 11:16 AM
> To: r-help at r-project.org
> Subject: [R] I'm sure I'm missing something with formatC() or
sprintf()
>
> I have a four-digit string I want to convert to five digits. Take the
> following frame:
>
> zip
> 2108
> 60321
> 60321
> 22030
> 91910
>
> I need row 1 to read '02108'. This forum directed me to formatC
previously
> (thanks!) That usually works but, for some reason, it's not in this
> instance. Neither of the syntaxes below change '2108' to
'02108.' The values
> in cand_receipts[,1] are of type 'character.'
>
> cand_receipts[,1] <- formatC(cand_receipts[,1], width = 5, format =
's',
> flag = '0')
> cand_receipts[,1] <- sprintf("%05s", cand_receipts[,1])
>
> Any thoughts?
>
> Thanks,
>
> Zack
>
>
>
>
>
> --
> View this message in context:
http://r.789695.n4.nabble.com/I-m-sure-I-m-missing-something-with-
> formatC-or-sprintf-tp4414905p4414905.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.
Sarah Goslee
2012-Feb-23 19:51 UTC
[R] I'm sure I'm missing something with formatC() or sprintf()
You said that the values are already character - that's the key. Compare:> sprintf("%05s", "2018")[1] " 2018"> sprintf("%05d", 2018)[1] "02018" Since they are already character, though, here's another option: x <- c("2108", "60321", "22030") # part of your data ifelse(nchar(x) == 4, paste("0", x, sep=""), x) [1] "02108" "60321" "22030" You could also use:> sprintf("%05d", as.numeric("2018"))[1] "02018" The help for sprintf says this, but not clearly: ?0? For numbers, pad to the field width with leading zeros. Sarah On Thu, Feb 23, 2012 at 2:16 PM, z2.0 <zack.abrahamson at gmail.com> wrote:> I have a four-digit string I want to convert to five digits. Take the > following frame: > > zip > 2108 > 60321 > 60321 > 22030 > 91910 > > I need row 1 to read '02108'. This forum directed me to formatC previously > (thanks!) That usually works but, for some reason, it's not in this > instance. Neither of the syntaxes below change '2108' to '02108.' The values > in cand_receipts[,1] are of type 'character.' > > cand_receipts[,1] <- formatC(cand_receipts[,1], width = 5, format = 's', > flag = '0') > cand_receipts[,1] <- sprintf("%05s", cand_receipts[,1]) > > ?Any thoughts? > > Thanks, > > Zack > > > > > > -- > View this message in context: http://r.789695.n4.nabble.com/I-m-sure-I-m-missing-something-with-formatC-or-sprintf-tp4414905p4414905.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.-- Sarah Goslee http://www.stringpage.com http://www.sarahgoslee.com http://www.functionaldiversity.org
(Ted Harding)
2012-Feb-23 19:58 UTC
[R] I'm sure I'm missing something with formatC() or sprintf()
On 23-Feb-2012 z2.0 wrote:> I have a four-digit string I want to convert to five digits. Take the > following frame: > > zip > 2108 > 60321 > 60321 > 22030 > 91910 > > I need row 1 to read '02108'. This forum directed me to formatC previously > (thanks!) That usually works but, for some reason, it's not in this > instance. Neither of the syntaxes below change '2108' to '02108.' The values > in cand_receipts[,1] are of type 'character.' > > cand_receipts[,1] <- formatC(cand_receipts[,1], width = 5, format = 's', > flag = '0') > cand_receipts[,1] <- sprintf("%05s", cand_receipts[,1]) > > Any thoughts? > > Thanks, > ZackFor this (and similar cases): formatC(2108,width=5,flag="0") # [1] "02108" For longer strings: formatC(2108,width=6,flag="0") # [1] "002108" see ?formatC for more details (the way formatC() aggregates information about the desired format is somewhat different from the format syntax in C's printf() and related functions). Ted. ------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding at wlandres.net> Date: 23-Feb-2012 Time: 19:58:22 This message was sent by XFMail