Learn R Programming

dreamerr (version 1.1.0)

signif_plus: Formatting for numbers to appear in-text

Description

Formatting of numbers, when they are to appear in messages. Displays only significant digits in a "nice way" and adds commas to separate thousands.

Usage

signif_plus(x, s = 2, r = 0, commas = TRUE)

Arguments

x

A numeric vector.

s

The number of significant digits to be displayed. Defaults to 2. All digits not in the decimal are always shown.

r

For large values, the number of digits after the decimals to be displayed (beyond the number of significant digits). Defaults to 0. It is useful to suggest that a number is not an integer.

commas

Whether or not to add commas to separate thousands. Defaults to TRUE.

Value

It returns a character vector of the same length as the input.

Examples

Run this code
# NOT RUN {
x = rnorm(1e5)
x[sample(1e5, 1e4, TRUE)] = NA

# Dumb function telling the number of NA values
tell_na = function(x) message("x contains ", signif_plus(sum(is.na(x))), " NA values.")

tell_na(x)

# Some differences with signif:
show_diff = function(x, d = 2) cat("signif(x, ", d, ") -> ", signif(x, d),
                                   " vs signif_plus(x, ", d, ") -> ",
                                   signif_plus(x, d), "\n", sep = "")

# Main difference is for large numbers
show_diff(95123.125)
show_diff(95123.125, 7)

# Identical for small numbers
show_diff(pi / 500)


# }

Run the code above in your browser using DataLab