Skip to content

Instantly share code, notes, and snippets.

@jcheng5
Created January 29, 2019 03:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jcheng5/5a90fd10eb255e978944b319ac850959 to your computer and use it in GitHub Desktop.
Save jcheng5/5a90fd10eb255e978944b319ac850959 to your computer and use it in GitHub Desktop.
num_suffix <- function(x, base = 1000, suffixes = c("K", "M", "B", "T")) {
if (length(suffixes) == 0) {
tibble(
scale_by = rep_len(1, length(x)),
suffix = rep_len("", length(x))
)
}
i <- floor(log(abs(x), base = base))
i <- pmin(i, length(suffixes))
tibble(
scale_by = base ^ i,
suffix = ifelse(i == 0, "", suffixes[pmax(1, i)])
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment