prependZeros: Pad numbers with leading zeros to specified total width

View source: R/Functions.R

prependZerosR Documentation

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))))

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.

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.

Value

Character vector with the 0-padded numbers.

Author(s)

Peter Langfelder

Examples

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.

WGCNA documentation built on Sept. 18, 2024, 5:08 p.m.

Related to prependZeros in WGCNA...