【问题标题】:r read.table too many itemsr read.table 项目太多
【发布时间】:2017-03-21 10:03:06
【问题描述】:

我有一个大小为 53 Gb 的文件,这是它的头部:

1   10  2873
1   100 22246
1   1000    28474
1   10000   35663
1   10001   35755
1   10002   35944
1   10003   36387
1   10004   36453
1   10005   36758
1   10006   37240

我在 CentOS7 64 位服务器上运行 R 3.3.2,RAM 为 128 Gb。我已将 4098 个类似文件读入 R。但是,我无法将最大的文件读入 R。

df <- read.table(f, header=FALSE, col.names=c('a', 'b', 'dist'), sep='\t', quote='', comment.char='')
Error in scan(file = file, what = what, sep = sep, quote = quote, dec = dec,  : har='')
  too many items

它返回“项目太多”的错误。然后我关注了这个tip

df5rows <- read.table(f, nrows=5, header=FALSE, col.names=c('a', 'b', 'dist'), sep='\t', quote='', comment.char='')
classes <- sapply(df5rows, class)
df <- read.table(f, nrows=3231959401, colClass=classes, header=FALSE, col.names=c('a', 'b', 'dist'), sep='\t', quote='', comment.char='')

它仍然显示“项目太多”和“引入了 NA”。我也试过没有colClasses,结果一样:

df <- read.table(f, nrows=3231959401, header=FALSE, col.names=c('a', 'b', 'dist'), sep='\t', quote='', comment.char='')
Error in scan(file = file, what = what, sep = sep, quote = quote, dec = dec,  : har='')
  too many items
In addition: Warning message:
In scan(file = file, what = what, sep = sep, quote = quote, dec = dec,  :
  NAs introduced by coercion to integer range

使用的内存从未超过 90 Gb(当没有任何 nrowscolClasses 时,使用这些参数它从未超过 60 Gb)。我不明白为什么 R 不能读取文件。

我还检查了没有包含 4 列或更多列的行。

【问题讨论】:

  • 文件很大,你有没有试过data.table::fread
  • 感谢您的建议!我在谷歌搜索这个问题时遇到了fread,以前从未使用过。我阅读了它的功能(github.com/Rdatatable/data.table/wiki/…),但我仍然不知道它是否与igraph 兼容。这是一个边缘列表文件,接下来我需要做g &lt;- graph_from_data_frame(df, directed = FALSE)
  • 是的,这应该不是问题。如果您愿意,您甚至可以告诉fread 返回data.frame 而不是data.table(尽管我不一定建议使用这种大小的数据)
  • @docendodiscimus 你可以添加这个作为答案吗?在我看来,这是最好的解决方案。
  • 您可以按照@DamienCormann 的建议拆分文件。但是,请记住,将 53 GB 的文件作为一个对象读取会使得使用 128 GB RAM 很难用它做任何有用的事情。我建议您决定是否需要完整数据。

标签: r read.table


【解决方案1】:

您是否尝试使用轻量级编辑器(例如(sed 或 VI))剪切文件? 然后你只需要合并两个数据集。 在具有大文件的非常相似的机器上,我遇到了同样的问题。 它是一条垃圾线,关于文件的大小,会发生这种错误。

【讨论】:

  • 我同意这是一个选项,但只需使用fread 即可解决此问题,无需任何额外步骤。另外有点吹毛求疵,sed 不是文本编辑器,而是命令行文本处理工具。
  • OP 更新了他的问题,你的建议现在似乎更相关了:)。 +1!
猜你喜欢
  • 2012-02-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-13
  • 2016-04-28
  • 1970-01-01
  • 1970-01-01
  • 2017-04-19
相关资源
最近更新 更多