Skip to contents

Formats proportions as percentages without a percent sign. Values of 10% and above get digits decimal places, values below 10% get one more, and values too small to show are displayed as e.g. "<0.1".

Usage

style_percent(
  x,
  digits = 0,
  big.mark = ifelse(decimal.mark == ",", " ", ","),
  decimal.mark = getOption("OutDec"),
  prefix = "",
  suffix = "",
  na = NA_character_,
  ...
)

Arguments

x

(numeric)
Proportions between 0 and 1.

digits

(integer)
Number of decimal places. Recycled to the length of x.

big.mark

(string)
Character used between every three digits to the left of the decimal mark.

decimal.mark

(string)
Character used as the decimal mark.

prefix, suffix

(string)
Text added before and after each formatted value.

na

(string)
Replacement for missing values.

...

Not used. Accepted so that formatting functions can be called interchangeably.

Value

A character vector the same length as x.

Examples

percent_vals <- c(-1, 0, 0.0001, 0.005, 0.01, 0.10, 0.45356, 0.99, 1.45)
style_percent(percent_vals)
#> [1] NA     "0"    "<0.1" "0.5"  "1.0"  "10"   "45"   "99"   "145" 
style_percent(percent_vals, suffix = "%", digits = 1)
#> [1] NA       "0%"     "0.01%"  "0.50%"  "1.00%"  "10.0%"  "45.4%"  "99.0%" 
#> [9] "145.0%"