Skip to content

Instantly share code, notes, and snippets.

@simonthompson99
Last active January 15, 2021 12:46
Show Gist options
  • Save simonthompson99/acdf1248f1e13e19da6c6836b7a04303 to your computer and use it in GitHub Desktop.
Save simonthompson99/acdf1248f1e13e19da6c6836b7a04303 to your computer and use it in GitHub Desktop.
[Pretty Round Up/Down] Round a number up or down to a pretty, convenient number #r
#-- function to round up or down a number nicely
roundNicely <- function(x, nice=c(1,1.5,2,2.5,3,4,5,6,8,10), down = TRUE) {
stopifnot(length(x) == 1)
if(down){
10^floor(log10(x)) * nice[which(x <= 10^floor(log10(x)) * nice)[1] - 1]
} else {
10^floor(log10(x)) * nice[which(x <= 10^floor(log10(x)) * nice)[1]]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment