gpt4 book ai didi

r - 如何让 fread 像 read.delim 一样执行

转载 作者:行者123 更新时间:2023-12-01 13:48:13 24 4
gpt4 key购买 nike

我有一个很大的制表符分隔数据表,我正尝试使用 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="\"")

但我得到了同样的错误。

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

谢谢!

最佳答案

最近的提交c1b7cda , fread 的引用逻辑在处理这种棘手的情况时变得更聪明了。有了这个:

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

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


如评论中所述,指定引号参数就可以了。

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

关于r - 如何让 fread 像 read.delim 一样执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34144314/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com