[Rd] read.fwf; bug reports 8226 & 8236 (PR#8284)

From: <rolf_at_math.unb.ca>
Date: Sat 05 Nov 2005 - 15:56:47 GMT

It seems to me that the bug dealt with in bug reports 8226 and 8236 is still not fixed.

I obtained the revised version of read.fwf from the latest R-patched and tried

        try.it <- read.fwf("junk",w,header=TRUE,as.is=TRUE)

This gave the error

Error in read.table(file = FILE, header = header, sep = sep, as.is = as.is, :

        more columns than column names

Inspection of file ``FILE'' revealed that (of course) the first line of ``FILE'' was not tab separated and was hence being treated as a single field.

I then tried the last of Emmanuel Paradis' proposed fixes; this didn't quite work --- an extra ``sep'' (tab) gets tacked on at the end of the line being catted and makes read.table think that there is one more field than there actually are.

So I modified the fix to:

	if (header) {
        	headerline <- readLines(file, n = 1)
        	head.last  <- cumsum(widths)
        	head.first <- head.last - widths + 1
        	headerline <- substring(headerline,head.first,head.last)[!drop]
        	headerline <- paste(headerline,collapse=sep)
        	cat(file = FILE, headerline, "\n")

This works for me.

Brian Ripley says that this will crash with multiline records. Well, at least it works with single line records, as the current version seems not to.


					Rolf Turner

R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Sun Nov 06 03:00:46 2005

This archive was generated by hypermail 2.1.8 : Sat 05 Nov 2005 - 21:20:46 GMT