Skip to contents

This is a collection of small utilities for formatting numbers, especially for use in charts and tables. Many of them are simply wrappers around scales functions.

Usage

percent100(x, ...)

percent_lt1(x, ...)

round100(x, ...)

Arguments

x

A numeric vector

...

Additional arguments to pass to underlying scales functions, such as scales::percent and scales::number.

Value

A string vector of formatted numbers.

Details

round100 takes a decimal, such as a percentage, multiplies by 100, and rounds, e.g. 0.251 becomes "25", useful for then pasting with additional text (see examples).

percent100 adds on to round100 by appending a percent sign, e.g. 0.251 becomes "25%".

percent_lt1 takes an additional step, replacing any values below 1 percent with "<1%", useful for suppressing small numbers.

Examples

paste("The value is", round100(0.251), "percent.") # outputs "The value is 25 percent."
#> [1] "The value is 25 percent."

percent100(c(0.25, 0.251, 0.008)) # outputs "25%", "25%", "1%"
#> [1] "25%" "25%" "1%" 

percent_lt1(c(0.25, 0.251, 0.008)) # outputs "25%", "25%", "<1%"
#> [1] "25%" "25%" "<1%"