The vec_fmt_index() function

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

num_vals <- c(1, 4, 5, 8, 12, 20, 26, 34, 0, -5, 1.3, NA)

Using vec_fmt_index() with the default options will create a character vector with values rendered as index numerals. Zero values will be rendered as "" (i.e., empty strings), any NA values remain as NA values, and negative values will be automatically made positive. The rendering context will be autodetected unless specified in the output argument (here, it is of the "plain" output type).

vec_fmt_index(num_vals)
 [1] "A"  "D"  "E"  "H"  "L"  "T"  "Z"  "HH" ""   "E"  "A"  "NA"
#> [1] "A" "D" "E" "H" "L" "T" "Z" "HH" "" "E" "A" "NA"

We can also use vec_fmt_index() with the case = "lower" option to create a character vector with values rendered as lowercase Roman numerals.

vec_fmt_index(num_vals, case = "lower")
 [1] "a"  "d"  "e"  "h"  "l"  "t"  "z"  "hh" ""   "e"  "a"  "NA"
#> [1] "a" "d" "e" "h" "l" "t" "z" "hh" "" "e" "a" "NA"

If we are formatting for a different locale, we could supply the locale ID and let gt obtain a locale-specific set of index values:

vec_fmt_index(1:10, locale = "so")
 [1] "BCDFGHJKLMNQRSTWXY"                                                                                                                                                                  
 [2] "BCDFGHJKLMNQRSTWXYBCDFGHJKLMNQRSTWXY"                                                                                                                                                
 [3] "BCDFGHJKLMNQRSTWXYBCDFGHJKLMNQRSTWXYBCDFGHJKLMNQRSTWXY"                                                                                                                              
 [4] "BCDFGHJKLMNQRSTWXYBCDFGHJKLMNQRSTWXYBCDFGHJKLMNQRSTWXYBCDFGHJKLMNQRSTWXY"                                                                                                            
 [5] "BCDFGHJKLMNQRSTWXYBCDFGHJKLMNQRSTWXYBCDFGHJKLMNQRSTWXYBCDFGHJKLMNQRSTWXYBCDFGHJKLMNQRSTWXY"                                                                                          
 [6] "BCDFGHJKLMNQRSTWXYBCDFGHJKLMNQRSTWXYBCDFGHJKLMNQRSTWXYBCDFGHJKLMNQRSTWXYBCDFGHJKLMNQRSTWXYBCDFGHJKLMNQRSTWXY"                                                                        
 [7] "BCDFGHJKLMNQRSTWXYBCDFGHJKLMNQRSTWXYBCDFGHJKLMNQRSTWXYBCDFGHJKLMNQRSTWXYBCDFGHJKLMNQRSTWXYBCDFGHJKLMNQRSTWXYBCDFGHJKLMNQRSTWXY"                                                      
 [8] "BCDFGHJKLMNQRSTWXYBCDFGHJKLMNQRSTWXYBCDFGHJKLMNQRSTWXYBCDFGHJKLMNQRSTWXYBCDFGHJKLMNQRSTWXYBCDFGHJKLMNQRSTWXYBCDFGHJKLMNQRSTWXYBCDFGHJKLMNQRSTWXY"                                    
 [9] "BCDFGHJKLMNQRSTWXYBCDFGHJKLMNQRSTWXYBCDFGHJKLMNQRSTWXYBCDFGHJKLMNQRSTWXYBCDFGHJKLMNQRSTWXYBCDFGHJKLMNQRSTWXYBCDFGHJKLMNQRSTWXYBCDFGHJKLMNQRSTWXYBCDFGHJKLMNQRSTWXY"                  
[10] "BCDFGHJKLMNQRSTWXYBCDFGHJKLMNQRSTWXYBCDFGHJKLMNQRSTWXYBCDFGHJKLMNQRSTWXYBCDFGHJKLMNQRSTWXYBCDFGHJKLMNQRSTWXYBCDFGHJKLMNQRSTWXYBCDFGHJKLMNQRSTWXYBCDFGHJKLMNQRSTWXYBCDFGHJKLMNQRSTWXY"
#> [1] "B" "C" "D" "F" "G" "H" "J" "K" "L" "M"

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_index(num_vals, case = "lower", pattern = "{x}.")
 [1] "a."  "d."  "e."  "h."  "l."  "t."  "z."  "hh." "."   "e."  "a."  "NA" 
#> [1] "a." "d." "e." "h." "l." "t." "z." "hh." "." "e." "a." "NA"