f_num: Format Digits

Description Usage Arguments Value Examples

View source: R/f_num.R

Description

Remove leading zeros and standardize number of digits. A workhorse for the numform package.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
f_num(
  x,
  digits = getOption("numformdigits"),
  p,
  s,
  pad.char = NA,
  zero = NULL,
  retain.leading.zero = FALSE,
  ...
)

ff_num(...)

Arguments

x

A vector of numbers (or string equivalents).

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.

p

A string to paste at the beginning of the output from f_num.

s

A string to paste at the end of the output from f_num.

pad.char

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

zero

A value to insert in for zero values.

retain.leading.zero

logical. If TRUE then leading zeros before a decimal place are retained.

...

ignored.

Value

Returns a string of publication ready digits.

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
f_num(c(0.0, 0, .2, -00.02, 1.122222, pi))
f_num(rnorm(10))
f_num(rnorm(20, 100, 200), 0)
f_num(c("-0.23", "0", ".23"))

## Percents
f_num(c(30, 33.45, .1), 3, s="%")

## Money
f_num(c(30, 33.45, .1), 2, p="$")

## Units
f_num(c(30, 33.45, .1), 2, s=" in.<sup>2</sup>")
f_num(c(30, 33.45, .1), 2, p="&Chi;<sup>2</sup>=")

## Not run: 
library(dplyr)

is.int <- function(x) !all(x %% 1 == 0)

mtcars %>%
    mutate_if(.funs = f_num, is.int)

df <- data.frame(x = -10:10, y = (-10:10)/10)

ggplot(df, aes(x, y))+
    geom_point() +
    scale_y_continuous(labels = ff_num(zero = 0))

## End(Not run)

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