If there are less than 10 samples, they are correctly ordered S1_X..., S2_X..., S3_X......
But if there are e.g. 21 samples, they are ordered as S1_X..., S10_X..., S11_X..., ...,S19_X..., S2_X..., S20_X..., S21_X..., S3_X..., S4_X..., ..., S9_X..., so the original order is lost.
Probably the easiest repair is to add enough leading zeros to all numbers have the same length.
E.g. to replace
If there are less than 10 samples, they are correctly ordered
S1_X...
,S2_X...
,S3_X...
... But if there are e.g. 21 samples, they are ordered asS1_X...
,S10_X...
,S11_X...
, ...,S19_X...
,S2_X...
,S20_X...
,S21_X...
,S3_X...
,S4_X...
, ...,S9_X...
, so the original order is lost.Probably the easiest repair is to add enough leading zeros to all numbers have the same length. E.g. to replace
with
I.e. replace
as.character(i)
withsprintf(paste0("%0",nchar(as.character(length(samples))),"d"),i)
.