Specifying multi-character comment tokens in R's read.table()

comments, r, read.table

Solution

I don't think you can but here is a workaround. A function that reads the file in, cleans its lines using `sub`, and pastes everything back together before passing it to `read.table`:

my.read.table <- function(file, comment.char = "//", ...) {
  clean.lines <- sub(paste0(comment.char, ".*"), "", readLines(file))
  read.table(..., text = paste(clean.lines, collapse = "\n"))
   }

Testing:

file <- textConnection("3 4 //a
                        1 2")
my.read.table(file)
#   V1 V2
# 1  3  4
# 2  1  2

Problem

Is it somehow possible to specify a comment character in R that consists of more than 1 symbol? for example, ``` read.table("data.dat", comment.char="//") ``` won't work.

Original source