【问题标题】:Ignore # in header in text file (R language)忽略文本文件头中的#(R语言)
【发布时间】:2014-01-21 16:17:14
【问题描述】:

我在 R 中使用 read.table 读取具有以下标题的文件:

ColA ColB# ColC ColD ColE

但是,在标题名称中包含“#”会使 read.table 混淆,我收到以下错误:

*read.table 中的错误(paste(path, file, sep = ""), skip = SKIP_LINES, sep = "", : 列多于列名*

有什么建议可以消除错误信息吗?

【问题讨论】:

    标签: r input header read.table


    【解决方案1】:

    为了改进 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
      

      【讨论】:

      • ...如果需要,使用check.names = FALSE 保留#。
      猜你喜欢
      • 2014-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-04
      • 2011-12-26
      • 1970-01-01
      相关资源
      最近更新 更多