knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
set.seed(1)
library(magrittr)
poorly_ordered <- expand.grid(98:103, 9:11) %>%
  apply(1, function(x) paste0("patient", x[1], "-day", x[2], ".png")) %>%
  sample(size = length(.))

Numbers don't comply with alphabetical order

poorly_ordered is a vector of file names of images of patient samples from different days.

poorly_ordered

How do we get this vector into order? Well, alphabetical order doesn't work:

sort(poorly_ordered)

Patient 100 comes before patient 99. This is because 1 comes before 9 in alphabetical order.

Alphordering numbers

It's possible to alphord the numbers by prefixing them with zeroes:

strex::str_alphord_nums(poorly_ordered)

Having done this, the alphabetical order is the one we want:

sort(strex::str_alphord_nums(poorly_ordered))


rorynolan/strex documentation built on Oct. 12, 2024, 12:32 p.m.