<- c(0.0052, 0.08, 0, -0.535, NA) num_vals
The vec_fmt_fraction()
function
Let’s create a numeric vector for the next few examples:
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)
[1] "0" "1/9" "0" "−5/9" "NA"
#> [1] "0" "1/9" "0" "-5/9" "NA"
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")
[1] "1/200" "2/25" "0" "−107/200" "NA"
#> [1] "1/200" "2/25" "0" "-107/200" "NA"
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}]")
[1] "[0]" "[1/8]" "[0]" "[−1/2]" "NA"
#> [1] "[0]" "[1/8]" "[0]" "[-1/2]" "NA"