/
ddply.r
57 lines (56 loc) · 1.93 KB
/
ddply.r
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#' Split data frame, apply function, and return results in a data frame.
#'
#' For each subset of a data frame, apply function then combine results into a
#' data frame.
#'
#' @template ply
#' @template d-
#' @template -d
#' @seealso \code{\link{tapply}} for similar functionality in the base package
#' @export
#' @examples
#' # Summarize a dataset by two variables
#' require(plyr)
#' group <- c(rep('A', 8), rep('B', 15), rep('C', 6))
#' sex <- sample(c("M", "F"), size = 29, replace = TRUE)
#' age <- runif(n = 29, min = 18, max = 54)
#' dfx <- data.frame (group, time, age)
#'
#' # Note the use of the '.' function to allow
#' # group and sex to be used without quoting
#' ddply(dfx, .(group, sex), summarize,
#' mean <- round(mean(age), 2),
#' sd <- round(sd(age), 2))
#'
#' # group sex mean sd
#' # 1 A F 35.89 8.53
#' # 2 A M 38.01 15.09
#' # 3 B F 39.08 10.79
#' # 4 B M 37.38 9.55
#' # 5 C F 30.01 NA
#' # 6 C M 34.78 11.50
#'
#' # An example using a formula for .variables
#' ddply(baseball[1:100,], .variables = ~year, .fun=nrow)
#' # Applying two functions; nrow and ncol
#' ddply(baseball, .(lg), c("nrow", "mean"))
#'
#' # Calculate mean runs batted in for each year
#' rbi <- ddply(baseball, .(year), summarise,
#' mean_rbi = mean(rbi, na.rm = TRUE))
#' # Plot a line chart of the result
#' plot(mean_rbi ~year, type = "l", data = rbi))
#'
#' # make new variable career_year based on the
#' # start year for each player (id)
#' base2 <- ddply(baseball, .(id), transform,
#' career_year = year - min(year) + 1
#' )
ddply <- function(.data, .variables, .fun = NULL, ..., .progress = "none",
.drop = TRUE, .parallel = FALSE, .paropts = NULL) {
if (empty(.data)) return(.data)
.variables <- as.quoted(.variables)
pieces <- splitter_d(.data, .variables, drop = .drop)
ldply(.data = pieces, .fun = .fun, ...,
.progress = .progress, .parallel = .parallel, .paropts = .paropts)
}