f_denom: Abbreviate Numbers

Description Usage Arguments Value Examples

View source: R/f_denom.R

Description

Use the denomination abbreviations K (thousands), M (millions), and B (billions) with abbreviated numbers.
f_denom - Auto-detect the maximum denomination and attempt to use it (if max(x) is < 1K then x is returned).

f_trills - Force the abbreviation to the trillions denomination (B).

f_bills - Force the abbreviation to the billions denomination (B).

f_mills - Force the abbreviation to the millions denomination (B).

f_thous - Force the abbreviation to the thousands denomination (B).

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
f_denom(
  x,
  relative = 0,
  prefix = "",
  pad.char = ifelse(prefix == "", NA, " "),
  less.than.replace = FALSE,
  mix.denom = FALSE,
  ...
)

ff_denom(...)

f_trills(
  x,
  relative = 0,
  digits = -12,
  prefix = "",
  pad.char = ifelse(prefix == "", NA, " "),
  less.than.replace = FALSE,
  ...
)

ff_trills(...)

f_bills(
  x,
  relative = 0,
  digits = -9,
  prefix = "",
  pad.char = ifelse(prefix == "", NA, " "),
  less.than.replace = FALSE,
  ...
)

ff_bills(...)

f_mills(
  x,
  relative = 0,
  digits = -6,
  prefix = "",
  pad.char = ifelse(prefix == "", NA, " "),
  less.than.replace = FALSE,
  ...
)

ff_mills(...)

f_thous(
  x,
  relative = 0,
  digits = -3,
  prefix = "",
  pad.char = ifelse(prefix == "", NA, " "),
  less.than.replace = FALSE,
  ...
)

ff_thous(...)

Arguments

x

A vector of large numbers.

relative

A factor relative to the current digits being rounded. For example relative = -1 moves one to the left while relative = 1 moves one to the right.

prefix

A string to append to the front of elements.

pad.char

A character to use for leading padding if lengths of output are unequal. Use NA to forgo padding.

less.than.replace

logical. If TRUE values lower than lowest place value will be replaced with a less than sign followed by the integer representation of the place value. For example, if "$0K" then replacement will be "<1K".

mix.denom

logical. If TRUE then denominations can be mixed. Typically this is not a good idea for the sake of comparison. It is most useful when there is a total row which is a sum of the column and this value's denomination exceeds the denomination of the rest of the column.

digits

The number of digits to round to. Actual digits calculated as digits + relative.

...

ignored.

Value

Returns an abbreviated vector of numbers.

Examples

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
f_denom(c(12345, 12563, 191919), prefix = '$')
f_denom(c(12345, 12563, 191919), prefix = '$', pad.char = '')
f_denom(c(1234365, 122123563, 12913919), prefix = '$')
f_denom(c(12343676215, 122126763563, 1291673919), prefix = '$')
f_denom(c(NA, 2, 12343676215, 122126763563, 1291673919), prefix = '$')
f_denom(c(NA, 2, 123436, 122126763, 1291673919), prefix = '$', mix.denom = TRUE)
f_denom(c(NA, 2, 12343676215, 122126763563, 1291673919), prefix = '$', pad.char = '')
f_denom(c(NA, 2, 12343676215, 122126763563, 1291673919), relative = 1, prefix = '$')
f_denom(c(NA, 2, 12343676215, 122126763563, 1291673919), relative = 9, prefix = '$')
f_denom(c(NA, 2, 12343676215, 122126763563, 1291673919), less.than.replace = TRUE)

f_thous(1234)
f_thous(12345)
f_thous(123456)
f_mills(1234567)
f_mills(12345678)
f_mills(123456789)
f_bills(1234567891)
f_bills(12345678912)
f_bills(123456789123)

f_bills(123456789123, -1) # round to tens
f_bills(123456789123, -2) # round to hundreds
f_bills(123456789123, +1) # round to tenths
f_bills(123456789123, +2) # round to hundreths

