[Rd] feature request: comment character in read.table?
Peter Kleiweg
kleiweg@let.rug.nl
Thu, 13 Sep 2001 20:35:32 +0200 (CEST)
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._