[R] Ordering Filenames stored in list or vector

BARLAS Marios 247554 Marios.BARLAS at cea.fr
Fri Dec 4 11:51:42 CET 2015


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 at 1.xls   Q_Read_prist#1 at 1.xls
Q_Read_prist#1 at 10.xls Q_Read_prist#1 at 10.xls
Q_Read_prist#1 at 11.xls Q_Read_prist#1 at 11.xls
Q_Read_prist#1 at 12.xls Q_Read_prist#1 at 12.xls
Q_Read_prist#1 at 13.xls Q_Read_prist#1 at 13.xls
Q_Read_prist#1 at 14.xls Q_Read_prist#1 at 14.xls
Q_Read_prist#1 at 15.xls Q_Read_prist#1 at 15.xls
Q_Read_prist#1 at 16.xls Q_Read_prist#1 at 16.xls
Q_Read_prist#1 at 17.xls Q_Read_prist#1 at 17.xls
Q_Read_prist#1 at 18.xls Q_Read_prist#1 at 18.xls
Q_Read_prist#1 at 19.xls Q_Read_prist#1 at 19.xls
Q_Read_prist#1 at 2.xls   Q_Read_prist#1 at 2.xls
Q_Read_prist#1 at 3.xls   Q_Read_prist#1 at 3.xls
Q_Read_prist#1 at 4.xls   Q_Read_prist#1 at 4.xls
Q_Read_prist#1 at 5.xls   Q_Read_prist#1 at 5.xls
Q_Read_prist#1 at 6.xls   Q_Read_prist#1 at 6.xls
Q_Read_prist#1 at 7.xls   Q_Read_prist#1 at 7.xls
Q_Read_prist#1 at 8.xls   Q_Read_prist#1 at 8.xls
Q_Read_prist#1 at 9.xls   Q_Read_prist#1 at 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]]



More information about the R-help mailing list