format_num {psychmeta} | R Documentation |
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: |
leading0 |
How to print leading zeros on decimals. Can be logical to print ( |
drop0integer |
Logical. Should trailing decimal zeros be dropped for integers? |
neg.sign |
Character to use as negative sign. Defaults to minus-sign ( |
pos.sign |
Character to use as positive sign. Set to |
big.mark |
Character to mark between each |
big.interval |
See |
small.mark |
Character to mark between each |
small.interval |
See |
na.mark |
Character to replace |
lgl.mark |
A length 2 vector containing characters to replace |
inf.mark |
A length 2 vector containing characters to replace |
Examples
# 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 separate 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 commas to separate
# large digit groups, set big.mark = ",":
format_num(x = 10000, big.mark = ",")