round_using_magnitude {table.glue}R Documentation

Set rules for rounding numbers

Description

These functions update a rounding_specification object (see round_spec) so that a particular approach to rounding is applied:

Usage

round_using_magnitude(rspec, digits = c(2, 1, 0), breaks = c(1, 10, Inf))

round_using_signif(rspec, digits = 2)

round_using_decimal(rspec, digits = 1)

Arguments

rspec

a rounding_specification object (see round_spec).

digits

for round_using_decimal() and round_using_signif, a numeric value specifying the number of decimal places and significant digits to round to, respectively. For round_using_magnitude(), digits should be a numeric vector of equal length to breaks that indicates how many decimals to round to in the numeric range designated by breaks. (see notes for example).

breaks

(only relevant if rounding based on magnitude) a positive, monotonically increasing numeric vector designating rounding boundaries.

Details

digits and breaks must be used in coordination with each other when rounding based on magnitude. For example, using breaks = c(1, 10, Inf) and decimals = c(2, 1, 0),

Value

an object of class rounding_specification.

See Also

Other rounding helpers: round_half_up()

Examples


x <- c(pi, exp(1))
x <- c(x, x*10, x*100, x*1000)

# make one specification using each rounding approach
specs <- list(
  magnitude = round_using_magnitude(round_spec()),
  decimal = round_using_decimal(round_spec()),
  signif = round_using_signif(round_spec())
)

# apply all three rounding specifications to x
# notice how the rounding specifications are in agreement
# for smaller values of x but their answers are different
# for larger values of x.

sapply(specs, function(rspec) table_value(x, rspec))

# output:
#  magnitude   decimal    signif
# [1,] "3.1"     "3.1"     "3.1"
# [2,] "2.7"     "2.7"     "2.7"
# [3,] "31"      "31.4"    "31.0"
# [4,] "27"      "27.2"    "27.0"
# [5,] "314"     "314.2"   "310.0"
# [6,] "272"     "271.8"   "270.0"
# [7,] "3,142"   "3,141.6" "3,100.0"
# [8,] "2,718"   "2,718.3" "2,700.0"

[Package table.glue version 0.0.3 Index]