prependZeros | R Documentation |
These functions pad the specified numbers with zeros to a specified total width.
prependZeros(x, width = max(nchar(x)))
prependZeros.int(x, width = max(nchar(as.integer(x))))
x |
Vector of numbers to be padded. For |
width |
Width to pad the numbers to. |
The prependZeros.int
version works better with numbers such as 100000 which may get converted to character as 1e5 and
hence be incorrectly padded in the prependZeros
function. On the flip side, prependZeros works also for non-integer
inputs.
Character vector with the 0-padded numbers.
Peter Langfelder
prependZeros(1:10)
prependZeros(1:10, 4)
# more exotic examples
prependZeros(c(1, 100000), width = 6) ### Produces incorrect output
prependZeros.int(c(1, 100000)) ### Correct output
prependZeros(c("a", "b", "aa")) ### pads the shorter strings using zeros.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.