Format()
is the workhorse here and formats numbers and dates.
The argument fmt
is very flexible and is used to generate a variety of different formats. When x
is a date, it can take ISO-8601-date-and-time-format codes consisting of (d
, m
and y
for day, month or year) and defining the combination of day month and year representation. Repeating the specific code defines the degree of abbreviation. The format 'yyyy-mm-dd'
would yield a date as 2020-10-12
.
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 |
| |
The function as.CDateFmt()
converts ISO-8601 codes into the C-format codes used in base R.
So
as.CDateFmt("yyyy mm dd")
yields "%Y %m %d"
.
Even more variability is needed to display numeric values. For the most frequently used formats there are the following special codes available:
Code | | |
e | scientific | forces scientific representation of x, e.g. 3.141e-05. The number of digits, |
| | alignment and zero values are further respected. |
| | eng |
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, | |
| e.g. M for mega (1e6). 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. | | |
Use eps to define the threshold to switch to a < 000 representation. | | |
frac | fractions | will (try to) convert numbers to fractions. So 0.1 will be displayed as 1/10. |
| | 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] = | p* |
fmt
can as well be an object of class fmt
consisting of a list out of the arguments above.
This allows to store and manage the full format in variables or as options (in DescToolsOptions()
) and use it as format template subsequently.
Finally fmt
can also be a function in x, which makes formatting very flexible.
New formats can be created by means of as.fmt()
. This works quite straight on. We can use any of the arguments from Format()
and combine them to a list.
The following code will define a new format template named "myNumFmt
" of the class "fmt"
. Provided to Format()
this will result in a number displayed with 2 fixed digits and a comma as big mark:
myNumFmt <- as.fmt(digits=2, big.mark=",")
Format(12222.89345, fmt=myNumFmt) = 12,222.89
The latter returns the same result as if the arguments would have been supplied directly:
Format(12222.89345, digits=2, big.mark=",")
.
Many report functions (e.g. TOne()
) in DescTools use three default formats for counts (named "abs"
), numeric values ("num"
) and percentages ("per"
). These formats can be set by the user as options (see DescToolsOptions()
. For other purposes any number of any named formats can be defined.
Fmt()
is used to access and edit already defined Formats. It can directly adapt defined properties and returns the format template. Fmt("num", digits=1, sci=10)
will use the current version of the numeric format and change the digits to 1 and the threshold to switch to scientifc presentation to numbers >1e10 and <1e-10.
Format templates can be altered using their names. With Fmt(abs=Fmt("abs", big.mark=" "))
the format template for count values "abs"
will be overwritten with the new values and stored as option for the current session.
The formats can as well be organized as options. DescToolsOptions("fmt")
would display the currently defined formats. This mechanic works analogously to the options()
procedure of base R. So to store the current settings we can use
opt <- DescToolsOptions("fmt")
... do some stuff like redefining the global formats ...
DescToolOptions(opt)
The last command resets the options and so we have again the initial definitions for the format templates.