round2: Rounding

Description Usage Arguments Value Author(s) References Examples

View source: R/round.R

Description

round2 - By default R's round function uses the 'round half to even' method. This function (taken from https://stackoverflow.com/a/12688836/1000343) rounds half up.

round_any - This tooling lets you round to fractional values, not just whole numbers. Code adapted from https://stackoverflow.com/a/8665247/1000343.

Usage

1
2
3
round2(x, digits = 0, ...)

round_any(x, accuracy, f = round2, ...)

Arguments

x

A vector of digits.

digits

The number of decimal places to round to.

accuracy

Number to round to.

f

A function to round (e.g., round, ceiling, floor). efaults to round2.

...

ignored.

Value

round2 - Returns numeric vector half rounded up.

round_any - Returns a numeric vector or rounded fractional values.

Author(s)

Kohske Takahashi

References

https://stackoverflow.com/a/12688836/1000343
https://stackoverflow.com/a/8665247/1000343

Examples

1
2
3
4
5
6
7
8
data.frame(
    orig = .5 + (0:8),
    round = round(.5 + (0:8)),
    round2 = round2(.5 + (0:8))
)

round_any(c(.123, 1.234, 4, 4.715), .5)
round_any(c(.123, 1.234, 4, 4.715), .25)

Example output

  orig round round2
1  0.5     0      1
2  1.5     2      2
3  2.5     2      3
4  3.5     4      4
5  4.5     4      5
6  5.5     6      6
7  6.5     6      7
8  7.5     8      8
9  8.5     8      9
[1] 0.0 1.0 4.0 4.5
[1] 0.00 1.25 4.00 4.75

numform documentation built on Oct. 10, 2021, 1:10 a.m.