Learn R Programming

WGCNA (version 1.73)

prependZeros: Pad numbers with leading zeros to specified total width

Description

These functions pad the specified numbers with zeros to a specified total width.

Usage

prependZeros(x, width = max(nchar(x)))
prependZeros.int(x, width = max(nchar(as.integer(x))))

Value

Character vector with the 0-padded numbers.

Arguments

x

Vector of numbers to be padded. For prependZeros, the vector may be real (non-integer) or even character (and not necessarily representing numbers). For prependZeros, the vector must be numeric and non-integers get rounded down to the nearest integer.

width

Width to pad the numbers to.

Author

Peter Langfelder

Details

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.

Examples

Run this code
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.

Run the code above in your browser using DataLab