Pad Strings

Description

A convenience wrapper for sprintf that pads the front end of strings with spaces or 0s. Useful for creating multiple uniform directories that will maintain correct order.

Usage

1
2
  pad(x, padding = max(nchar(as.character(x))),
    sort = TRUE, type = "detect")

Arguments

x

A character, factor, numeric vector.

padding

Number of characters to pad. Default makes all elements of a string the number of characters of the element with the maximum characters.

sort

logical. If TRUE the outcome is sorted.

type

A character string of "detect", "numeric", "character", "d" or "s". If numeric zeros are padded. If character spaces are padded. The detect attempts to determine if x is numeric (d) or not (s).

Value

Returns a character vector every element padded with 0/spaces.

See Also

sprintf

Examples

1
2
3
4
pad(sample(1:10, 6))
pad(sample(1:10, 6), sort=FALSE)
pad(as.character(sample(1:10, 6)))
pad(as.character(sample(1:10, 6)), 4)