r read.table 项目太多

标签 r read.table

我有一个大小为 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,内存为 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 列或更多列的行。

最佳答案

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

关于r read.table 项目太多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42923816/

相关文章:

在不知道其中的 csv 文件名的情况下读取 R 中的 zip 文件

将带有名称和标签的 .csv 文件读取到 R 中

r - 在 read.table() : incomplete final line found by readTableHeader

r - 为什么 R 在文件路径中使用正斜杠 (/) 而不是反斜杠 (\)

R 包 : writing internal data, 但不是一次全部

r - 在 R 中创建具有定义相关性的正态分布变量

将制表符分隔的数据读入 R

r - 捕获列模式频率

r - 带有 ggplot2 的标准 eval 没有 `aes_string()`

r - read.table : ! header 中的错误:无效的参数类型