Reputation: 141
I am using this code to read in my files by mtime. However, read reads in the files so quickly that mtime doesnt work.
###### checks all files in directory ########
readinfiles<-function(){
details<- file.info(list.files("filename", all.files=F, full.name=T));
details<- details[with(details, order(as.POSIXct(mtime))),]
file<- rownames(details)
}
all_files<- readinfiles();
list_all_files<- as.list(all_files);
list_all_files;
Is there a way to sort the files by characters the spell numbers? this is what I want.
list_all_files;
THREE20142305//tablesCORRECTED///onea
THREE20142305//tablesCORRECTED///twoa
THREE20142305//tablesCORRECTED///threea
THREE20142305//tablesCORRECTED///foura
What I get:
list_all_files;
THREE20142305//tablesCORRECTED///foura
THREE20142305//tablesCORRECTED///onea
THREE20142305//tablesCORRECTED///threea
THREE20142305//tablesCORRECTED///twoa
Upvotes: 1
Views: 165
Reputation: 92292
It is possible to create some helper function using the english
package in order to solve this
The helper function
FileSort <- function(x){
require(english, quietly = TRUE) # Loading the `english` package
Nums <- as.character(english(seq_len(length(x)))) # Creating a vector of integers written in words (with the same length of the file list)
Nums <- gsub("\\s", "", Nums) # Remove spaces so, for example, "twenty two" will become "twentytwo"
temp <- gsub(".*//", "", x) # Retrieving the number out of the file name
temp <- substr(temp, 1, nchar(temp) - 1) # Removing the `a` at the end
x <- Map(cbind, x, match(temp, Nums)) # Adding the Numbers column to the file list
x <- do.call(rbind, x) # Collapsing
x <- as.list(x[order(as.numeric(x[, 2]))]) # Sorting
x
}
Your data
ist_all_files <- list("THREE20142305//tablesCORRECTED///foura",
"THREE20142305//tablesCORRECTED///onea",
"THREE20142305//tablesCORRECTED///threea",
"THREE20142305//tablesCORRECTED///twoa")
Implementation
list_all_files <- FileSort(list_all_files)
list_all_files
# [[1]]
# [1] "THREE20142305//tablesCORRECTED///onea"
#
# [[2]]
# [1] "THREE20142305//tablesCORRECTED///twoa"
#
# [[3]]
# [1] "THREE20142305//tablesCORRECTED///threea"
#
# [[4]]
# [1] "THREE20142305//tablesCORRECTED///foura"
Upvotes: 1
Reputation: 2470
I can think of a few ways to start working around this but, honestly, they all seem pretty inconvenient. Is it feasible to rename the files on disk? That's what I would do if it were me. Name them something like
THREE20142305//tablesCORRECTED///01_onea
THREE20142305//tablesCORRECTED///02_twoa
THREE20142305//tablesCORRECTED///03_threea
THREE20142305//tablesCORRECTED///04_foura
and then sort by name, instead of mtime
.
You can rename files through a GUI by hand in less time than it will take you to write an R solution, even though there are 100 of them. If these files are periodically overwritten, it would probably be faster to write a script to rename them one by one (use copy/paste generously) than to write an R workaround. The script would have 100 nearly identical lines and be tedious to write, but still easier than making R understand English number words.
Sorry for the bad news.
============= An example proprocessing script =================
file.rename("onea","01")
file.rename("twoa","02")
file.rename("threea","03")
file.rename("foura","04")
file.rename("fivea","05")
file.rename("sixa","06")
file.rename("sevena","07")
file.rename("eighta","08")
file.rename("ninea","09")
file.rename("tena","10")
file.rename("elevena","11")
file.rename("twelvea","12")
file.rename("thirteena","13")
file.rename("fourteena","14")
file.rename("fifteena","15")
file.rename("sixteena","16")
file.rename("seventeena","17")
file.rename("eighteena","18")
file.rename("nineteena","19")
file.rename("twentya","20")
file.rename("twentyonea","21")
file.rename("twentytwoa","22")
file.rename("twentythreea","23")
file.rename("twentyfoura","24")
file.rename("twentyfivea","25")
file.rename("twentysixa","26")
file.rename("twentysevena","27")
file.rename("twentyeighta","28")
file.rename("twentynine","29")
file.rename("twentya","20")
Copy paste and tweak for another 10 minutes or so (depending on your text editor search/replace skill), put this at the top of your program and you have a feasible solution if you don't get a better one from someone else.
Upvotes: 0