1

I have a list of names which I would like to sort by the last number in ascending order.

[1] "W2345_S-001-R1_1.csv"     "W2346_S-001-R1_10.csv"    

[3] "W2347_S-001-R1_2.csv"     "W2348_S-001-R1_9.csv"    

[5] "W2345_S-001-R2_1.csv"     "W2346_S-001-R2_10.csv" 

[7] "W2347_S-001-R2_2.csv"     "W2348_S-001-R2_9.csv"  

I would like to arrange them by R1 then R2. Within R1 or R2, it should be arranged as 1, 2, 9, 10. Hence the output should be

1] "W2345_S-001-R1_1.csv"     "W2346_S-001-R1_2.csv"    

[3] "W2347_S-001-R1_9.csv"     "W2348_S-001-R1_10.csv"    

[5] "W2345_S-001-R2_1.csv"     "W2346_S-001-R2_2.csv" 

[7] "W2347_S-001-R2_9.csv"     "W2348_S-001-R2_10.csv"   
Sotos
  • 51,121
  • 6
  • 32
  • 66
Loki123
  • 209
  • 6
  • Please share data in a [reproducible format](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) like `dput()` so we can copy/paste the values into R for testing. – MrFlick Jan 11 '23 at 14:49
  • Hi, I'm not sure how to do that. The excel files are stored in a folder, and I converted their names to values using file.list <- list.files(pattern='*.csv'). – Loki123 Jan 11 '23 at 14:54
  • 1
    Take your first 8 values: `dput(head(..., 8))` – Maël Jan 11 '23 at 15:01

2 Answers2

1

In your case, you should shorten your string to the numeric part that will be sorted: the first set of numbers is not the one you want ordering from. In that case, use gsub and str_order (gtools::mixedorder will work equally) to get the ordering correctly:

library(stringr)
v[str_order(gsub("W.*1-","", v), numeric = TRUE)]

#[1] "W2345_S-001-R1_1.csv"  "W2347_S-001-R1_2.csv"  "W2348_S-001-R1_9.csv"  "W2346_S-001-R1_10.csv"
#[5] "W2345_S-001-R2_1.csv"  "W2347_S-001-R2_2.csv"  "W2348_S-001-R2_9.csv"  "W2346_S-001-R2_10.csv"

Regex explanation: basically, "W.*1-" matches any substrings that start with W, finish with 1-, and have one or more characters in between .*. Many different regexes would work here, so this is only one possibility.

Maël
  • 45,206
  • 3
  • 29
  • 67
  • Thanks very much, this works well. I know this is a little basic, but could you explain what do the different annotations "W,*1-","",v mean? I don't know where to search for such information, but I want to learn how to use it. – Loki123 Jan 11 '23 at 15:14
1

Base R solution for fun,

d1 <- data.frame(v1 = as.numeric(gsub('^.*-R([0-9]+)_([0-9]+).csv', '\\1', x)), 
                 v2 = as.numeric(gsub('^.*-R([0-9]+)_([0-9]+).csv', '\\2', x)))
x[order(d1$v1, d1$v2)]

[1] "W2345_S-001-R1_1.csv"  "W2347_S-001-R1_2.csv"  "W2348_S-001-R1_9.csv"  "W2346_S-001-R1_10.csv" "W2345_S-001-R2_1.csv"  "W2347_S-001-R2_2.csv" 
[7] "W2348_S-001-R2_9.csv"  "W2346_S-001-R2_10.csv"

DATA

 dput(x)
c("W2345_S-001-R1_1.csv", "W2346_S-001-R1_10.csv", "W2347_S-001-R1_2.csv", 
"W2348_S-001-R1_9.csv", "W2345_S-001-R2_1.csv", "W2346_S-001-R2_10.csv", 
"W2347_S-001-R2_2.csv", "W2348_S-001-R2_9.csv")
Sotos
  • 51,121
  • 6
  • 32
  • 66