【发布时间】: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(当没有任何 nrows 或 colClasses 时,使用这些参数它从未超过 60 Gb)。我不明白为什么 R 不能读取文件。
我还检查了没有包含 4 列或更多列的行。
【问题讨论】:
-
文件很大,你有没有试过
data.table::fread? -
感谢您的建议!我在谷歌搜索这个问题时遇到了
fread,以前从未使用过。我阅读了它的功能(github.com/Rdatatable/data.table/wiki/…),但我仍然不知道它是否与igraph兼容。这是一个边缘列表文件,接下来我需要做g <- graph_from_data_frame(df, directed = FALSE)。 -
是的,这应该不是问题。如果您愿意,您甚至可以告诉
fread返回data.frame而不是data.table(尽管我不一定建议使用这种大小的数据) -
@docendodiscimus 你可以添加这个作为答案吗?在我看来,这是最好的解决方案。
-
您可以按照@DamienCormann 的建议拆分文件。但是,请记住,将 53 GB 的文件作为一个对象读取会使得使用 128 GB RAM 很难用它做任何有用的事情。我建议您决定是否需要完整数据。
标签: r read.table