Gilgamesh
Gilgamesh

Reputation: 629

How to round by an "interval"

Let VetA = c(3.12,3.13,3.23,3.29,3.46) and I wish to round it by 0.05 interval. A solution could be:

round(vetA/5,digits = 2)*5)

Which will give me c(3.1, 3.15, 3.25, 3.3, 3.45).

However, what if my desire changes, and now I would like to round them by 0.25. What should I do? Also, this thinking could grow much more, as I could as well wish to round by 15 interval numbers like 12,15.2,20,55,24.

For that reason, could you help me thinking in a generic function? I'm having troubles modeling just the grouping by 0.25, imagine making a more generic one.

Upvotes: 1

Views: 149

Answers (1)

Alejandro Andrade
Alejandro Andrade

Reputation: 2216

A plyr solution is to use round_any

library(plyr)
VetA = c(3.12,3.13,3.23,3.29,3.46)
round_any(VetA,0.25,f = round)

Upvotes: 2

Related Questions