Skip to content
Prev 376849 / 398502 Next

Reorder file names read by list.files function

Instead of changing the order in which you read the files, perhaps your analysis will work if you sort the data after you read it in. This may require that you add the month names as a column in the data frames, or you may already have dates in the data that you could sort by.

One idea:

fnames <- paste0( month.name, ".PDF" )
resultdf <- do.call( rbind, lapply(fnames, function(fn) { read.csv( file.path( "datadir", fn ), as.is=TRUE ) } )

but that only works if there are exactly 12 files. If there could be fewer, perhaps:

fnames <- list.files( "datadir" )
sfnames <- fnames[ match( sub("\\.PDF", "", fnames ), month.name ) ]
On October 9, 2018 6:44:21 AM PDT, Ek Esawi <esawiek at gmail.com> wrote: