precision_fixed: Suggested precision for fixed decimal formats

Description Usage Arguments Details Value Source See Also Examples

Description

Take either a numeric vector or the maximum absolute step size between values and and return a suggested decimal precision for fixed point formats such as "f".

Usage

1
2
3

Arguments

x

A numeric vector to be formatted.

step

Minimum absolute difference between values that will be formatted.

Details

precision_round Returns a suggested decimal precision for format types that round to significant digits, such as "g" and "r".

precision_fixed Returns a suggested SI prefix.

The suggested precision is

max(0, floor(log10(d)))

where d is the maximum absolute distance between values. This assumes that the values to be formatted are multiples of the step size d.

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 precisionFixed.

See Also

precision_round for the suggested precision to use with formats that round to significant digits, and precision_prefix for the suggested SI prefix to use with formatting.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# step size is 1 and the suggested precision is 0
x <- c(1, 1.5, 2)
p <- precision_fixed(x)
p
fmt(x, paste0(".", p, "f"))
# alternatively calculate the step size
step <- max(diff(sort(x)))
precision_fixed_(step)
# For 1, 2, 3, the step size is 1 suggested precision is 0
x2 <- 1:3
p2 <- precision_fixed(x2)
p2
fmt(x2, paste0(".", p, "f"))

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