user2015601
user2015601

Reputation:

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

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.

Upvotes: 8

Views: 1363

Answers (1)

flodel
flodel

Reputation: 89107

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

Upvotes: 9

Related Questions