Skip to content

Instantly share code, notes, and snippets.

@HarlanH
Created March 2, 2010 20:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HarlanH/319851 to your computer and use it in GitHub Desktop.
Save HarlanH/319851 to your computer and use it in GitHub Desktop.
a simplified version of aaply() from the R plyr package, much faster for simple cases
df.by.rows <- function(df, fn, agg.type=numeric, .progress=NULL)
{
# A simplified and more-efficient version of aaply for data frames. Iterates over rows of the df, applies the
# fn to each single-row slice, and aggregates the **scalar** results.
#
# Args:
# df - data frame to split
# fn - function to apply to each row
# agg.type - a constructor, one of numeric, character
# .progress - set to "text" to get a progress bar
#
# Returns: a vector of type agg.type of length nrow(df)
######################
stopifnot(is.data.frame(df))
stopifnot(is.function(fn))
#stopifnot(agg.type %in% c(numeric, character))
df.rows <- nrow(df)
new.col <- agg.type(df.rows)
if (.progress == "text")
progbar <- txtProgressBar(min=1, max=df.rows, initial=1, style=3)
for (i in seq_len(df.rows)) {
new.col[[i]] <- fn(df[i, ])
setTxtProgressBar(progbar, i)
}
close(progbar)
new.col
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment