如何使fread执行read.delim

时间:2015-12-07 22:15:49

标签: r data.table

我有一个大的制表符分隔数据表,我试图使用data.table包fread函数读入R。但是,fread遇到错误。如果我使用read.delim,表格会被正确读入,但我无法弄清楚如何配置fread以便它正确处理数据。

为了找到解决方案,我已经安装了data.table的开发版本,因此我目前正在运行Ubuntu 15.10的R v3.2.2下运行data.table v1.9.7。

我已将问题隔离到我的大桌子的几行,你可以download it here

当我使用fread时:

> fread('problemRows.txt')
Error in fread("problemRows.txt") : 
  Expecting 8 cols, but line 3 contains text after processing all cols. It is very likely that this is due to one or more fields having embedded sep=',' and/or (unescaped) '\n' characters within unbalanced unescaped quotes. fread cannot handle such ambiguous cases and those lines may not have been read in as expected. Please read the section on quotes in ?fread.

我尝试使用read.delim使用的参数:

fread('problemRows.txt', sep="\t", quote="\"")

但我得到同样的错误。

有关如何正确阅读此内容的任何想法?我不确定究竟是什么问题。

谢谢!

1 个答案:

答案 0 :(得分:3)

通过最近的提交c1b7cdafread的引用逻辑在处理这些棘手的情况时变得更加聪明。有了这个:

require(data.table) # v1.9.7+
fread("my_file.txt")

应该正常工作。如果无法处理,错误消息现在也会提供更多信息。请参阅#1462

正如评论中所解释的那样,指定引号参数就可以了。

fread("my_file.txt", quote="")