【问题标题】:Suppress wc -l in data.table's fread在 data.table 的 fread 中禁止 wc -l
【发布时间】:2015-10-22 08:39:27
【问题描述】:

如果文件很大(约 30 GB),我会分块读取,并注意到大部分时间都花在了对整个文件执行行计数上。

Read 500000 rows and 49 (of 49) columns from 28.250 GB file in 00:01:09
   4.510s (  7%) Memory map (rerun may be quicker)
   0.000s (  0%) sep and header detection
  53.890s ( 79%) Count rows (wc -l)
   0.010s (  0%) Column type detection (first, middle and last 5 rows)
   0.120s (  0%) Allocation of 500000x49 result (xMB) in RAM
   9.780s ( 14%) Reading data
   0.000s (  0%) Allocation for type bumps (if any), including gc time if triggered
   0.000s (  0%) Coercing data already read in type bumps (if any)
   0.060s (  0%) Changing na.strings to NA
  68.370s        Total

是否可以指定 fread 在我每次读取块时不进行完整的行计数,或者这是一个必要的步骤?

编辑: 这是我正在运行的确切命令:

fread(pfile, skip = 5E6, nrows = 5E5, sep = "\t", colClasses = rpColClasses, na.strings = c("NA", "N/A", "NULL"), head = FALSE, verbose = TRUE)

【问题讨论】:

  • 你可以试试低层scan
  • 不清楚您运行的是哪个版本。我认为它是 1.9.4。你能试试1.9.5吗? This commit 似乎完全按照您提到的方式处理它。
  • 我不相信fread 不再使用wc -l,您仍然可以访问该文件吗?可以重新运行吗?

标签: r data.table


【解决方案1】:

我不确定你是否可以在 fread 中“关闭”wc -l 命令。尽管如此,我确实有两个答案。

答案 1:在调用 fread 之前,使用 Unix 命令split 将大数据集分成块。我发现在处理大数据集(即不适合 RAM 的数据)时,了解一点 Unix 会有很长的路要走。

split -b 1m myfile.csv #breaks your file into 1mb chunks. 

答案 2:使用连接。不幸的是,这种方法不适用于 fread。查看我之前的帖子以了解我所说的使用连接的含义。 Strategies for reading in CSV files in pieces?

【讨论】:

  • 可能想提一下write.*函数的'append'参数。
  • @BondedDust 我看不出write.* 中的附加选项在这种情况下有何帮助。能详细点吗?
  • 我假设当您将输入拆分为段时,您试图构建一个不适合内存的大型 csv 文件。错了吗?
  • @BondedDust 我假设 mkemp6 正在尝试将数据加载到一个块中,分析数据,从内存中删除该块,将结果保存在本地工作空间中并重复。我不喜欢 mkemp6,而且我可能是错的,它是以块的形式加载数据只是为了重新组合这些块(可能是在剔除多余的数据之后)。如果他这样做,那么是的,您的评论确实有意义。当然,对于简单的情况,Unix 命令cut 非常适合从大型数据集中剔除不必要的信息。
猜你喜欢
  • 1970-01-01
  • 2020-05-10
  • 1970-01-01
  • 1970-01-01
  • 2014-10-28
  • 2022-11-09
  • 2013-10-14
  • 1970-01-01
  • 2015-03-30
相关资源
最近更新 更多