Learn R Programming

psychmeta (version 2.3.4)

format_num: Format numbers for presentation

Description

A function to format numbers and logical values as characters for display purposes. Includes control over formatting of decimal digits, leading zeros, sign characters, and characters to replace logical, NA, NaN, and Inf values. Factors are converted to strings. Strings are returned verbatim.

Usage

format_num(x, digits = 2L, decimal.mark = getOption("OutDec"),
              leading0 = "conditional", drop0integer = FALSE,
              neg.sign = "\u2212", pos.sign = "figure",
              big.mark = "\u202F", big.interval = 3L,
              small.mark = "\u202F", small.interval = 3L,
              na.mark = "\u2014", lgl.mark = c("+", "\u2212"),
              inf.mark = c("+\u221E", "\u2212\u221E") )

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) if a column contains any absolute values greater than 1 and suppressed otherwise. If "figure_html", the same as "figure", but using the HTML entity for figure space (useful for Windows users in some locales).

drop0integer

Logical. Should trailing decimal zeros be dropped for integers?

neg.sign

Character to use as negative sign. Defaults to minus-sign (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) if a column contains any negative numbers and suppressed otherwise. If "figure_html", the same as "figure", but using the HTML entity for figure space (useful for Windows users in some locales).

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).

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).

small.interval

See small.mark above; defaults to 3.

na.mark

Character to replace NA and NaN values. Defaults to em-dash (U+2014))

lgl.mark

A length 2 vector containing characters to replace TRUE and FALSE. Defaults to c("+", "U+2212").

inf.mark

A length 2 vector containing characters to replace Inf and -Inf. Defaults to c("+U+221e", "U+2212U+221e").

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