[R] Reorder file names read by list.files function

PIKAL Petr petr@p|k@| @end|ng |rom prechez@@cz
Tue Oct 9 16:07:52 CEST 2018


You could use brute force approach. Just print out "file.names" and estimate ordering vector.
In czech locale it is

oo <- c(6, 11, 1, 4, 5, 2, 3, 10, 12, 9, 7, 8)

In english locale it is different :-)

After that

should give you correct order of file names


> -----Original Message-----
> From: R-help <r-help-bounces using r-project.org> On Behalf Of Ek Esawi
> Sent: Tuesday, October 9, 2018 3:44 PM
> To: r-help using r-project.org
> Subject: [R] Reorder file names read by list.files function
> Hi All--
> I used base R list.file function to read files from a directory. The file names are
> months (April, August, etc). That's the system reads them in alphabetical order.,
> but i want to reordered them in calendar order (January, February,
> ...December).. I thought i might be able to do it via RegEx or possibly gtools
> package, I am wondering if there is an easier way.
> Thanks--EK
> Example
> path = "C:/Users/name/Downloads/MyFiles"
> file.names <- dir(path, pattern =".PDF")
> Example output
> Output:
> "February.PDF"  "January.PDF" "March.PDF"
> Desired output
> "January.PDF"  "February.PDF" "March.PDF"
> ______________________________________________
> R-help using 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.
Osobní údaje: Informace o zpracování a ochraně osobních údajů obchodních partnerů PRECHEZA a.s. jsou zveřejněny na: https://www.precheza.cz/zasady-ochrany-osobnich-udaju/ | Information about processing and protection of business partner’s personal data are available on website: https://www.precheza.cz/en/personal-data-protection-principles/
Důvěrnost: Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a podléhají tomuto právně závaznému prohláąení o vyloučení odpovědnosti: https://www.precheza.cz/01-dovetek/ | This email and any documents attached to it may be confidential and are subject to the legally binding disclaimer: https://www.precheza.cz/en/01-disclaimer/

More information about the R-help mailing list