[Rd] feature request: comment character in read.table?

Prof Brian D Ripley ripley@stats.ox.ac.uk
Thu, 13 Sep 2001 20:43:26 +0100 (BST)


I do intend to do this at a lower level where it will be robust.
As Peter says, quotes are one of the issues I had been thinking about.
I would be happy in the interim to insist on starting with a comment
character.

B


On Thu, 13 Sep 2001, Peter Kleiweg wrote:

> Ben Bolker scribeva...
>
> >
> >   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
> > }
>
> That is not very robust. What about these:
>
>     # a comment
>     1 2 3  # a comment
>            # a comment
>     "1" "2" "3  # not a comment"
>     "# not a comment"  # a comment
>
> Comments don't have to start at the first column, and comments
> can also exist after real data. A comment char within a string
> should not be taken as the start of a comment, and you also have
> to take into account that the tokens delimiting a string can
> vary.
>
> --
> Peter Kleiweg
> http://www.let.rug.nl/~kleiweg/
>
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> r-devel 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-devel-request@stat.math.ethz.ch
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
>

-- 
Brian D. Ripley,                  ripley@stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel 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-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._