首页 > 解决方案 > fread 从大文件中读取前 n 行

问题描述

使用fread. 看起来是内存问题。我尝试使用nrows=1000. 但没有运气。使用Linux

file ok but could not memory map it. This is a 64bit process. There is probably not enough contiguous virtual memory available.

下面的代码可以用read.csv下面使用的所有选项替换吗?它有帮助吗?

  rdata<- fread(
      file=csvfile, sep= "|", header=FALSE, col.names= colsinfile,
    select= colstoselect, key = "keycolname", na.strings= c("", "NA")
    , nrows= 500
  )

标签: rdata.tablefread

解决方案


另一种解决方法是使用 shell 命令获取前 500 行:

rdata<- fread(
    cmd = paste('head -n 500', csvfile),
    sep= "|", header=FALSE, col.names= colsinfile,
    select= colstoselect, key = "keycolname", na.strings= c("", "NA")
)

不过,我不知道为什么nrows不起作用。


推荐阅读