Number formatters to apply to a column in a dataframe. Helpful for displaying tibbles in console or in conjunction with make_flextable. Based off the formattable package.

format_number(tbl, ..., digits = 0)

format_percent(tbl, ..., digits = 0)

format_currency(tbl, ..., symbol = "yen", digits = 0)

Arguments

tbl

dataframe

...

tidyselect.

digits

integer. trailing digits

symbol

chr. currency symbol

Value

dataframe dataframe

Details

  • format_number formats a number accounting style by inserting commas. default selection is integer columns

  • format_percent formats a number as a percentage. default selection is numeric columns in between -1 and 1.

  • format_currency formats a monetary value with the currency symbol. default currency symbol is yen.

Examples


tibble::tibble(
y = seq(1000L, 10000L, by = 1000L),
z = c(-.59, -.23, -.11, 0, .1, .21, .3, .4, .6, .9),
w = c(.1, 1.4, .23, -.10, 0, -2.3, .2,.3,.4,.5)) -> tbl1

tbl1
#> # A tibble: 10 × 3
#>        y     z     w
#>    <int> <dbl> <dbl>
#>  1  1000 -0.59  0.1 
#>  2  2000 -0.23  1.4 
#>  3  3000 -0.11  0.23
#>  4  4000  0    -0.1 
#>  5  5000  0.1   0   
#>  6  6000  0.21 -2.3 
#>  7  7000  0.3   0.2 
#>  8  8000  0.4   0.3 
#>  9  9000  0.6   0.4 
#> 10 10000  0.9   0.5 

# automatically formats the integer column
tbl1 %>%
format_number()
#> # A tibble: 10 × 3
#>    y              z     w
#>    <formttbl> <dbl> <dbl>
#>  1 1,000      -0.59  0.1 
#>  2 2,000      -0.23  1.4 
#>  3 3,000      -0.11  0.23
#>  4 4,000       0    -0.1 
#>  5 5,000       0.1   0   
#>  6 6,000       0.21 -2.3 
#>  7 7,000       0.3   0.2 
#>  8 8,000       0.4   0.3 
#>  9 9,000       0.6   0.4 
#> 10 10,000      0.9   0.5 

# automatically formats to yen
tbl1 %>%
format_currency(y)
#> # A tibble: 10 × 3
#>    y              z     w
#>    <formttbl> <dbl> <dbl>
#>  1 ¥1,000     -0.59  0.1 
#>  2 ¥2,000     -0.23  1.4 
#>  3 ¥3,000     -0.11  0.23
#>  4 ¥4,000      0    -0.1 
#>  5 ¥5,000      0.1   0   
#>  6 ¥6,000      0.21 -2.3 
#>  7 ¥7,000      0.3   0.2 
#>  8 ¥8,000      0.4   0.3 
#>  9 ¥9,000      0.6   0.4 
#> 10 ¥10,000     0.9   0.5 

# automatically detects columns between -1 and 1 to convert to percentages
tbl1 %>%
format_percent()
#> # A tibble: 10 × 3
#>        y z              w
#>    <int> <formttbl> <dbl>
#>  1  1000 -59%        0.1 
#>  2  2000 -23%        1.4 
#>  3  3000 -11%        0.23
#>  4  4000 0%         -0.1 
#>  5  5000 10%         0   
#>  6  6000 21%        -2.3 
#>  7  7000 30%         0.2 
#>  8  8000 40%         0.3 
#>  9  9000 60%         0.4 
#> 10 10000 90%         0.5 

# select specific columns to convert.
tbl1 %>%
format_percent(z, w)
#> # A tibble: 10 × 3
#>        y z          w         
#>    <int> <formttbl> <formttbl>
#>  1  1000 -59%       10%       
#>  2  2000 -23%       140%      
#>  3  3000 -11%       23%       
#>  4  4000 0%         -10%      
#>  5  5000 10%        0%        
#>  6  6000 21%        -230%     
#>  7  7000 30%        20%       
#>  8  8000 40%        30%       
#>  9  9000 60%        40%       
#> 10 10000 90%        50%