These functions round to the nearest fraction of a
denominator rather than to a decimal place. They follow
janitor::round_to_fraction() but take all of reround()'s options:
reround_to_fraction()rounds to fractions of a whole number, then optionally rounds the result todigitsdecimal places.reround_to_fraction_level()rounds to the nearest fraction at the decimal level given bydigits, with no further rounding.
Usage
reround_to_fraction(
x = NULL,
denominator = 1,
digits = Inf,
rounding = "up_or_down",
threshold = 5,
symmetric = FALSE
)
reround_to_fraction_level(
x = NULL,
denominator = 1,
digits = 0L,
rounding = "up_or_down",
threshold = 5,
symmetric = FALSE
)Arguments
- x
Numeric. Values to round.
- denominator
Numeric (at least 1).
xis rounded to the nearest fraction ofdenominator. Default is1.- digits
Integer. In
reround_to_fraction(), the number of decimal places to round the result to."auto"meansceiling(log10(denominator)) + 1, as in janitor, and the defaultInfmeans no further rounding. Inreround_to_fraction_level(), the decimal level at which to round to a fraction; default is0.- rounding, threshold, symmetric
Passed on to
reround().
Value
Numeric, as long as x, except for the compound rounding methods,
which return two values per element of x as in reround().
See also
reround() and janitor::round_to_fraction().
Examples
# With `denominator = 1`, this is ordinary rounding...
reround_to_fraction(0.4, denominator = 1, rounding = "even")
#> [1] 0
# ...with `denominator = 2`, it rounds to the nearest half:
reround_to_fraction(0.4, denominator = 2, rounding = "even")
#> [1] 0.5
reround_to_fraction(0.25, denominator = 3, rounding = "even")
#> [1] 0.3333333
# The default `rounding` returns both directions, as in `reround()`:
reround_to_fraction(0.25, denominator = 2)
#> [1] 0.5 0.0
# `reround_to_fraction_level()` rounds to a fraction at a decimal level:
reround_to_fraction_level(0.12345, denominator = 2, digits = 0)
#> [1] 0 0
reround_to_fraction_level(0.12345, denominator = 2, digits = 1)
#> [1] 0.1 0.1
reround_to_fraction_level(0.12345, denominator = 2, digits = 2)
#> [1] 0.125 0.125