signif_pad: Round numbers with 0-padding.

View source: R/table1.R

signif_padR Documentation

Round numbers with 0-padding.

Description

Utility functions to round numbers, similar the the base functions signif and round, but resulting in character representations that keep zeros at the right edge if they are significant.

Usage

signif_pad(x, digits = 3, round.integers = TRUE, round5up = TRUE, dec, ...)

round_pad(x, digits = 2, round5up = TRUE, dec, ...)

Arguments

x

A numeric vector.

digits

An integer specifying the number of significant digits to keep (for signif_pad) or the number of digits after the decimal point (for round_pad).

round.integers

Should rounding be limited to digits to the right of the decimal point?

round5up

Should numbers with 5 as the last digit always be rounded up? The standard R approach is "go to the even digit" (IEC 60559 standard, see round), while some other softwares (e.g. SAS, Excel) always round up.

dec

The character symbol to use as decimal mark (locale specific). [Deprecated; use decimal.mark instead]

...

Further options, passed to formatC (which is used internally). Not all options will work, but some might be useful (e.g. big.mark, decimal.mark).

Value

A character vector containing the rounded numbers.

See Also

signif round formatC prettyNum format

Examples

x <- c(0.9001, 12345, 1.2, 1., 0.1, 0.00001 , 1e5)
signif_pad(x, digits=3)
signif_pad(x, digits=3, round.integers=TRUE)

# Compare:
as.character(signif(x, digits=3))
format(x, digits=3, nsmall=3)
prettyNum(x, digits=3, drop0trailing=TRUE)
prettyNum(x, digits=3, drop0trailing=FALSE)

# This is very close.
formatC(x, format="fg", flag="#", digits=3) 
formatC(signif(x, 3), format="fg", flag="#", digits=3)

# Could always remove the trailing "."
sub("[.]$", "", formatC(x, format="fg", flag="#", digits=3))


table1 documentation built on Jan. 6, 2023, 5:07 p.m.