Skip to contents

Convert Numbers into Padded Strings for Easier Group Numbering

Usage

group_numbers(numbers)

Arguments

numbers

A numeric vector with a length of at least 1.

Value

A Character Vector

Examples

# Useful for easier group numbering so groups are ordered as intended
# Expects a numeric vector of numbers to convert to padded numbers
regular_numbers <- 1:19
padded_numbers <- group_numbers(regular_numbers)

# The padding matters when creating labels for groupings
# as numbers will be converted to characters if attached to strings.
# Sorts as expected:
sort(regular_numbers)
#>  [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19

# Does not as a character:
sort(paste0("group_", regular_numbers))
#>  [1] "group_1"  "group_10" "group_11" "group_12" "group_13" "group_14"
#>  [7] "group_15" "group_16" "group_17" "group_18" "group_19" "group_2" 
#> [13] "group_3"  "group_4"  "group_5"  "group_6"  "group_7"  "group_8" 
#> [19] "group_9" 

# Will sort as expected when padded:
sort(paste0("group_", padded_numbers))
#>  [1] "group_01" "group_02" "group_03" "group_04" "group_05" "group_06"
#>  [7] "group_07" "group_08" "group_09" "group_10" "group_11" "group_12"
#> [13] "group_13" "group_14" "group_15" "group_16" "group_17" "group_18"
#> [19] "group_19"