0

I have a folder containing asc files named as bio1_t2.asc, bio1_t3.asc up to bio1_t539.asc. I'm using a package that loads these files from the folder for some calculation. The function inside the package loads files directly from the folder.

My problem is that the package loads files in alphabetical order like bio1_t10.asc bio1_t101.asc...bio1_t2.asc. But i want it to load in numeric order like bio1_t2, bio1_t3,...bio1_t539 for a true calculation.

Is there a way to load the files in numeric order from the folder? Or how can i rename the files so that they are loaded in numerical order?

2 Answers2

1

Assuming the question is about reordering your list of file names, not something specific to the way they are loaded into the specific package you are using.

One option would be to use stringr::str_extract to capture all of the numbers after t and order based on those values.

As Megan mentions, a full list of file_names would be created using list.files.

library(stringr)

# example file names in an incorrect order
file_names <- c('bio1_t100.asc', 'bio1_t2.asc', 'bio1_t202.asc', 'bio1_t301.asc', 'bio1_t3.asc') 
file_names[order(as.numeric(str_extract(file_names, '(?<=t)[0-9]*')))]
#--------

[1] "bio1_t2.asc"   "bio1_t3.asc"   "bio1_t100.asc" "bio1_t202.asc" "bio1_t301.asc"

EDIT: based on the comments it sounds like renaming the files is the easiest path forward. Using str_extract call from above we can rename all of the files.

new_names <- paste0((1000 + as.numeric(str_extract(file_names, '(?<=t)[0-9]*'))),'.asc')
# [1] "1100.asc" "1002.asc" "1202.asc" "1301.asc" "1003.asc"  

file.rename(file_names, new_names)
nniloc
  • 4,128
  • 2
  • 11
  • 22
  • Actually the function of the package loads the files from the folder.The function is as follows: deviationThroughTime(variableDirectory, timeSlicePeriod, fileExtension = "asc"). Therefore, even if i load and order the files the way i wanted in R, the function loads it only from the folder and loads in alpabethical order. I tried to rename files but there are over 500 files and i dont know how to give names in alphabetical order. I hope i could tell where i am struggling. – user14316891 Sep 21 '20 at 20:54
  • 1
    I've been reading about the function `deviationTroughTime()` from the package `climateStability`. From the package's vignette: "You may want to also that R loads the files in the correct order–that is, that your files are named in such a way that they are in order when read in.". Thus it seems there is no direct fix for your problem. You can work around it by renaming the files using R so the function `deviationTroughTime()` reads them in the correct order. In such regard you may find this post interesting: https://stackoverflow.com/questions/10758965/how-do-i-rename-files-using-r – davidnortes Sep 21 '20 at 21:15
  • Following the comments here I added one possible solution of renaming each file using the number following `t` in the original file names. – nniloc Sep 21 '20 at 21:38
  • Tricky. How about if you add 1000 to all of the new filenames? `new_names <- paste0((1000 + as.numeric(str_extract(file_names, '(?<=t)[0-9]*'))),'.asc')` – nniloc Sep 21 '20 at 21:54
0

You can first create a list of all the files you want to load in and then sort the way you want them. For example:

myfiles1 = list.files(pattern= "substringhere", full.names = F)
sort(myfiles1) # sort how you want

# set up empty data table
dt = data.table()
# loop through them and load 
for (f in myfiles[1:length(myfiles)]) {
  # just to see which file number you are on
  print(f)
  temp = fread(f)
# add whatever you wanted to do with the files here
}
Megan
  • 31
  • 3