Mario,

I am certain there are more elegant solutions. This is an effort to make the 
process clear by dividing out each transformation used into separate lines.

## Start of code
library(stringi) # This is written in C and C++ (ICU library), is fast, and is 
well documented.
filenames <- c("Q_Read_prist#1...@1.xls", "Q_Read_prist#1...@10.xls", 
           "Q_Read_prist#1...@11.xls", "Q_Read_prist#1...@12.xls", 
           "Q_Read_prist#1...@13.xls", "Q_Read_prist#1...@14.xls", 
           "Q_Read_prist#1...@15.xls", "Q_Read_prist#1...@16.xls", 
           "Q_Read_prist#1...@17.xls", "Q_Read_prist#1...@18.xls", 
           "Q_Read_prist#1...@19.xls", "Q_Read_prist#1...@2.xls", 
           "Q_Read_prist#1...@3.xls", "Q_Read_prist#1...@4.xls", 
           "Q_Read_prist#1...@5.xls", "Q_Read_prist#1...@6.xls", 
           "Q_Read_prist#1...@7.xls", "Q_Read_prist#1...@8.xls", 
           "Q_Read_prist#1...@9.xls")
indx_list <- stri_split_regex(filenames, pattern = "[@.]")
indx <- sapply(indx_list, function(x) {x[[2]]})
filenames_df <- data.frame(file_name = filenames, indx = indx, 
                            stringsAsFactors = FALSE)
filenames_ordered <- filenames_df[order(as.numeric(filenames_df$indx)), 
                                    "file_name"]
filenames_ordered
## end of code
Mark


R. Mark Sharp, Ph.D.
Director of Primate Records Database
Southwest National Primate Research Center
Texas Biomedical Research Institute
P.O. Box 760549
San Antonio, TX 78245-0549
Telephone: (210)258-9476
e-mail: msh...@txbiomed.org







> On Dec 4, 2015, at 4:51 AM, BARLAS Marios 247554 <marios.bar...@cea.fr> wrote:
> 
> Hello everyone,
> 
> I am an R rookie and I'm learning as I program.
> 
> I am working on a script to process a large amount of data: I read a pattern 
> of filenames in the folder I want and import their data
> 
> filenames = list.files(path, pattern="*Q_Read_prist*")
> 
> myfiles = lapply(filenames, function(x) read.xlsx2(file=x, sheetName="Data", 
> header=TRUE, FILENAMEVAR=x))
> 
> The problem is that R recognizes the files in a 'non human' order.
> 
> Q_Read_prist#1...@1.xls   Q_Read_prist#1...@1.xls
> Q_Read_prist#1...@10.xls Q_Read_prist#1...@10.xls
> Q_Read_prist#1...@11.xls Q_Read_prist#1...@11.xls
> Q_Read_prist#1...@12.xls Q_Read_prist#1...@12.xls
> Q_Read_prist#1...@13.xls Q_Read_prist#1...@13.xls
> Q_Read_prist#1...@14.xls Q_Read_prist#1...@14.xls
> Q_Read_prist#1...@15.xls Q_Read_prist#1...@15.xls
> Q_Read_prist#1...@16.xls Q_Read_prist#1...@16.xls
> Q_Read_prist#1...@17.xls Q_Read_prist#1...@17.xls
> Q_Read_prist#1...@18.xls Q_Read_prist#1...@18.xls
> Q_Read_prist#1...@19.xls Q_Read_prist#1...@19.xls
> Q_Read_prist#1...@2.xls   Q_Read_prist#1...@2.xls
> Q_Read_prist#1...@3.xls   Q_Read_prist#1...@3.xls
> Q_Read_prist#1...@4.xls   Q_Read_prist#1...@4.xls
> Q_Read_prist#1...@5.xls   Q_Read_prist#1...@5.xls
> Q_Read_prist#1...@6.xls   Q_Read_prist#1...@6.xls
> Q_Read_prist#1...@7.xls   Q_Read_prist#1...@7.xls
> Q_Read_prist#1...@8.xls   Q_Read_prist#1...@8.xls
> Q_Read_prist#1...@9.xls   Q_Read_prist#1...@9.xls
> 
> I tried to order them using order or sort but it doesn' seem to work. I have 
> had the same issue in matlab but there I have a function to re-define the 
> order in a "correct" way.
> 
> Anyone knows of a smart way to sort these guys from 1 to 19 ascending or 
> descending?
> 
> Thanks in advance,
> Mario
> 
>       [[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to