Learn R Programming

numform (version 0.7.0)

f_num: Format Digits

Description

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

Usage

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

Run this code
# NOT RUN {
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="Χ<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))
# }

Run the code above in your browser using DataLab