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

Ek Esawi e@@w|ek @end|ng |rom gm@||@com
Tue Oct 9 22:38:42 CEST 2018


Hi again,

I worked with RUi's idea of using the match function with month.name.
I got numerical values for months then i sorted and pasted the PDF
file extension. It gave me the file order i wanted, but now statements
8,9,&10 don't work and i kept getting an error which is listed below.
The dilemma is if i add full.names=TRUE in statement 6 then statements
9 and 10 don't produce what they did earlier. If i put
full.names=FALSE, then i am back to square 1.
Any idea is greatly appreciated.:

The code

1. nstall.packages("tabulizer")
2. installed.packages("stringr")
3. library(stringr)
4. library(tabulizer)
5. path = "C:/Users/namei/Documents/TextMining/S2017"
6. file.names <- dir(path, pattern =".PDF",full.names = TRUE)
7. file.names <- str_remove(file.names,"\\s[0-9][0-9]")
8. FNs <- sort(match(sub("\\.PDF", "", file.names), month.name))
9. FNs1 <- paste0(month.name[FNs],".","PDF")
10 A <- lapply(FNs1, function(i) extract_tables(i))

Output and the error message.

path = "C:/Users/eesawi/Documents/TextMining/S2017"
> file.names <- dir(path, pattern =".PDF",full.names = TRUE)
> file.names <- str_remove(file.names,"\\s[0-9][0-9]")
> FNs <- sort(match(sub("\\.PDF", "", file.names), month.name))
> FNs1 <- paste0(month.name[FNs],".","PDF")
> A <- lapply(FNs1, function(i) extract_tables(i))
 Show Traceback

 Error in normalizePath(path.expand(path), winslash, mustWork) :
  path[1]=".PDF": The system cannot find the file specified
On Tue, Oct 9, 2018 at 9:44 AM Ek Esawi <esawiek using gmail.com> wrote:
>
> 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"




More information about the R-help mailing list