2

假设我们有这种情况:

  1. 我有很多 .RData 文件,它们超过 100mb(无论如何,但很大)。
  2. 在每个 .RData 文件中都有一个名为“Dataset_of_interest”的数据集,它们都是我想要创建的一个大数据集的一部分。

所以我想知道是否有可能只将我感兴趣的数据集加载到内存中,而不是加载整个 .RData 文件?

我想循环加载每个“Dataset_of_interest”,合并到一个大文件中,然后将其保存在一个文件中。

编辑:我在 Windows 7 上工作。

4

1 回答 1

3

我认为这是可能的,但需要一些并行处理能力。每个工作人员都会加载 .RData 文件并输出所需的对象。合并结果可能非常简单。

我无法为您的数据提供代码,因为我不知道结构,但我会按照下面的块'o'代码的行做一些事情。请注意,我使用的是 Windows,您的工作流程可能会有所不同。您不应该缺少计算机内存。此外,降雪并不是使用多核的唯一接口。

# load library snowfall and set up working directory
# to where the RData files are
library(snowfall)
working.dir <- "/path/to/dir/with/files"
setwd(working.dir)

# initiate (redneck jargon: and then she ate) workers and export
# working directory. Working directory could be hard coded into
# the function, rendering this step moot
sfInit(parallel = TRUE, cpus = 4, type = "SOCK")
sfExport(list = c("working.dir")) # you need to export all variables but x

# read filenames and step through each, returning only the
# desired object
lofs <- list.files(pattern = ".RData")
inres <- sfSapply(x = lofs, fun = function(x, wd = working.dir) {
    setwd(wd)
    load(x)
    return(Dataset_of_interest)
  }, simplify = FALSE)
sfStop()

# you could post-process the data by rbinding, cbinding, cing...
result <- do.call("rbind", inres)
于 2012-08-07T07:19:52.803 回答