8

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.

Matthew Lundberg
  • 42,009
  • 6
  • 90
  • 112

1 Answers1

9

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
thelatemail
  • 91,185
  • 12
  • 128
  • 188
flodel
  • 87,577
  • 21
  • 185
  • 223