在 R 的 read.table() 中指定多字符注释标记



是否有可能在R中指定由1个以上符号组成的注释字符?

例如,

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

最新更新