precision_round: Suggested precision for rounded significant digit formats

Description Usage Arguments Details Value Source See Also Examples

Description

The function precision_round_ takes the maximum absolute distance between values, (step), and the maximum absolute value, (value), of the values to be formatted, and returns the suggested precision for formats that round to significant digits (e.g. "g"). precision_round calculates this precision given a numeric vector of values to format, x.

Usage

1
2
3

Arguments

x

A numeric vector of values to be formatted.

step

Numeric: Minimum absolute difference between values that will be formatted.

xmax

Numeric: Maximum absolute value of the values to be formatted.

Details

The suggested precision, p, for values in a vector, x, is max(0, floor(log10(max(abs(x)) - d)) - floor(log10(d))), where d the maximum absolute distance between values of x.

For the exponential format, "e", substract use p - 1 for the precision.

Value

An integer vector of suggest precisions. For precision_fixed, this is length one, for precision_fixed_ it is the same length as step.

Source

The d3-format function precisionRound

See Also

precision_fixed for the suggested precision to use with formats that use a fixed number of digits after the decimal point, and precision_prefix for a suggested SI prefix to use.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# For these, the step size is 0.01 and suggested precision is 3
x <- c(0.99, 1, 1.01)
p <- precision_round(x)
fmt(x, paste0(".", p, "r"))
# For these, the step size is 0.1 and suggested precision is 2
x2 <- c(0.9, 1.0, 1.1)
p2 <- precision_round(x2)
fmt(x2, paste0(".", p2, "r"))
# For the e format type subtract one
fmt(x, paste0(".", p - 1, "e"))

jrnold/fivemat documentation built on May 20, 2019, 1 a.m.