I have a set of data which looks something like this:
anim <- c(25499,25500,25501,25502,25503,25504)
sex <- c(1,2,2,1,2,1)
wt <- c(0.8,1.2,1.0,2.0,1.8,1.4)
data <- data.frame(anim,sex,wt)
data
anim sex wt anim2
1 25499 1 0.8 2
2 25500 2 1.2 2
3 25501 2 1.0 2
4 25502 1 2.0 2
5 25503 2 1.8 2
6 25504 1 1.4 2
I would like a zero to be added before each animal id:
data
anim sex wt anim2
1 025499 1 0.8 2
2 025500 2 1.2 2
3 025501 2 1.0 2
4 025502 1 2.0 2
5 025503 2 1.8 2
6 025504 1 1.4 2
And for interest sake, what if I need to add two or three zeros before the animal id's?
For other circumstances in which you want the number string to be consistent, I made a function.
Someone may find this useful:
Sorry about the formatting.
For a general solution that works regardless of how many digits are in
data$anim
, use thesprintf
function. It works like this:In your case, you probably want:
data$anim <- sprintf("%06d", data$anim)
Expanding on @goodside's repsonse:
In some cases you may want to pad a string with zeros (e.g. fips codes or other numeric-like factors). In OSX/Linux:
But because
sprintf()
calls the OS's Csprintf()
command, discussed here, in Windows 7 you get a different result:So on Windows machines the work around is:
Here's a generalizable base R function:
I like
sprintf
but it comes with caveats like:The short version: use
formatC
orsprintf
.The longer version:
There are several functions available for formatting numbers, including adding leading zeroes. Which one is best depends upon what other formatting you want to do.
The example from the question is quite easy since all the values have the same number of digits to begin with, so let's try a harder example of making powers of 10 width 8 too.
paste
(and it's variantpaste0
) are often the first string manipulation functions that you come across. They aren't really designed for manipulating numbers, but they can be used for that. In the simple case where we always have to prepend a single zero,paste0
is the best solution.For the case where there are a variable number of digits in the numbers, you have to manually calculate how many zeroes to prepend, which is horrible enough that you should only do it out of morbid curiosity.
str_pad
fromstringr
works similarly topaste
, making it more explicit that you want to pad things.Again, it isn't really designed for use with numbers, so the harder case requires a little thinking about. We ought to just be able to say "pad with zeroes to width 8", but look at this output:
You need to set the scientific penalty option so that numbers are always formatted using fixed notation (rather than scientific notation).
stri_pad
instringi
works exactly likestr_pad
fromstringr
.formatC
is an interface to the C functionprintf
. Using it requires some knowledge of the arcana of that underlying function (see link). In this case, the important points are thewidth
argument,format
being"d"
for "integer", and a"0"
flag
for prepending zeroes.This is my favourite solution, since it is easy to tinker with changing the width, and the function is powerful enough to make other formatting changes.
sprintf
is an interface to the C function of the same name; likeformatC
but with a different syntax.The main advantage of
sprintf
is that you can embed formatted numbers inside longer bits of text.See also goodside's answer.
For completeness it is worth mentioning the other formatting functions that are occasionally useful, but have no method of prepending zeroes.
format
, a generic function for formatting any kind of object, with a method for numbers. It works a little bit likeformatC
, but with yet another interface.prettyNum
is yet another formatting function, mostly for creating manual axis tick labels. It works particularly well for wide ranges of numbers.The
scales
package has several functions such aspercent
,date_format
anddollar
for specialist format types.