If strings are numbered, their numbers may not comply with alphabetical
order, e.g. "abc2" comes after "abc10"
in alphabetical order. We might (for
whatever reason) wish to change them such that they come in the order that
we would like. This function alters the strings such that they comply with
alphabetical order, so here "abc2"
would be renamed to "abc02". It works on
file names with more than one number in them e.g. "abc01def3"
(a string
with 2 numbers). All the strings in the character vector string
must have
the same number of numbers, and the non-number bits must be the same.
str_alphord_nums(string)
A character vector.
string <- paste0("abc", 1:12)
print(string)
#> [1] "abc1" "abc2" "abc3" "abc4" "abc5" "abc6" "abc7" "abc8" "abc9"
#> [10] "abc10" "abc11" "abc12"
str_alphord_nums(string)
#> [1] "abc01" "abc02" "abc03" "abc04" "abc05" "abc06" "abc07" "abc08" "abc09"
#> [10] "abc10" "abc11" "abc12"
str_alphord_nums(c("abc9def55", "abc10def7"))
#> [1] "abc09def55" "abc10def07"
str_alphord_nums(c("01abc9def55", "5abc10def777", "99abc4def4"))
#> [1] "01abc09def055" "05abc10def777" "99abc04def004"
str_alphord_nums(1:10)
#> [1] "01" "02" "03" "04" "05" "06" "07" "08" "09" "10"
if (FALSE) { # \dontrun{
str_alphord_nums(c("abc9def55", "abc10xyz7")) # error
} # }