忽略文本文件头部的#号(R语言)

3
我将使用R中的read.table来读取一个文件,其标题如下:
ColA ColB# ColC ColD ColE

然而,在标题名称中包含 '#' 会导致 read.table 混淆,从而出现以下错误:

*Error in read.table(paste(path, file, sep = ""), skip = SKIP_LINES, sep = "", : more columns than column names*

有什么建议可以摆脱这个错误信息吗?

2个回答

3
为了改进BrodieG的回答,假如文件中有随机的问号("?"),当你想忽略任何和所有注释字符时,使用comment.char=""是正确的约定。
read.table(comment.char="", header=T, check.names=F, text="ColA ColB# ColC ColD ColE\n1 2 3 4 5").

这将会提供:

 ColA ColB# ColC ColD ColE
1    1     2    3    4    5

2
尝试使用read.table(comment.char="?" ...),其中的comment.char是你表格中没有的内容:
read.table(
  comment.char="?", header=T, check.names=F,
  text="ColA ColB# ColC ColD ColE\n1 2 3 4 5"
)
#   ColA ColB# ColC ColD ColE
# 1    1     2    3    4    5

1
...并使用 check.names = FALSE 以保留 #(如果需要的话)。 - joran

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