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

8 comments r read.table

是否可以在R中指定一个由多于1个符号组成的注释字符?

例如,

read.table("data.dat", comment.char="//") 
Run Code Online (Sandbox Code Playgroud)

不行.

flo*_*del 9

我认为你不能,但这是一个解决方法.读取文件的函数,使用它来清除其行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"))
   }
Run Code Online (Sandbox Code Playgroud)

测试:

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