round_sensibly: Round Numbers to an "Appropriate" Number of Digits

Description Usage Arguments Details Value Examples

Description

This function is primarily geared towards use in labelling. By default it will round a numeric vector to the lowest number of digits required to make sure that no two values get rounded to the same value. Alternatively it will round to significant digits, significant relative to the range of the input vector.

Usage

1
round_sensibly(.x, .digits = 0, .separation = TRUE)

Arguments

.x

Numeric vector. Values to be rounded.

.digits

Numeric vector. Number of digits that should be rounded to relative to the base value decided by .separation=.

.separation

Logical scalar. If TRUE``.digits=is relative to the number of digits required to discern **all** values. IfFALSE‘ it’s relative to the number of digits required to discern the lowest and highest values.

Details

The usage of .digits is not quite consistent between .separation = TRUE and .separation = FALSE. But it was chosen this way because it corresponds in my experience with the way we think about rounding numbers for labels.

Either you want "x significant digits" as in digits that make actually a difference. It makes sense that 1 should be one significant digit and so on.

Or you want enough digits so that there are no identical labels. In this case it makes sense that 0 should be that number of digits.

Value

A rounded numeric vector.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
x <- c(1.123, 1.134, 2.23)

# rounded to just enough digits to be able to separate one and 2
round_sensibly(x)

# rounded to one more digit
round_sensibly(x, 1)

# rounded to 1 significant digit of separation.
round_sensibly(x, 1, .separation = FALSE)

STAT-UP/statupinternal documentation built on May 9, 2019, 11:43 a.m.