Public-Health-Scotland / phsmethods

An R package to standardise methods used in Public Health Scotland (https://public-health-scotland.github.io/phsmethods/)
https://public-health-scotland.github.io/phsmethods/
54 stars 13 forks source link

Function: Round to nearest n #62

Closed Moohan closed 3 years ago

Moohan commented 3 years ago

Simple function for rounding to a nearest number, example usage could be to round all output figures to the nearest 5 for suppression.

round_nearest_n <- function(x, n) {
  rounded <- round(x / n) * n) 
  return(rounded)
}
davidc92 commented 3 years ago

A function for this already exists within the tidyverse suite: https://www.rdocumentation.org/packages/plyr/versions/1.8.6/topics/round_any