Learn R Programming

numform (version 0.7.0)

f_percent: Format Percentages

Description

f_percent - A wrapper for f_num that formats percent values as labeled percentages.

f_prop2percent - A wrapper for f_num that formats proportions as labeled percentages.

f_pp - A wrapper for f_prop2percent that requires less typing and has digits set to 0 by default.

Usage

f_percent(
  x,
  digits = getOption("numformdigits"),
  less.than.replace = FALSE,
  s = "%",
  ...
)

ff_percent(...)

f_prop2percent( x, digits = getOption("numformdigits"), less.than.replace = FALSE, s = "%", ... )

ff_prop2percent(...)

f_pp(x, digits = 0, less.than.replace = FALSE, s = "%", ...)

ff_pp(...)

Arguments

x

A vector of proportions.

digits

The number of digits to use. Defaults to 1. Can be set globally via: options(numformdigits = n) where n is the number of digits beyond the decimal point to include.

less.than.replace

logical. If TRUE values lower than lowest place value, specified by digits, will be replaced with a less than sign followed by the double representation of the place value specified by digits. For example, if digits = 0 then replacement will be "<1%" or if digits = 2 then replacement will be "<.01%".

s

A string to paste at the end of the output from f_num. Defaults to percent sign. This could be useful, for example, to turn a single percent sign into an escaped version for LaTeX output.

Other values passed to f_num.

Value

Returns a string of publication ready digits.

See Also

f_num

Examples

Run this code
# NOT RUN {
f_percent(c(30, 33.45, .1))
f_percent(c(30, 33.45, .1), 1)
f_percent(c(0.0, 0, .2, -00.02, 1.122222, pi))
f_prop2percent(c(.30, 1, 1.01, .33, .222, .01))
f_pp(c(.30, 1, 1.01, .33, .222, .01))

f_percent(c(30, 33.45, .1), digits = 0, less.than.replace = TRUE)
## Escaped for LaTeX:
f_percent(c(30, 33.45, .1), digits = 0, less.than.replace = TRUE, s = '\\%')
f_prop2percent(c(.30, 1, 1.01, .33, .222, .01, .0001, NA), digits = 0,
    less.than.replace = TRUE)

# }
# NOT RUN {
library(tidyverse)

mtcars %>%
    count(cyl, gear) %>%
    group_by(cyl) %>%
    mutate(prop = n/sum(n)) %>%
    ggplot(aes(gear, prop)) +
        geom_bar(stat = 'identity') +
        facet_wrap(~cyl, ncol = 1) +
        scale_y_continuous(labels = ff_prop2percent(digits = 0))
# }

Run the code above in your browser using DataLab