[R] read.table() suggestions

Ben Bolker ben at zoo.ufl.edu
Wed Sep 26 21:31:21 CEST 2001


  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:

> 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
>
>
>

-- 
318 Carr Hall                                bolker at zoo.ufl.edu
Zoology Department, University of Florida    http://www.zoo.ufl.edu/bolker
Box 118525                                   (ph)  352-392-5697
Gainesville, FL 32611-8525                   (fax) 352-392-3704

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list