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

8

有没有办法在R中指定一个由多个符号组成的注释字符?

例如:

read.table("data.dat", comment.char="//") 

无法工作。

1个回答

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"))
   }

测试:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接