有没有办法在R中指定一个由多个符号组成的注释字符?
例如:
read.table("data.dat", comment.char="//")
无法工作。
有没有办法在R中指定一个由多个符号组成的注释字符?
例如:
read.table("data.dat", comment.char="//")
无法工作。
我认为你不能这样做,但是这里有一个解决方法。一个函数读入文件,使用sub
清理行,并将所有内容粘贴在一起,然后传递给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"))
}
测试:
file <- textConnection("3 4 //a
1 2")
my.read.table(file)
# V1 V2
# 1 3 4
# 2 1 2