1

Is there a way to list files in R as it is listed in Windows when sorted by "Name"?

For instance, say I have a Directory (henceforth "testDir") that has the following files:

Slide1_1.txt
Slide1_2.txt
Slide1_3.txt
Slide1_11.txt
Slide1_111.txt
Slide2_1.txt
Slide3_1.txt

If I do the following in R:

testFiles = list.files(path = testDir, full.names = F)

testFiles will have the following output:

> testFiles
"Slide1_1.txt"   "Slide1_11.txt"  "Slide1_111.txt" "Slide1_2.txt"   "Slide1_3.txt"  "Slide2_1.txt"   "Slide3_1.txt"  

However, I would like it to be orderer as such:

"Slide1_1.txt"   "Slide1_2.txt"   "Slide1_3.txt"  "Slide1_11.txt"  "Slide1_111.txt" "Slide2_1.txt"   "Slide3_1.txt"  

Is there another way of reading in these files? It should be noted that the timestamp for these files are not in any specific order.

EDIT: Added additional conditions to filenames

Roland
  • 127,288
  • 10
  • 191
  • 288
JCB
  • 35
  • 5

1 Answers1

1

Try

library(gtools)
mixedsort(testFiles)
#[1] "Slide1_1.txt"   "Slide1_2.txt"   "Slide1_3.txt"   "Slide1_11.txt" 
#[5] "Slide1_111.txt" "Slide2_1.txt"   "Slide3_1.txt"  
akrun
  • 874,273
  • 37
  • 540
  • 662