From 4615945ecf8837d5c9beddbbc4eabb3b8aba8838 Mon Sep 17 00:00:00 2001 From: Enrico Schumann Date: Tue, 10 Oct 2023 18:21:46 +0200 Subject: [PATCH] Create groups more efficiently --- R/functions.R | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/R/functions.R b/R/functions.R index 7ce5d9e..93dfd1b 100644 --- a/R/functions.R +++ b/R/functions.R @@ -71,7 +71,6 @@ fetch_data <- function(id, dats <- read.table(text = dats, sep = ";", header = TRUE, - quote = stringsAsFactors = FALSE, as.is = TRUE, skip = empty, ...) @@ -92,8 +91,9 @@ fetch_data <- function(id, dates, perl = TRUE))) dates <- as.Date(dates) if (length(other.col) > 1L) - groups <- apply(dats[, other.col], 1, paste0, - collapse = name.sep) + groups <- do.call(function(...) + paste(..., sep = name.sep), + dats[other.col]) else groups <- dats[, other.col] u.groups <- unique(groups) @@ -106,21 +106,13 @@ fetch_data <- function(id, for (g in u.groups) { tmp <- dats[g == groups, ] - ## no (documented) guarantee data are sorted + + ## no (documented) guarantee data are sorted, + ## so match dates i <- match(tmp$Date, dates) result[i, g] <- tmp[, "Value"] } - - ## id <- dats[[2]][seq(1, min(which(duplicated(dats[[2]]))) - 1)] - ## ans <- vector("list", length(id)) - ## names(ans) <- as.character(id) - ## for (i in as.character(id)) { - ## ans[[i]] <- dats[dats[, 2] == i, -2] - ## } - ## result <- do.call(cbind, lapply(ans, `[[`, 2)) - ## row.names(result) <- ans[[1]][[1]] - if (na.drop) { drop <- apply(result[, -1], 1, function(x) all(!is.finite(x))) -- 2.45.2