Learn R Programming

table1 (version 1.1)

signif_pad: Round numbers to specified significant digits with 0-padding.

Description

A utility function to round numbers to a specified number of significant digits. Zeros are kept if they are significant.

Usage

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

Arguments

x

A numeric vector.

digits

An integer specifying the number of significant digits to keep.

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.

Value

A character vector containing the rounded numbers.

See Also

signif formatC prettyNum format

Examples

Run this code
# NOT RUN {
x <- c(0.9001, 12345, 1.2, 1., 0.1)
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))

# }

Run the code above in your browser using DataLab