R:如何读取以“ ##”开头的注释行和以“#”开头的常规行的文件]]

问题描述 投票:0回答:1

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
    

read.delim和朋友的文档说“ comment.char”参数只能接受一个字符。有没有解决以“ ##”开头的注释行和...

r file comments delimiter
1个回答
1
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.