【问题标题】:Problems with reading in a text file读取文本文件的问题
【发布时间】:2013-11-20 10:46:30
【问题描述】:

我正在尝试使用以下代码将文本文件读入 R:

d = read.table("test_data.txt")

它返回以下错误消息:

"Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings,  : 
  line 2 did not have 119 elements"

我试过这个:

read.table("man_cohort9_check.txt", header=T, sep="\t")

但它给出了这个错误:

"Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings,  : 
  line 43 did not have 116 elements"

我不明白出了什么问题??

【问题讨论】:

    标签: r csv file-io text-processing


    【解决方案1】:

    这是因为您的文件有不同列数的行。要开始调查,您可以运行:

    d = read.table("test_data.txt", fill=TRUE, header=TRUE, sep="\t")
    

    【讨论】:

    • @agstudy 哦耶.. :-) T<-1; > T [1] 1> TRUE<-1 Error in TRUE <- 1 : invalid (do_set) left-hand side to assignment 这样更安全!
    • 是的,更安全!在第二种方法中,myb OP 应该看到如何读取分隔文件,read.fwf!
    【解决方案2】:

    这种情况的常见原因是不匹配的引号和/或潜伏的八字形(“#”)。我将通过查看其中哪些生成最常规的表格来调查这些:

    table( countfields("test_data.txt", quote="", comment.char="") )
    table( countfields("test_data.txt", quote="") )
    table( countfields("test_data.txt", comment.char="") )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-23
      • 2011-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多