tbl_val: Table value rounding

Description Usage Arguments Details Value Note Examples

View source: R/tbl_val.R

Description

Table value rounding

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
tbl_val(
  x,
  round_half_to_even = FALSE,
  breaks = c(1, 10, Inf),
  decimals = c(2, 1, 0),
  miss_replace = "--",
  big_mark = ",",
  big_interval = 3L,
  small_mark = "",
  small_interval = 5L,
  decimal_mark = getOption("OutDec"),
  zero_print = NULL,
  trim = TRUE
)

Arguments

x

a vector of numeric values

round_half_to_even

a logical value. If TRUE, then when the fractional part of x is half-way between two rounding boundaries, it will be rounded to the even boundary nearest to x. For example, +23.5 becomes +24, as does +24.5; while −23.5 becomes −24, as does −24.5. If FALSE, halves are automatically rounded to the boundary with highest absolute value.

breaks

a positive, monotonically increasing numeric vector designating rounding boundaries. With breaks = c(1, 10, 100, 1000) and decimals = c(2, 1, 0)

decimals

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). Allowed values are 0 <= decimals <= 20.

miss_replace

a character string that replaces missing values.

big_mark

a character value used to separate number groups to the left of the decimal point. See prettyNum for more details on this. Set this input to ” to negate it's effect.

big_interval

a numeric value indicating the size of number groups for numbers before (hence big) the decimal.

small_mark

a character value used to separate number groups to the right of the decimal point.

small_interval

a numeric value indicating the size of number groups for numbers after (hence small) the decimal.

decimal_mark

the character to be used to indicate the numeric decimal point.

zero_print

a logical value, character string or NULL value specifying if and how zeros should be formatted specially. Useful for pretty printing 'sparse' objects.

trim

a logical value; if FALSE, logical, numeric and complex values are right-justified to a common width: if TRUE the leading blanks for justification are suppressed.

Details

Using round_half_to_even = TRUE minimizes the expected error when summing over rounded figures, even when the inputs are mostly positive or mostly negative. This variant of the round-to-nearest method is also called convergent rounding, statistician's rounding, Dutch rounding, Gaussian rounding, odd–even rounding, or bankers' rounding.

Value

a character vector with rounded values

Note

tbl_val converts NA values to non-missing character values. This is because different R packages that tabulate numbers handle true NA values differently, but handle character values the same way.

Examples

1
2
3
4
5
tbl_val( c(0.1234, 1.234, 12.34, 123.4, 1234) )

tbl_val(x = c(0.995, 9.995, 99.95, 2003.5),
        breaks = c(1,10,100,Inf),
        decimals = c(2,2,1,0))

bcjaeger/tblStrings documentation built on Aug. 17, 2020, 9:36 a.m.