With numeric values in vector, we can perform mixed-fraction-based formatting. There are several options for setting the accuracy of the fractions. Furthermore, there is an option for choosing a layout (i.e., typesetting style) for the mixed-fraction output.
The following options are available for controlling this type of formatting:
accuracy: how to express the fractional part of the mixed fractions; there are three keyword options for this and an allowance for arbitrary denominator settings
simplification: an option to simplify fractions whenever possible
layout: We can choose to output values with diagonal or inline fractions
digit grouping separators: options to enable/disable digit separators and provide a choice of separator symbol for the whole number portion
pattern: option to use a text pattern for decoration of the formatted mixed fractions
locale-based formatting: providing a locale ID will result in number formatting specific to the chosen locale
Arguments
- x
A numeric vector.
- accuracy
The type of fractions to generate. This can either be one of the keywords
"low"
,"med"
, or"high"
(to generate fractions with denominators of up to 1, 2, or 3 digits, respectively) or an integer value greater than zero to obtain fractions with a fixed denominator (2
yields halves,3
is for thirds,4
is quarters, etc.). For the latter option, usingsimplify = TRUE
will simplify fractions where possible (e.g.,2/4
will be simplified as1/2
). By default, the"low"
option is used.- simplify
If choosing to provide a numeric value for
accuracy
, the option to simplify the fraction (where possible) can be taken withTRUE
(the default). WithFALSE
, denominators in fractions will be fixed to the value provided inaccuracy
.- layout
For HTML output, the
"inline"
layout is the default. This layout places the numerals of the fraction on the baseline and uses a standard slash character. The"diagonal"
layout will generate fractions that are typeset with raised/lowered numerals and a virgule.- use_seps
An option to use digit group separators. The type of digit group separator is set by
sep_mark
and overridden if a locale ID is provided tolocale
. This setting isTRUE
by default.- pattern
A formatting pattern that allows for decoration of the formatted value. The value itself is represented by
{x}
and all other characters are taken to be string literals.- sep_mark
The mark to use as a separator between groups of digits (e.g., using
sep_mark = ","
with1000
would result in a formatted value of1,000
).- locale
An optional locale ID that can be used for formatting the value according the locale's rules. Examples include
"en"
for English (United States) and"fr"
for French (France). The use of a valid locale ID will override any values provided insep_mark
anddec_mark
. We can use theinfo_locales()
function as a useful reference for all of the locales that are supported.- output
The output style of the resulting character vector. This can either be
"auto"
(the default),"plain"
,"html"
,"latex"
,"rtf"
, or"word"
. In knitr rendering (i.e., Quarto or R Markdown), the"auto"
option will choose the correctoutput
value
Examples
Let's create a numeric vector for the next few examples:
num_vals <- c(0.0052, 0.08, 0, -0.535, NA)
Using vec_fmt_fraction()
will create a character vector of fractions. Any
NA
values will render as "NA"
. The rendering context will be autodetected
unless specified in the output
argument (here, it is of the "plain"
output type).
vec_fmt_fraction(num_vals)
There are many options for formatting as fractions. If you'd like a higher
degree of accuracy in the computation of fractions we can supply the "med"
or "high"
keywords to the accuracy
argument:
vec_fmt_fraction(num_vals, accuracy = "high")
As a last example, one can wrap the values in a pattern with the pattern
argument. Note here that NA
values won't have the pattern applied.
vec_fmt_fraction(num_vals, accuracy = 8, pattern = "[{x}]")
See also
Other vector formatting functions:
vec_fmt_bytes()
,
vec_fmt_currency()
,
vec_fmt_datetime()
,
vec_fmt_date()
,
vec_fmt_duration()
,
vec_fmt_engineering()
,
vec_fmt_integer()
,
vec_fmt_markdown()
,
vec_fmt_number()
,
vec_fmt_partsper()
,
vec_fmt_percent()
,
vec_fmt_roman()
,
vec_fmt_scientific()
,
vec_fmt_time()