[R] Reorder file names read by list.files function
ru|pb@rr@d@@ @end|ng |rom @@po@pt
Tue Oct 9 16:09:39 CEST 2018
You can use the built in variable month.name to get the calendar order
and match it with your file names.
i <- match(sub("\\.PDF", "", file.names), month.name)
# "January.PDF" "February.PDF" "March.PDF"
Hope this helps,
Às 14:44 de 09/10/2018, Ek Esawi escreveu:
> 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.
> path = "C:/Users/name/Downloads/MyFiles"
> file.names <- dir(path, pattern =".PDF")
> Example 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
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help