hadley/plyr — Issue #62: Do R-style currying in colwise
Author: macrakis
Created: 2011-10-17 21:00:24+00:00
State: open

Suggest that colwise(f,...)(df) do R-style currying like sapply(df,f,...). This potentially needs to handle two sets of ... arguments as in colwise(f,...)(df,...)

The following code does this:

colwise <-
function (.fun, ..., .cols = true)
{
if (!is.function(.cols)) {
.cols <- as.quoted(.cols)
filter <- function(df) eval.quoted(.cols, df)
}
else {
filter <- function(df) Filter(.cols, df)
}
outerargs = list(...)
function(df, ...) {
stopifnot(is.data.frame(df))
filtered <- filter(df)
if (length(filtered) == 0)
return(data.frame())
df <- quickdf(do.call(lapply,c(list(filtered, .fun), outerargs,...)))
names(df) <- names(filtered)
df
}
}