【问题标题】:R: How to read in a file with comment lines starting with "##" and some regular lines starting with "#"R:如何读取带有以“##”开头的注释行和一些以“#”开头的常规行的文件
【发布时间】:2020-06-14 21:33:55
【问题描述】:

read.delim 和朋友的文档说“comment.char”参数只能接受一个字符。

注释行以“##”开头,真行以“#”开头的问题有解决办法吗?

一些生物信息学文件格式可以做到这一点。标题行以“#”开头

可惜没有正则表达式选项。

### Write file with comment line indicated by "##"
### Read in with comment.char="#"
text1 = "##comment\nCol1\tCol2\n10\t20"
write(text1, file="text1.txt")
t1 = read.delim("text1.txt", comment.char="#")
print(t1)
#>   Col1 Col2
#> 1   10   20

### Write file with comment line indicated by "##"
### and header column starting with "#"
### Read in with comment.char="#"
text2 = "##comment\n#Col1\tCol2\n10\t20"
write(text2, file="text2.txt")
t2 = read.delim("text2.txt", comment.char="#")
print(t2)
#> [1] X10 X20
#> <0 rows> (or 0-length row.names)

### Write file with comment line indicated by "##"
### and header column starting with "#"
### Read in with comment.char="##"
text3 = "##comment\n#Col1\tCol2\n10\t20"
write(text3, file="text3.txt")
t3 = read.delim("text3.txt", comment.char="##")
#> Error in read.table(file = file, header = header, sep = sep, quote = quote, : invalid 'comment.char' argument
print(t3)
#> Error in print(t3): object 't3' not found

【问题讨论】:

标签: r file comments delimiter


【解决方案1】:

预处理文件删除双 "##" 是解决问题的一种方法。然后从生成的字符向量中读取。

removeDoubleChar <- function(x, ...){
  txt <- readLines(x)
  txt <- sub('^#([^#]*)', '\\1', txt)
  read.delim(text = txt, comment.char = "#", ...)
}

fls <- list.files(pattern = '^t.*\\.txt')
lapply(fls, removeDoubleChar)
#[[1]]
#  Col1 Col2
#1   10   20
#
#[[2]]
#  Col1 Col2
#1   10   20
#
#[[3]]
#  Col1 Col2
#1   10   20

【讨论】:

  • 谢谢!我使用sed 进行了预处理,但在 R 中这样做是有意义的。根据您的建议,我想我会制作一个不同的包装器来一次完成整个工作。
猜你喜欢
  • 2017-07-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-30
  • 2020-10-31
  • 2015-02-15
  • 1970-01-01
  • 2018-01-05
相关资源
最近更新 更多