
Last chance! 50% off unlimited learning
Sale ends in
Formatting numbers in R often degenerates into a major intellectual challenge for us little minds down here in the Valley of Tears. We have several functions available and quite often it's hard to work out which one to use, when an uncommon option is needed. This function wraps those functions and tries to offer a simpler, less technical, but still flexible interface.
There's also an easygoing interface for format templates, defined as a list consisting of any accepted format features. In this way templates can be defined outside and may easily be changed or modified later.
Format(x, digits = NULL, sci = NULL, big.mark = NULL,
leading = NULL, zero.form = NULL, na.form = NULL,
fmt = NULL, align = NULL, width = NULL, lang = NULL,
eps = .Machine$double.eps, ...)# S3 method for table
Format(x, digits = NULL, sci = NULL, big.mark = NULL,
leading = NULL, zero.form = NULL, na.form = NULL,
fmt = NULL, align = NULL, width = NULL, lang = NULL,
eps = .Machine$double.eps, ...)
# S3 method for matrix
Format(x, digits = NULL, sci = NULL, big.mark = NULL,
leading = NULL, zero.form = NULL, na.form = NULL,
fmt = NULL, align = NULL, width = NULL, lang = NULL,
eps = .Machine$double.eps, ...)
# S3 method for default
Format(x, digits = NULL, sci = NULL, big.mark = NULL,
leading = NULL, zero.form = NULL, na.form = NULL,
fmt = NULL, align = NULL, width = NULL, lang = NULL,
eps = .Machine$double.eps, ...)
Fmt(...)
as.fmt(...)
as.CDateFmt(fmt)
an atomic numerical, typically a vector of real numbers or a matrix of numerical values. Factors will be converted to strings.
integer, the desired (fixed) number of digits after the decimal point. Unlike formatC
you will always get this number of digits even if the last digit is 0.
integer. The power of 10 to be set when deciding to print numeric values in exponential notation. Fixed notation will be preferred unless the number is larger than 10^scipen. If just one value is set it will be used for the left border 10^(-scipen) as well as for the right one (10^scipen). A negative and a positive value can also be set independently. Default is
getOption("scipen")
, whereas scipen=0
is overridden.
character; if not empty used as mark between every 3 decimals before the decimal point. Default is "" (none).
character string that can be used for setting leading zeros. "000" would make sure that at least 3 digits on the left side will be printed. Setting leading to "drop" will yield results like ".452" for 0.452. The default NULL
will leave the numbers as they are.
character, string specifying how zeros should be specially formatted. Useful for pretty printing 'sparse' objects.
If set to NULL
(default) no special action will be taken.
character, string specifying how NA
s should be specially formatted.
If set to NULL
(default) no special action will be taken.
either a format string, allowing to flexibly define special formats or an object of class fmt
, consisting of a list of Format
arguments. See Details.
the character on whose position the strings will be aligned. Left alignment can be requested by setting sep = "\\l"
, right alignment by "\\r"
and center alignment by "\\c"
. Mind the backslashes, as if they are omitted, strings would be aligned to the character l, r or c respectively. The default is NULL
which would just leave the strings as they are.
This argument is send directly to the function StrAlign()
as argument sep
.
integer, the defined fixed width of the strings.
optional value setting the language for the months and daynames. Can be either "local"
for current locale or "engl"
for english. If left to NULL
, the DescToolsOption "lang"
will be searched for and if not found "local"
will be taken as default.
a numerical tolerance used mainly for formatting p values, those less than eps are formatted as "< [eps]
" (where '[eps]' stands for format(eps, digits))
.
further arguments to be passed to or from methods.
the formatted values as characters.
If x
was a matrix
, then a the result will also be a matrix
. (Hope this will not surprise you...)
Format()
is used to format numbers. Fmt()
can be used to display existing or define new format templates. as.fmt()
will convert a list to a format object (class fmt
).
The argument fmt
can be used be used to generate different formats. When x
is a date,
it can be formatted with any combination of the format codes d
, m
and y
for day, month or year.
Repeting the specific code defines the degree of abbreviation:
Date codes | |
d |
day of the month without leading zero (1 - 31) |
dd |
day of the month with leading zero (01 - 31) |
ddd |
abbreviated name for the day of the week (e.g. Mon) in the current user's language |
dddd |
full name for the day of the week (e.g. Monday) in the current user's language |
m |
month without leading zero (1 - 12) |
mm |
month with leading zero (01 - 12) |
mmm |
abbreviated month name (e.g. Jan) in the current user's language |
mmmm |
full month name (e.g. January) in the current user's language |
y |
year without century, without leading zero (0 - 99) |
yy |
year without century, with leading zero (00 - 99) |
yyyy |
year with century. For example: 2005 |
Date codes |
For numeric values there are the following special codes:
Code | ||
e |
scientific | forces scientific representation of x, e.g. 3.141e-05. The number of digits, alignment |
and zero values are respected. | ||
|
||
engineering | forces scientific representation of x, but only with powers that are a multiple of 3. |
engabb |
engineering abbr. | same as eng , but replaces the exponential representation by codes. See d.prefix . |
|
percent | will divide the given number by 100 and append the %-sign (without a separator). | |
p |
p-value | |
will wrap the function format.pval and return a p-value format. |
||
frac |
fractions | will try to convert numbers to fractions. See fractions() . |
|
||
significance | will produce a significance representation of a p-value consisting of * and ., | |
while the breaks are set according to the used defaults e.g. in lm as |
||
[0, 0.001] = *** |
||
(0.001, 0.01] = ** |
||
(0.01, 0.05] = * |
||
(0.05, 0.1] = . |
||
(0.1,1] = |
Code |
fmt
can as well be an object of class fmt
consisting of a list out of the arguments above.
This allows to store the full format in a variable or as an option and use it as format template subsequently. If we would want to define a format for counts, we could create a list with a big mark and with 0 digits and subsequently call the Format
function with this format:
fmt.count <- as.fmt(digits=0, big.mark="'")
Format(4231.2, fmt=fmt.count)
where the latter returns the same result as if the arguments would have been supplied directly:
Format(4231.2, digits=0, big.mark="'")
.
Finally fmt
can also be a function in x, which makes formatting very flexible.
The formats can as well be organized as options. DescToolsOptions("fmt")
would display the currently defined formats. The function Fmt()
can be used to get, modify and store new formats in dependency of the arguments given. See examples.
format
, formatC
, prettyNum
, sprintf
, symnum
,
StrAlign
, StrPad
, Sys.setlocale
,
Weekday
, Month
,
DescToolsOptions
# NOT RUN {
Format(as.Date(c("2014-11-28", "2014-1-2")), fmt="ddd, d mmmm yyyy")
Format(as.Date(c("2014-11-28", "2014-1-2")), fmt="ddd, d mmmm yyyy", lang="engl")
x <- pi * 10^(-10:10)
Format(x, digits=3, fmt="%", sci=NA)
Format(x, digits=4, sci=c(4, 6), leading = "drop", width=9, align=".")
# format a matrix
m <- matrix(runif(100), nrow=10,
dimnames=list(LETTERS[1:10], LETTERS[1:10]))
Format(m, digits=1)
# engineering format
Format(x, fmt="eng", digits=2)
Format(x, fmt="engabb", leading="00", digits=2)
# combine with grams [g]
paste(Format(x, fmt="engabb", leading="00", digits=2), "g", sep="")
# example form symnum
pval <- rev(sort(c(outer(1:6, 10^-(1:3)))))
noquote(cbind(Format(pval, fmt="p"), Format(pval, fmt="*")))
# use Fmt() to get and define new formats stored as option
Fmt() # all defined formats
Fmt("abs") # only format named "abs"
Fmt("nexist") # only format named "nexist" (nonexisting)
Fmt("abs", "per", "nexist")
Fmt("abs", digits=3) # get Fmt("abs") and overwrite digits
Fmt("abs", na.form="-") # get Fmt("abs") and add user defined na.form
# define totally new format and store as option
Fmt(nob=as.fmt(digits=10, na.form="nodat"))
# overwrite an existing format
Fmt(nob=Fmt("nob", digits=5))
Fmt("nob")
# change the character to be used as the decimal point
opt <- options(OutDec=",")
Format(1200, digits=2, big.mark = ".")
options(opt)
# }
Run the code above in your browser using DataLab