From 06d65d51c055dc1197c5284147e2508384bd6bc2 Mon Sep 17 00:00:00 2001 From: Jim Hester Date: Thu, 26 Jul 2012 09:55:16 -0400 Subject: [PATCH 1/2] Summarise sequentially Summarise will now work sequentially, so you can use earlier summaries in later summaries --- R/helper-summarise.r | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/R/helper-summarise.r b/R/helper-summarise.r index f3c2eb04..4093723e 100644 --- a/R/helper-summarise.r +++ b/R/helper-summarise.r @@ -18,8 +18,7 @@ #' duration = max(year) - min(year), #' nteams = length(unique(team))) summarise <- function(.data, ...) { - cols <- eval(substitute(list(...)), .data, parent.frame()) - + cols <- as.list(substitute(list(...))[-1]) # ... not a named list, figure out names by deparsing call if(is.null(names(cols))) { missing_names <- rep(TRUE, length(cols)) @@ -30,7 +29,15 @@ summarise <- function(.data, ...) { names <- unname(unlist(lapply(match.call(expand = FALSE)$`...`, deparse))) names(cols)[missing_names] <- names[missing_names] } - - quickdf(cols) + cols <- cols[names(cols) != ""] + env <- new.env(parent=parent.frame()) + for(name in names(.data)){ + assign(name,.data[[name]],envir=env) + } + ret <- list() + for (col in names(cols)) { + ret[[col]] <- eval(cols[[col]], ret, env) + } + quickdf(ret) } summarize <- summarise From e51fc374d2cf6ab472d8caa8d4238f5486ffb0b4 Mon Sep 17 00:00:00 2001 From: Jim Hester Date: Wed, 8 Aug 2012 11:25:49 -0400 Subject: [PATCH 2/2] Convert data frame to list Rather than copying the data frame to the environment, convert the data frame to a list, so that the new values can be added to it. --- R/helper-summarise.r | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/R/helper-summarise.r b/R/helper-summarise.r index 4093723e..476c9dfc 100644 --- a/R/helper-summarise.r +++ b/R/helper-summarise.r @@ -19,6 +19,7 @@ #' nteams = length(unique(team))) summarise <- function(.data, ...) { cols <- as.list(substitute(list(...))[-1]) + # ... not a named list, figure out names by deparsing call if(is.null(names(cols))) { missing_names <- rep(TRUE, length(cols)) @@ -29,15 +30,10 @@ summarise <- function(.data, ...) { names <- unname(unlist(lapply(match.call(expand = FALSE)$`...`, deparse))) names(cols)[missing_names] <- names[missing_names] } - cols <- cols[names(cols) != ""] - env <- new.env(parent=parent.frame()) - for(name in names(.data)){ - assign(name,.data[[name]],envir=env) - } - ret <- list() + .data <- as.list(.data) for (col in names(cols)) { - ret[[col]] <- eval(cols[[col]], ret, env) + .data[[col]] <- eval(cols[[col]], .data, parent.frame()) } - quickdf(ret) + quickdf(.data[names(cols)]) } summarize <- summarise