1

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    
user3919708
  • 141
  • 1
  • 1
  • 9

2 Answers2

1

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"
David Arenburg
  • 91,361
  • 17
  • 137
  • 196
  • @ David, how did you find the english package? This is awesome! – user3919708 Aug 11 '14 at 23:18
  • @ this is naive David, but what is the syntax to install this english package? install.packages("english"); ? – user3919708 Aug 12 '14 at 13:55
  • Yes. What are you using as your GUI? In most of them (like RStudio) you can do it by just pressing a button – David Arenburg Aug 12 '14 at 13:57
  • R 2.10.1 GUI 1.31 Leopard build 32-bit (5537) , is what I am doing. – user3919708 Aug 12 '14 at 13:59
  • I called this and it claims english package doesnt exist; however, I see it does exist when I was looking at the package details – user3919708 Aug 12 '14 at 13:59
  • Maybe your R version is very old. It does [exsist](http://cran.r-project.org/web/packages/english/english.pdf) – David Arenburg Aug 12 '14 at 14:00
  • My R version is very old, but so is my mac. Newer version of R are not supported. – user3919708 Aug 12 '14 at 14:02
  • @ david, I have updated to the highest version I can get with this OX system R 2.15.3. This is what I get now. Seeing I am not a daily programmer, what does this mean: package ‘english’ is available as a source package but not as a binary – user3919708 Aug 12 '14 at 14:23
  • Try `install.packages('english', type='source')` – David Arenburg Aug 12 '14 at 17:09
  • @ David, it doesnt work. dput(List_all_files) list("THREE20142305//tablesCORRECTED///fivea_2014-08-12", "THREE20142305//tablesCORRECTED///foura_2014-08-12", "THREE20142305//tablesCORRECTED///onea_2014-08-12", "THREE20142305//tablesCORRECTED///sevena_2014-08-12", "THREE20142305//tablesCORRECTED///sixa_2014-08-12", – user3919708 Aug 12 '14 at 18:09
  • So you were able to install the package? Also please provide `dput` of `List_all_files` before running my function. Btw, what is `foura_2014-08-12`?? You didn't have dates at the end of the string in the original question. Please make your problem reproducible, I can't guess that you have dates there if you won't mention it, common. – David Arenburg Aug 12 '14 at 18:10
  • 1
    @ david, I removed dates part and works perfect. The dates arent important. Sorry about that. This answer is amazing! – user3919708 Aug 12 '14 at 18:32
0

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.

farnsy
  • 2,282
  • 19
  • 22
  • Yeah, everyday these file will be overwritten. This is bad news. I can by hand go through and rename them. Was just hoping for a quick fix. Thanks – user3919708 Aug 11 '14 at 21:07