Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Generally, deal better when working with function names

  • Loading branch information...
commit 63030c266e5a2c0a562071dad92f727aa4145295 1 parent 13b8ae0
Hadley Wickham authored
2  NEWS
View
@@ -2,9 +2,11 @@ plyr 0.1.6 (2008-XX-XX) ---------------------------------------------------
* use list names where present
* failwith now accepts a function name, as well as a function itself
+* each now accepts function names, as well as a functions
* fix typo that was causing a 50% speed penalty for d*ply
* aaply now works with vectors
* fix bug in daply which prevented it from working correctly when data frame was split by multiple variables
+* all ply functions deal more elegantly when given function names: can supply a vector of function names, and name is used as label in output
plyr 0.1.5 (2008-02-23) ---------------------------------------------------
5 R/helper-each.r
View
@@ -10,6 +10,11 @@
each <- function(...) {
fnames <- laply(match.call()[-1], deparse)
fs <- list(...)
+
+ # Find function names and replace with function objects
+ char <- laply(fs, is.character)
+ fnames[char] <- fs[char]
+ fs[char] <- llply(fs[char], match.fun)
unames <- names(fs)
if (is.null(unames)) unames <- fnames
2  R/ply-array.r
View
@@ -30,7 +30,7 @@
#X laply(seq_len(10), rep, times = 4)
#X laply(seq_len(10), matrix, nrow = 2, ncol = 2)
laply <- function(.data, .fun = NULL, ..., .progress = "none", .drop = TRUE) {
- if (is.character(.fun)) .fun <- match.fun(.fun)
+ if (is.character(.fun)) .fun <- do.call("each", as.list(.fun))
if (!is.function(.fun)) stop(".fun is not a function.")
if (!inherits(.data, "split")) .data <- as.list(.data)
6 R/ply-data-frame.r
View
@@ -24,7 +24,6 @@
ldply <- function(.data, .fun = NULL, ..., .progress = "none") {
if (!inherits(.data, "split")) .data <- as.list(.data)
res <- llply(.data = .data, .fun = .fun, ..., .progress = .progress)
-
list_to_dataframe(res, attr(.data, "split_labels"))
}
@@ -53,9 +52,14 @@ ldply <- function(.data, .fun = NULL, ..., .progress = "none") {
# @arguments other arguments passed on to \code{.fun}
# @arguments name of the progress bar to use, see \code{\link{create_progress_bar}}
# @value a data frame
+#X
+#X ddply(baseball, .(year), "nrow")
+#X ddply(baseball, .(lg), c("nrow", "ncol"))
+#X
#X mean_rbi <- function(df) mean(df$rbi, na.rm=TRUE)
#X rbi <- ddply(baseball, .(year), mean_rbi)
#X with(rbi, plot(year, V1, type="l"))
+#X rbi <- ddply(baseball, .(year), "mean_rbi")
#X
#X mean_rbi <- function(rbi, ...) mean(rbi, na.rm=TRUE)
#X rbi <- ddply(baseball, .(year), splat(mean_rbi))
2  R/ply-list.r
View
@@ -31,7 +31,7 @@ llply <- function(.data, .fun = NULL, ..., .progress = "none") {
n <- length(pieces)
if (n == 0) return(list())
- if (is.character(.fun)) .fun <- match.fun(.fun)
+ if (is.character(.fun)) .fun <- do.call("each", as.list(.fun))
if (!is.function(.fun)) stop(".fun is not a function.")
progress <- create_progress_bar(.progress)
2  R/ply-null.r
View
@@ -15,7 +15,7 @@
# @arguments name of the progress bar to use, see \code{\link{create_progress_bar}}
# @argument automatically print each result? (default: \code{FALSE})
l_ply <- function(.data, .fun = NULL, ..., .progress = "none", .print = FALSE) {
- if (is.character(.fun)) .fun <- match.fun(.fun)
+ if (is.character(.fun)) .fun <- do.call("each", as.list(.fun))
if (!is.function(.fun)) stop(".fun is not a function.")
progress <- create_progress_bar(.progress)
2  R/simplify.r
View
@@ -13,7 +13,7 @@ list_to_dataframe <- function(res, labels = NULL) {
if (length(ulength) != 1) stop("Results are not equal lengths")
if (length(res) > 1) {
- resdf <- as.data.frame(do.call("rbind", res))
+ resdf <- as.data.frame(do.call("rbind", res))
} else {
resdf <- data.frame(res[[1]])
}
Please sign in to comment.
Something went wrong with that request. Please try again.