您所描述的内容通过 RevoScaleR 的单个功能并不容易实现。你所描述的rxSplit
是一种方式。在这里,将结果与aggregate
内存中的结果进行比较,以表明它们是相同的。
set.seed(1234)
myData <- data.frame(year = factor(sample(2000:2015, size = 100, replace = TRUE)),
x = rnorm(100))
xdfFile <- rxDataStep(inData = myData, outFile = "test.xdf", rowsPerRead = 10)
newDir <- file.path(getwd(), "splits")
dir.create(newDir)
splitFiles <- rxSplit(inData = xdfFile,
outFilesBase = paste0(newDir, "/", gsub(".xdf", "",
basename(xdfFile@file))),
splitByFactor = "year")
minFun <- function(xdf) {
dat <- rxDataStep(inData = xdf, reportProgress = 0)
data.frame(year = dat$year[1], minPos = which.min(dat$x))
}
minPos <- do.call(rbind, lapply(splitFiles, minFun))
row.names(minPos) <- NULL
minPos
aggregate(x ~ year, data = myData, FUN = which.min
上面确实假设每组中的数据都可以放入 RAM。如果不是这种情况,则需要进行一些调整。
假设各个组可以放入 RAM,还有另一种解决方案,那就是使用RevoPemaR
包。
library("RevoPemaR")
rxSort(inData = xdfFile, outFile = xdfFile, sortByVars = "year", overwrite = TRUE)
byGroupPemaObj <- PemaByGroup()
minByYear <- pemaCompute(pemaObj = byGroupPemaObj, data = xdfFile,
groupByVar = "year", computeVars = "x",
fnList = list(
minPos = list(FUN = which.min, x = NULL)))
minPos