Re: [R] I'm sure I'm missing something with formatC() or sprintf()

2012-02-23 Thread Ted Harding
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,

Re: [R] I'm sure I'm missing something with formatC() or sprintf()

2012-02-23 Thread Sarah Goslee
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"

Re: [R] I'm sure I'm missing something with formatC() or sprintf()

2012-02-23 Thread William Dunlap
unlap tibco.com > -Original Message----- > From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On > Behalf Of z2.0 > Sent: Thursday, February 23, 2012 11:16 AM > To: r-help@r-project.org > Subject: [R] I'm sure I'm missing something with formatC()

[R] I'm sure I'm missing something with formatC() or sprintf()

2012-02-23 Thread z2.0
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