round_to_nearest: Round numbers to a nearest "pretty" value

View source: R/number_tools.R

round_to_nearestR Documentation

Round numbers to a nearest "pretty" value

Description

It's sometimes useful to round numbers to a predictable range, e.g. when preparing graph axes or perhaps for filtering or presenting data. This function can round numbers to the nearest, say, 0.5.

Usage

round_to_nearest(num, to, dir = NULL)

Arguments

num

(Numeric) A vector of numbers.

to

(Numeric) What interval should num be rounded to?

dir

(Optional Char: "up", "down", "both", or omitted) Controls the rounding function used. If omitted or "both", the round() function is used. If "up", ceiling() is used. If "down", floor() is used.

Value

A numeric vector.

Authors

Source

https://stackoverflow.com/a/8665247/5578429

Examples

vec <- c(1.739006, 2, -1.4, 1.05, 1.90, 3.826)
round_to_nearest(vec, 0.5)
#> [1]  1.5  2.0 -1.5  1.0  2.0  4.0


DesiQuintans/desiderata documentation built on April 9, 2023, 5:43 a.m.