Created
March 2, 2010 20:01
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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