Skip to content
Prev 6149 / 63421 Next

feature request: comment character in read.table?

It was pretty straightforward.
  I haven't tested it very extensively, and not on a Mac at all.  The only
subtlety is skipping commented lines at the beginning of the file because
read.table assumes that the first line (not the first non-blank line) is
the header line if header=TRUE.

read.table.c <- function(file,comment="#",debug=FALSE,...) {
  infile <- file(file,"r")
  tmpfile <- file()
  cchar <- "#"
  while (length(cline <- readLines(infile,1))>0) {
    s <- strsplit(cline,cchar)[[1]][1]
    if (nchar(s)>0)  { ## skip blank lines to not screw up header
      if (debug) cat(s,"\n")
      writeLines(s,tmpfile)
    }
  }
  r <- read.table(tmpfile,...)
  close(infile)
  close(tmpfile)
  r
}
On Wed, 12 Sep 2001, Prof Brian D Ripley wrote: