<- c(10^(-3:-5), NA) num_vals
The vec_fmt_partsper()
function
Let’s create a numeric vector for the next few examples:
Using vec_fmt_partsper()
with the default options will create a character vector where the resultant per mille values have two decimal places and 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_partsper(num_vals)
[1] "1.00‰" "0.10‰" "0.01‰" "NA"
#> [1] "1.00%." "0.10%." "0.01%." "NA"
We can change the output units to a different measure. If ppm units are desired then to_units = "ppm"
can be used.
vec_fmt_partsper(num_vals, to_units = "ppm")
[1] "1,000.00 ppm" "100.00 ppm" "10.00 ppm" "NA"
#> [1] "1,000.00 ppm" "100.00 ppm" "10.00 ppm" "NA"
We can change the decimal mark to a comma, and we have to be sure to change the digit separator mark from the default comma to something else (a period works here):
vec_fmt_partsper(
num_vals,to_units = "ppm",
sep_mark = ".",
dec_mark = ","
)
[1] "1.000,00 ppm" "100,00 ppm" "10,00 ppm" "NA"
#> [1] "1.000,00 ppm" "100,00 ppm" "10,00 ppm" "NA"
If we are formatting for a different locale, we could supply the locale ID and let gt handle these locale-specific formatting options:
vec_fmt_partsper(num_vals, to_units = "ppm", locale = "es")
[1] "1.000,00 ppm" "100,00 ppm" "10,00 ppm" "NA"
#> [1] "1.000,00 ppm" "100,00 ppm" "10,00 ppm" "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_partsper(num_vals, to_units = "ppm", pattern = "{x}V")
[1] "1,000.00 ppmV" "100.00 ppmV" "10.00 ppmV" "NA"
#> [1] "1,000.00 ppmV" "100.00 ppmV" "10.00 ppmV" "NA"