padZero: Pad a vector of numbers with zeros

Description Usage Arguments Details Value Author(s) Examples

View source: R/padZero.R

Description

Pad a numeric vector with zeros so that each element in the vector either has 1) the same number of characters or 2) the same number of trailing decimal places.

Usage

1
padZero(vec, num = NULL, side = c("left", "right"))

Arguments

vec

The numeric vector to be padded

num

The maximum number of zeros that will be added. If NULL, the value is chosen based on the longest string in the vector.

side

The side to which the zeros are added.

Details

For side = 'left', num is the number of characters that each element of the padded, output vector should have. If num = NULL, the largest number of characters that appears in the vector is chosen for num.

For side = 'right', num is the number of decimal places to be displayed. If num = NULL, the number of decimals in the element with the largest number of decimal places is used.

Note that vec must be numeric when side = 'right'. However, vec may be character when side = 'left'.

Value

Character vector with the leading (or trailing) elements padded with zeros.

Author(s)

Landon Sego

Examples

1
2
3
4
5
6
7
# Examples with 0's on the left
padZero(c(1, 10, 100))
padZero(c(1, 10, 100), num = 4)

# Examples with 0's on the right
padZero(c(1.2, 1.34, 1.399), side = "r")
padZero(c(1.2, 1.34, 1.399), num = 5, side = "r")

Example output

[1] "001" "010" "100"
[1] "0001" "0010" "0100"
[1] "1.200" "1.340" "1.399"
[1] "1.20000" "1.34000" "1.39900"

Smisc documentation built on May 2, 2019, 2:46 a.m.