r - 跳过在 fread R 中产生错误的行/行

标签 r fread

我尝试将一个大文件读入 r。在尝试读取它时,会发生此错误。即使我跳过前 800607 行,它也不会消失。我还尝试使用命令删除终端中的行。

sed '800608d' filename.csv

它没有解决我的问题。如果您能帮助我,我将不胜感激。

我从 R 得到的原始错误是:

> data<-fread("filename.csv")
Read 2.0% of 34143409 rows
Error in fread("filename.csv") : 
Field 16 on line 800607 starts with quote (") but then has a problem. It can contain balanced unescaped quoted subregions but if it does it can't contain embedded \n as well. Check for unbalanced unescaped quotes: """The attorney for Martin's family, Benjamin Crump, says the evidence is ""irrelevant\"""" """".","NULL","NULL","NULL","NULL","NULL","NULL","NULL","Negative"
In addition: Warning message:
 In fread("filename.csv") :
Starting data input on line 8 and discarded previous non-empty line: done

最佳答案

我目前正在自己​​解决此类问题。我不确定这是否适用于所有情况——更不用说我自己处理的所有文件了。但是现在我似乎在以下方面取得了一些成功:

skip.list <- c()

for (i in 1:length(dir(input.dir))){ # i=3
  file <- dir(input.dir)[i]
  ingested.file <- NULL
  ingested.file <- try(fread(paste0(input.dir,file), header=T, stringsAsFactors=F))
  if (class(ingested.file)=="try-error") {
    error.line <-as.integer(sub(" .*","",sub(".*but line ","",as.character(ingested.file))))
    app.reviews.input <- try(fread(paste0(input.dir,file), header=T, stringsAsFactors=F,skip=error.line))
    if (class(ingested.file)=="try-error") {
      skip.list_by.downloads <- c(skip.list_by.downloads, file)
      next
    }
  }
}

我目前正在处理大约 750 个文件,每个文件 1000 行——其中大约 50 个文件有相同的问题。但是,使用这种方法,我能够阅读这 50 本书中的 30 本书;剩下的 20 个似乎在多行中有错误,但我无法指定多个跳过值。

如果可以指定更多的跳过,那么您可以尝试使用 while 语句。即

while (class(ingested.file)=="try-error") ... 然后根据需要自动多次更新 error.list。

希望对您有所帮助!

关于r - 跳过在 fread R 中产生错误的行/行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32440255/

相关文章:

r - 获取元素随机从另一个表B取数据表

包含列表的数据框中的 R 元素计算

c - 使用 fread 更改结构值 C 时出现问题

c - 第一次调用 fread 后 ftell 错误

fread - fread()中的 "short item count"是什么?

r - 按 R 中的位置对列表进行平均

使用 tidyverse 将分组数据中的缺失值替换为非值

r - 有没有办法使用 geom_abline 绘制垂直线?

clang-分析 : how to avoid "garbage value" warning?

c - 关于用c解密图像文件的许多疑问