Skip to content

read.table() suggestions

2 messages · David Larsen, Ben Bolker

#
David Brahm wrote:
> Hi,
 >
 >   I understand work is being done to improve read.table(), especially by
 > Prof. Brian D. Ripley.  I offer below a version that I wrote, in the 
hope some
 > aspects of it may prove useful or at least inspire discussion.



I have a read.table suggestion:

I find over the years, I forget many of the details about a data set so
I like to add comment statements to the file.  I know that if the comments
are in the start of the file I can use skip but it is nice to be able to add
comments and have R ignore them.

I implemented this in the following crude way:


###############################################
read.table( ... , comment="#" )

# after the opening of file

system( paste( "sed '/^[\\", comment, "]/D' ", file, ">", tempname, sep 
= ""))
file <- tempname
# continue processing file

###############################################
I would like see something like this added to the offiicial read.table

     Dave Larsen
#
Actually, Brian Ripley just implemented this -- it should be in R-devel
already.  I had previously written the following, which doesn't rely on
outside programs (so can e.g. be used on Windows machines without sed).

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, 26 Sep 2001, David Larsen wrote: