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

标签 r comments 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

关于r - 在 R 的 read.table() 中指定多字符注释标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15513956/

相关文章:

r - 更改 visreg 线颜色

mysql - 如何使用sparklyr spark_write_jdbc连接MySql

r - 多个ggplots总标题

java - 可以注释掉 JSTL 代码吗?

C# 隐藏和取消隐藏评论

Bash 脚本将 x 个起始注释行提取到另一个文件中

r - R 中 read.table() 函数中的多个 na.strings

r - 列名在 read.table 或 read.csv 上向左移动

r - 防止 fread() 中的列类推断

删除 R 中整个数据帧列上的随机字符串的一部分