Round to multiple of any number.

round_any(x, accuracy, f = round)

Arguments

x

numeric or date-time (POSIXct) vector to round

accuracy

number to round to; for POSIXct objects, a number of seconds

f

rounding function: floor, ceiling or round

Examples

round_any(135, 10)
#> [1] 140
round_any(135, 100)
#> [1] 100
round_any(135, 25)
#> [1] 125
round_any(135, 10, floor)
#> [1] 130
round_any(135, 100, floor)
#> [1] 100
round_any(135, 25, floor)
#> [1] 125
round_any(135, 10, ceiling)
#> [1] 140
round_any(135, 100, ceiling)
#> [1] 200
round_any(135, 25, ceiling)
#> [1] 150
round_any(Sys.time() + 1:10, 5)
#> [1] "2019-12-31 11:47:00 UTC" "2019-12-31 11:47:00 UTC" #> [3] "2019-12-31 11:47:05 UTC" "2019-12-31 11:47:05 UTC" #> [5] "2019-12-31 11:47:05 UTC" "2019-12-31 11:47:05 UTC" #> [7] "2019-12-31 11:47:05 UTC" "2019-12-31 11:47:10 UTC" #> [9] "2019-12-31 11:47:10 UTC" "2019-12-31 11:47:10 UTC"
round_any(Sys.time() + 1:10, 5, floor)
#> [1] "2019-12-31 11:47:00 UTC" "2019-12-31 11:47:00 UTC" #> [3] "2019-12-31 11:47:00 UTC" "2019-12-31 11:47:00 UTC" #> [5] "2019-12-31 11:47:00 UTC" "2019-12-31 11:47:05 UTC" #> [7] "2019-12-31 11:47:05 UTC" "2019-12-31 11:47:05 UTC" #> [9] "2019-12-31 11:47:05 UTC" "2019-12-31 11:47:05 UTC"
round_any(Sys.time(), 3600)
#> [1] "2019-12-31 12:00:00 UTC"