[R] Reading multiple text files where some files are empty

Michael Sumner mdsumner at gmail.com
Tue Aug 31 06:56:08 CEST 2010


What about trimming the file list based on a minimum size? You can
also use the arguments to list.files to get only certain patterns in
file names, and otherwise you can put a condition in the function
passed to lapply to return NULL, which will rbind ok, or process x in
a similar way.

a <- a[file.info(a)$size > 1]



On Tue, Aug 31, 2010 at 2:47 PM, Paul <p.franklin at niwa.co.nz> wrote:
>
> Hi All,
>
> I have a problem with reading in multiple text files where some of the files
> have no data and was hoping someone may be able to help me find a solution.
>
> Each text file is a daily log of fish movement. However, on some occasions
> no movements will be recorded on a particular day and therefore the text
> file for that day is empty.
>
> I'm currently using the following code to read the files:
>
> a<-list.files()
> x<-lapply(a,function(x){read.table(x,colClasses='character')}) #the
> colClasses is to ensure zeros at the beginning of tag numbers are included
> myData<-do.call(rbind, x)
>
> This works fine as long as all the files being read have data in them, but
> if one of the empty log files is included it returns the following error:
>
> Error in read.table(x, colClasses = "character", blank.lines.skip = TRUE) :
>  no lines available in input
>
> Does anyone know of a way that I can overcome this (maybe skip empty files
> somehow?) without having to manually trawl through the hundreds of files I
> have to remove the empty ones?
>
> Thanks in advance,
> Paul
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Reading-multiple-text-files-where-some-files-are-empty-tp2401035p2401035.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> 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.
>



-- 
Michael Sumner
Institute for Marine and Antarctic Studies, University of Tasmania
Hobart, Australia
e-mail: mdsumner at gmail.com



More information about the R-help mailing list