How can I read the files in a directory in sorted order using R?

后端 未结 4 600
忘了有多久
忘了有多久 2020-12-17 05:26

The code given below worked well and read the files in my directory and extracted the values:

X <- c(75:85) ; Y <- c(208:215) 
extract <- vector()
f         


        
4条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-17 06:11

    Why not retrieve all files (with a particular pattern) with list.files() and then sort that. Then you retrieve the files from the sorted vector, which gives them to you in correctly sorted order. This has the advantage that it also works when numbers are missing from the 1:365 sequence

    Something like:

    myFiles <- list.files(pattern = "^Climate_Rad_") #all files starting with Climate_
    myFiles <- sort(myFiles)
    # then read them in, for instance through
    for (fileNames in myFiles) {READ.IN.AND.DO.YOUR.MAGIC.ON.THEM}
    

提交回复
热议问题