This picks out the order of magnitude of each number in a vector and returns a formatted abbreviation, such as 1230 --> 1.2k. It handles abbreviations through the trillions; after that, it's suggested that you switch over to scientific notation. The convenience of this function over those from the scales
package is that you can mix & match orders of magnitude.
big_number(x, digits = 2)
big_money(x, digits = 2, currency = "$")
A numeric vector
Number of digits (significant figures) to keep; defaults to 2
Symbol to use to denote currency; defaults "$"
A character vector
Its sibling big_money
tacks on a currency symbol, placed after a negative sign if applicable.
big_number(c(123, 12345, 1234567), digits = 3)
#> [1] "123" "12.3k" "1.23M"
big_number(c(-12000, 15000, 16000, 77777))
#> [1] "-12k" "15k" "16k" "78k"
big_money(c(-12000, 15000, 16000, 2e6))
#> [1] "-$12k" "$15k" "$16k" "$2M"
# take care with your significant digits choices
purrr::map_chr(1:3, ~big_number(987, digits = .))
#> [1] "1k" "990" "987"