Learn R Programming

numform (version 0.6.4)

round2: Rounding

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

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.

References

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

Examples

Run this code
# NOT RUN {
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)
# }

Run the code above in your browser using DataLab