The vec_fmt_integer() function

Let’s create a numeric vector for the next few examples:

num_vals <- c(5.2, 8.65, 13602, -5.3, NA)

Using vec_fmt_integer() with the default options will create a character vector where the input values undergo rounding to become integers and NA values will render as "NA". Also, the rendering context will be autodetected unless specified in the output argument (here, it is of the "plain" output type).

vec_fmt_integer(num_vals)
[1] "5"      "9"      "13,602" "−5"     "NA"    
#> [1] "5" "9" "13,602" "-5" "NA"

We can change the digit separator mark to a period with the sep_mark option:

vec_fmt_integer(num_vals, sep_mark = ".")
[1] "5"      "9"      "13.602" "−5"     "NA"    
#> [1] "5" "9" "13.602" "-5" "NA"

Many options abound for formatting values. If you have a need for positive and negative signs in front of each and every value, use force_sign = TRUE:

vec_fmt_integer(num_vals, force_sign = TRUE)
[1] "+5"      "+9"      "+13,602" "−5"      "NA"     
#> [1] "+5" "+9" "+13,602" "-5" "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_integer(num_vals, pattern = "`{x}`")
[1] "`5`"      "`9`"      "`13,602`" "`−5`"     "NA"      
#> [1] "`5`" "`9`" "`13,602`" "`-5`" "NA"