The following is an example of a reproducible example of a situation in which I am experiencing and stuck (this is a test client that I use to evaluate various approaches to merging data sets for my dissertation research).
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)
MERGE_OPTION <- "data.table"
loadData <- function (dataFile) {
if (file.exists(dataFile)) {
data <- readRDS(dataFile)
}
else {
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)
}
dataSets <- loadDataSets(tmpDir)
if (MERGE_OPTION == "lapply_merge") {
flossData <- data.frame(dataSets[[1]][1])
silent <- lapply(seq(2, length(dataSets)),
function(i) {merge(flossData, dataSets[[1]][i],
by = "Project ID",
all = TRUE)})
}
if (MERGE_OPTION == "lapply_merge2") {
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") {
flossData <- Reduce(function(...)
merge(..., by.x = "row.names", by.y = "Project ID", all = TRUE),
dataSets)
}
if (MERGE_OPTION == "reduce_merge2") {
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") {
if (!suppressMessages(require(reshape))) install.packages('reshape')
library(reshape)
flossData <- reshape::merge_all(dataSets)
}
if (MERGE_OPTION == "plyr") {
if (!suppressMessages(require(plyr))) install.packages('plyr')
library(plyr)
flossData <- plyr::join_all(dataSets)
}
if (MERGE_OPTION == "dplyr") {
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") {
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)
}
}
if (MERGE_OPTION == "data.table2") {
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)])
}
flossData[["Repo URL"]] <- as.integer(flossData[["Repo URL"]] != "")
flossData[["User Community Size"]] <-
as.integer(flossData[["User Community Size"]])
rowsNA <- apply(flossData, 1, function(x) {any(is.na(x))})
flossData <- flossData[!rowsNA,]
print(str(flossData))
Message as follows:
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 = (nrow (), nrow ()). i, x. , j by (by-without-by), j , . , , allow.cartesian = TRUE. , , FAQ, Wiki, Qaru datatable-help .
data.table, , , , > data.table ( , ). !