[R] How to get read.table() or scan() to report line on which error occurs

David Clunie dclunie at dclunie.com
Thu Jan 24 15:38:08 CET 2013


Hi

Please excuse this naive question, but I have search the FAQs and lists
and web for an answer but not found one.

I have a text file of several hundred thousand rows and somewhere in the middle
there is a malformed row.

Doing:

   mydata<-read.table("mydata.dat", header=FALSE, sep=" ",
     col.names=mydatacolnames, colClasses=mydatacolclasses, row.names="hash")

results in:

   Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings,  :
     scan() expected 'a real', got 'MONOCHROME2'
   Calls: read.table -> scan
   Execution halted

which is not very helpful, since it does not report where the error occurs,
and searching for the offending string does not always help because it may
occur many times.

I know that I can do a:

   count.fields("mydata.dat", sep=" ")

and trawl through the results to find a row that does not have the same number
of fields, and that worked on this occasion just fine.

But I would like to know for next time if there is an easier way to just command
read.table() or scan() to give a more helpful message in the first place?

Thanks ... David



More information about the R-help mailing list