Learn R Programming

psychmeta (version 2.1.1)

format_num: Format numbers for presentation

Description

A function to format decimal digits, leading zeros, and sign characters.

Usage

format_num(x, digits = 2L, decimal.mark = getOption("OutDec"),
  leading0 = "conditional", drop0integer = FALSE, neg.sign = "minus",
  pos.sign = "figure", big.mark = "thinspace", big.interval = 3L,
  small.mark = "thinspace", small.interval = 3L)

Arguments

x

A vector, matrix, or data.frame of numbers to format

digits

The number of decimal digits desired (used strictly; default: 2)

decimal.mark

The character to use for the decimal point (defaults to locale default: getOption("OutDec"))

leading0

How to print leading zeros on decimals. Can be logical to print (TRUE) or suppress (FALSE) leading zeros or a character string to subsitute for leading zeros. If "conditional" (default), leading zeros are shown if a column contains any absolute values greater than 1 and suppressed otherwise. If "figure", leading zeros are replaced with a figure space (U+2007: "<U+2007>") if a column contains any absolute values greater than 1 and suppressed otherwise.

drop0integer

Logical. Should trailing decimal zeros be dropped for integers?

neg.sign

Character to use as negative sign. Defaults to minus-sign (U+2212: "<U+2212>").

pos.sign

Character to use as positive sign. Set to FALSE to suppress. If "figure" (default), the positive sign is a figure-space (U+2007: "<U+2007>") if a column contains any negative numbers and suppressed otherwise.

big.mark

Character to mark between each big.interval digits before the decimal point. Set to FALSE to suppress. Defaults to the SI/ISO 31-0 standard-recommened thin-spaces (U+202F: "<U+202F>").

big.interval

See big.mark above; defaults to 3.

small.mark

Character to mark between each small.interval digits after the decimal point. Set to FALSE to suppress. Defaults to the SI/ISO 31-0 standard-recommened thin-spaces (U+202F: "<U+202F>").

small.interval

See small.mark above; defaults to 3.

Examples

Run this code
# NOT RUN {
# format_num() converts numeric values to characters with the specified formatting options.
# By default, thousands digit groups are separated by thin spaces, negative signs are replaced
# with minus signs, and positive signs and leading zeros are replaced with figure spaces
# (which have the same width as numbers and minus signs). These options ensure that all
# results will align neatly in columns when tabled.
format_num(x = c(10000, 1000, 2.41, -1.20, 0.41, -0.20))

# By default, format_num() uses your computer locale's default decimal mark as
# the decimal point. To force the usage of "." instead (e.g., for submission to
# a U.S. journal), set decimal.mark = ".":
format_num(x = .41, decimal.mark = ".")

# By default, format_num() separates groups of large digits using thin spaces.
# This is following the international standard for scientific communication (SI/ISO 31-0),
# which advises against using "." or "," to seprate digits because doing so can lead
# to confusion for human and computer readers because "." and "," are also used
# as decimal marks in various countries. If you prefer to use commmas to separate
# large digit groups, set big.mark = ",":
format_num(x = 10000, big.mark = ",")
# }

Run the code above in your browser using DataLab