【问题标题】:how to have fread perform like read.delim如何让 fread 像 read.delim 一样执行
【发布时间】:2023-03-23 17:50:02
【问题描述】:

我有一个大的制表符分隔的数据表,我正在尝试使用 data.table 包 fread 函数将其读入 R。但是,fread 遇到错误。如果我使用 read.delim,表格会正确读入,但我不知道如何配置 fread 以便正确处理数据。

为了寻找解决方案,我安装了 data.table 的开发版本,所以我目前正在运行 data.table v1.9.7,在 R v3.2.2 下,在 Ubuntu 15.10 上运行。

我已将问题从我的大表中隔离到几行,您可以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="\"")

但我得到了同样的错误。

关于如何正确读取此内容的任何想法?我不确定到底是什么问题。

谢谢!

【问题讨论】:

  • 第二行代码还有一个额外的“双引号”

标签: r data.table


【解决方案1】:

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

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

应该可以工作。如果无法处理,错误消息现在也会提供更多信息。见#1462


正如 cmets 中所解释的,指定引号参数就可以了。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-20
    • 2015-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-09
    相关资源
    最近更新 更多