以下是一个可重现的示例我正在经历和坚持的情况(它是测试客户端我用来评估合并数据集用于我的论文研究。)
testData <- "https://github.com/abnova/test/blob/master/mergeTestData.zip?raw=true"
tmpFile <- tempfile()
tmpDir <- tempdir()
download.file(testData, tmpFile, method = 'curl',
extra = '-L', quiet = TRUE)
testFiles <- unzip(tmpFile, exdir = tmpDir)
# To enable desired merge option, uncomment corresponding line
#MERGE_OPTION <- "lapply_merge"
#MERGE_OPTION <- "lapply_merge2"
#MERGE_OPTION <- "reduce_merge"
#MERGE_OPTION <- "reduce_merge2"
#MERGE_OPTION <- "reshape"
#MERGE_OPTION <- "plyr"
#MERGE_OPTION <- "dplyr"
MERGE_OPTION <- "data.table"
#MERGE_OPTION <- "data.table2"
loadData <- function (dataFile) {
if (file.exists(dataFile)) {
data <- readRDS(dataFile)
}
else { # error() undefined - replaced for stop() for now
stop("Data file \'", dataFile, "\' not found! Run 'make' first.")
}
return (data)
}
loadDataSets <- function (dataDir) {
dataSets <- list()
dataFiles <- dir(dataDir, pattern='\\.rds$')
dataSets <- lapply(seq_along(dataFiles),
function(i) {
nameSplit <- strsplit(dataFiles[i], "\\.")
dataset <- nameSplit[[1]][1]
assign(dataset,
loadData(file.path(dataDir, dataFiles[i])))
return (get(dataset))
})
return (dataSets)
}
# load the datasets of transformed data
dataSets <- loadDataSets(tmpDir)
if (MERGE_OPTION == "lapply_merge") { # Option 1
flossData <- data.frame(dataSets[[1]][1])
# merge all loaded datasets by common column ("Project ID")
silent <- lapply(seq(2, length(dataSets)),
function(i) {merge(flossData, dataSets[[1]][i],
by = "Project ID",
all = TRUE)})
}
if (MERGE_OPTION == "lapply_merge2") { # Option 1
pids <- which(sapply(dataSets,
FUN=function(x) {'Project ID' %in% names(x)}))
flossData <- dataSets[[pids[1]]]
for (id in pids[2:length(pids)]) {
flossData <- merge(flossData, dataSets[[id]],
by='Project ID', all = TRUE)
}
}
if (MERGE_OPTION == "reduce_merge") { # Option 2
flossData <- Reduce(function(...)
merge(..., by.x = "row.names", by.y = "Project ID", all = TRUE),
dataSets)
}
# http://r.789695.n4.nabble.com/merge-multiple-data-frames-tt4331089.html#a4333772
if (MERGE_OPTION == "reduce_merge2") { # Option 2
mergeAll <- function(..., by = "Project ID", all = TRUE) {
dotArgs <- list(...)
dotNames <- lapply(dotArgs, names)
repNames <- Reduce(intersect, dotNames)
repNames <- repNames[repNames != by]
for(i in seq_along(dotArgs)){
wn <- which( (names(dotArgs[[i]]) %in% repNames) &
(names(dotArgs[[i]]) != by))
names(dotArgs[[i]])[wn] <- paste(names(dotArgs[[i]])[wn],
names(dotArgs)[[i]], sep = ".")
}
Reduce(function(x, y) merge(x, y, by = by, all = all), dotArgs)
}
flossData <- mergeAll(dataSets)
}
if (MERGE_OPTION == "reshape") { # Option 3
if (!suppressMessages(require(reshape))) install.packages('reshape')
library(reshape)
flossData <- reshape::merge_all(dataSets)
}
if (MERGE_OPTION == "plyr") { # Option 4
if (!suppressMessages(require(plyr))) install.packages('plyr')
library(plyr)
flossData <- plyr::join_all(dataSets)
}
if (MERGE_OPTION == "dplyr") { # Option 5
if (!suppressMessages(require(dplyr))) install.packages('dplyr')
library(dplyr)
flossData <- dataSets[[1]][1]
flossData <- lapply(dataSets[[1]][-1],
function(x) {dplyr::left_join(x, flossData)})
}
if (MERGE_OPTION == "data.table") { # Option 6
if (!suppressMessages(require(data.table)))
install.packages('data.table')
library(data.table)
flossData <- data.table(dataSets[[1]], key="Project ID")
for (id in 2:length(dataSets)) {
flossData <- merge(flossData, data.table(dataSets[[id]]),
by='Project ID', all.x = TRUE, all.y = FALSE)
}
}
# http://stackoverflow.com/a/17458887/2872891
if (MERGE_OPTION == "data.table2") { # Option 6
if (!suppressMessages(require(data.table)))
install.packages('data.table')
library(data.table)
DT <- data.table(dataSets[[1]], key="Project ID")
flossData <- lapply(dataSets[[1]][-1], function(x) DT[.(x)])
}
# Additional Transformations (see TODO above)
# convert presence of Repo URL to integer
flossData[["Repo URL"]] <- as.integer(flossData[["Repo URL"]] != "")
# convert License Restrictiveness' factor levels to integers
#flossData[["License Restrictiveness"]] <-
# as.integer(flossData[["License Restrictiveness"]])
# convert User Community Size from character to integer
flossData[["User Community Size"]] <-
as.integer(flossData[["User Community Size"]])
# remove NAs
#flossData <- flossData[complete.cases(flossData[,3]),]
rowsNA <- apply(flossData, 1, function(x) {any(is.na(x))})
flossData <- flossData[!rowsNA,]
print(str(flossData))
错误消息如下:
Starting bmerge ...done in 0.001 secs
Starting bmerge ...done in 0.002 secs
Error in vecseq(f__, len__, if (allow.cartesian) NULL else as.integer(max(nrow(x), :
将结果加入121229行;超过100000 = MAX(nrow(x)中,nrow(I))。检查i中的重复键值 它一遍又一遍地加入x中的同一组。如果那没关系, 尝试包括
j
并删除by
(by-without-by)以便j运行 每组避免大量分配。如果你确定你想 继续,用allow.cartesian = TRUE重新运行。否则,请搜索 FAQ,Wiki,Stack Overflow和datatable-help中的此错误消息 建议。
当前问题是使用启用的data.table
选项,但是,由于它是相同的包,我还要感谢下一个选项的建议,该选项使用替代< / strong> data.table
语法用于合并(即使我觉得它太混乱了,但为了知识的完整性)。提前谢谢!
答案 0 :(得分:16)
我会以这种方式处理这个问题:
首先,有一条错误消息。这是什么意思?
将结果加入121229行;超过100000 = max(nrow(x),nrow(i))。检查i中的重复键值,每个键值一遍又一遍地连接到x中的同一组。如果没关系,请尝试包含j并按(逐个)删除,以便为每个组运行j以避免大量分配。如果您确定要继续,请使用allow.cartesian = TRUE重新运行。否则,请在FAQ,Wiki,Stack Overflow和datatable-help中搜索此错误消息以获取建议。
大!但是我正在使用这么多的数据集,以及很多软件包和很多功能。我必须将其缩小到哪个数据集产生此错误。
ans1 = merge(as.data.table(dataSets[[1]]), as.data.table(dataSets[[2]]),
all.x=TRUE, all.y=FALSE, by="Project ID")
## works fine.
ans2 = merge(as.data.table(dataSets[[1]]), as.data.table(dataSets[[3]]),
all.x=TRUE, all.y=FALSE, by="Project ID")
## same error
啊哈,得到了同样的错误。
因此,dataSets[[3]]
似乎发生了一些事情。它表示要检查i
中的重复键值。我们这样做:
dim(dataSets[[3]])
# [1] 81487 3
dim(unique(as.data.table(dataSets[[3]]), by="Project ID"))
# [1] 49999 3
因此,dataSets[[3]]
具有重复的“项目ID”值,因此对于每个重复的值,将返回dataSets[[1]]
中所有匹配的行 - 这是第2行的第2部分解释的内容: each of which join to the same group in x over and over again
。
allow.cartesian=TRUE
:我知道有重复的密钥仍然希望继续。但错误消息提到我们如何继续,添加“allow.cartesian = TRUE”。
ans2 = merge(as.data.table(dataSets[[1]]), as.data.table(dataSets[[3]]),
all.x=TRUE, all.y=FALSE, by="Project ID", allow.cartesian=TRUE)
啊,啊哈,现在工作正常!那么allow.cartesian = TRUE
做了什么?或者为什么要添加?错误消息表示在stackoverflow上搜索消息(在其他事情中)。
allow.cartesian=TRUE
:搜索让我了解了这个Why is allow.cartesian required at times when when joining data.tables with duplicate keys?问题,该问题解释了目的,并在评论中还包含了来自@Roland的另一个链接:Merging data.tables uses more than 10 GB RAM指出了最初的问题一切都开始了。让我现在阅读这些帖子。
base::merge
会给出不同的结果吗?现在,base :: merge是否返回不同的结果(100,000行)?
dim(merge(dataSets[[1]], dataSets[[3]], all.x=TRUE, all.y=FALSE, by="Project ID"))
# [1] 121229 4
不是真的。它提供与使用data.table
时相同的维度,但它并不关心是否存在重复的键,而data.table
会警告您合并结果的潜在爆炸并允许您做出明智的决定