x <- c(3886902.8696, 4044584.0424, 6591893.2104, 591893.2104, -3454678)
f_mills(x)
f_mills(x, 1)
f_mills(x, 1, prefix = '$')
f_mills(x, 1, prefix = '$', pad.char = '0')

## Not run: 
if (!require("pacman")) install.packages("pacman")
pacman::p_load(tidyverse, magrittr)

f_bills(123456789123, -2) %>%
    f_prefix("$")


data_frame(
    revenue = rnorm(100, 500000, 50000),
    deals = sample(20:50, 100, TRUE)
) %>%
    mutate(
        dollar = f_dollar(revenue, digits = -3),
        thous = f_thous(revenue),
        thous_dollars = f_thous(revenue, prefix = '$')
    ) %T>%
    print() %>%
    ggplot(aes(deals, revenue)) +
        geom_point() +
        geom_smooth() +
        scale_y_continuous(label = ff_thous(prefix = '$') )

data_frame(
    revenue = rnorm(10000, 500000, 50000),
    date = sample(seq(as.Date('1999/01/01'), as.Date('2000/01/01'), by="day"), 10000, TRUE),
    site = sample(paste("Site", 1:5), 10000, TRUE)
) %>%
    mutate(
        dollar = f_dollar(revenue, digits = -3),
        thous = f_thous(revenue),
        thous_dollars = f_thous(revenue, prefix = '$'),
        abb_month = f_month(date),
        abb_week = factor(f_weekday(date, distinct = TRUE),
        levels = c('Su', 'M', 'T', 'W', 'Th', 'F', 'S'))
    ) %T>%
    print() %>%
    ggplot(aes(abb_week, revenue)) +
        geom_jitter(width = .2, height = 0, alpha = .2) +
        scale_y_continuous(label = ff_thous(prefix = '$'))+
        facet_wrap(~site)

set.seed(10)
data_frame(
    w = paste(constant_months, rep(2016:2017, each = 12))[1:20] ,
    x = rnorm(20, 200000, 75000)
) %>%
    {
        a <- .
        rbind(
            a,
            a %>%
                mutate(w = 'Total') %>%
                group_by(w) %>%
                summarize(x = sum(x))
        )
    } %>%
    mutate(
        y = f_denom(x, prefix = '$'),
        z = f_denom(x, mix.denom = TRUE, prefix = '$')
    )  %>%
    data.frame(stringsAsFactors = FALSE, check.names = FALSE) %>%
    pander::pander(split.tables = Inf, justify = alignment(.))

## Scale with mixed units
library(tidyverse)
library(numform)

dat <- data_frame(
    Value = c(111, 2345, 34567, 456789, 1000001, 1000000001),
    Time = 1:6
)

## Uniform units
ggplot(dat, aes(Time, Value)) +
    geom_line() +
    scale_y_continuous(labels = ff_denom( prefix = '$'))

## Mixed units
ggplot(dat, aes(Time, Value)) +
    geom_line() +
    scale_y_continuous(labels = ff_denom(mix.denom = TRUE, prefix = '$', pad.char = ''))

## End(Not run)

Example output

[1] "$ 12K" "$ 13K" "$192K"
[1] "$12K"  "$13K"  "$192K"
[1] "$  1M" "$122M" "$ 13M"
[1] "$ 12B" "$122B" "$  1B"
[1] NA      "$  0B" "$ 12B" "$122B" "$  1B"
[1] NA      "$   2" "$123K" "$122M" "$  1B"
[1] NA      "$0B"   "$12B"  "$122B" "$1B"  
[1] NA        "$   .0B" "$ 12.3B" "$122.1B" "$  1.3B"
[1] NA                "$   .000000002B" "$ 12.343676215B" "$122.126763563B"
[5] "$  1.291673919B"
[1] NA     "<1B"  "12B"  "122B" "1B"  
[1] "1K"
[1] "12K"
[1] "123K"
[1] "1M"
[1] "12M"
[1] "123M"
[1] "1B"
[1] "12B"
[1] "123B"
[1] "120B"
[1] "100B"
[1] "123.5B"
[1] "123.46B"
[1] "4M"  "4M"  "7M"  "1M"  "-3M"
[1] "3.9M"  "4.0M"  "6.6M"  ".6M"   "-3.5M"
[1] "$ 3.9M" "$ 4.0M" "$ 6.6M" "$  .6M" "$-3.5M"
[1] "$03.9M" "$04.0M" "$06.6M" "$00.6M" "$-3.5M"
Loading required package: pacman
[1] "$100B"
# A tibble: 100 x 5
   revenue deals dollar  thous thous_dollars
     <dbl> <int> <chr>   <chr> <chr>        
 1 423088.    20 $423000 423K  $423K        
 2 419251.    25 $419000 419K  $419K        
 3 570116.    29 $570000 570K  $570K        
 4 513192.    39 $513000 513K  $513K        
 5 495684.    28 $496000 496K  $496K        
 6 621413.    36 $621000 621K  $621K        
 7 543114.    37 $543000 543K  $543K        
 8 612597.    35 $613000 613K  $613K        
 9 435416.    38 $435000 435K  $435K        
10 473370.    23 $473000 473K  $473K        
# ... with 90 more rows
`geom_smooth()` using method = 'loess' and formula 'y ~ x'
Warning message:
`data_frame()` is deprecated, use `tibble()`.
This warning is displayed once per session. 
# A tibble: 10,000 x 8
   revenue date       site   dollar  thous thous_dollars abb_month abb_week
     <dbl> <date>     <chr>  <chr>   <chr> <chr>         <chr>     <fct>   
 1 480994. 1999-05-31 Site 5 $481000 481K  $481K         M         M       
 2 547592. 1999-03-26 Site 5 $548000 548K  $548K         M         F       
 3 466275. 1999-02-28 Site 4 $466000 466K  $466K         F         Su      
 4 475457. 1999-11-08 Site 5 $475000 475K  $475K         N         M       
 5 527192. 1999-12-24 Site 4 $527000 527K  $527K         D         F       
 6 428555. 1999-05-12 Site 2 $429000 429K  $429K         M         W       
 7 471544. 1999-05-01 Site 5 $472000 472K  $472K         M         S       
 8 481073. 1999-05-27 Site 4 $481000 481K  $481K         M         Th      
 9 532061. 1999-04-10 Site 5 $532000 532K  $532K         A         S       
10 505910. 1999-04-19 Site 4 $506000 506K  $506K         A         M       
# ... with 9,990 more rows

----------------------------------------
w                        x     y       z
---------------- --------- ----- -------
January 2016        201406   $0M   $201K

February 2016       186181   $0M   $186K

March 2016           97150   $0M   $ 97K

April 2016          155062   $0M   $155K

May 2016            222091   $0M   $222K

June 2016           229235   $0M   $229K

July 2016           109394   $0M   $109K

August 2016         172724   $0M   $173K

September 2016       78000   $0M   $ 78K

October 2016        180764   $0M   $181K

November 2016       282633   $0M   $283K

December 2016       256684   $0M   $257K

January 2017        182132   $0M   $182K

February 2017       274058   $0M   $274K

March 2017          255604   $0M   $256K

April 2017          206701   $0M   $207K

May 2017            128379   $0M   $128K

June 2017           185364   $0M   $185K

July 2017           269414   $0M   $269K

August 2017         236223   $0M   $236K

Total              3909201   $4M    $ 4M
----------------------------------------

Warning messages:
1: In digit_warn(x, "f_mills", 3) :
  Detected one or more elements with a larger denomination.
  Consider using `f_mills` function instead.
2: In digit_warn(x, "f_mills", 3) :
  Detected one or more elements with a larger denomination.
  Consider using `f_mills` function instead.

numform documentation built on Oct. 10, 2021, 1:10 a.m.