Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 36 lines (33 sloc) 1.22 kb
3ee18e5 @hadley Move functions into own files
authored
1 #' Split list, apply function, and return results in an array.
2 #'
3 #' For each element of a list, apply function then combine results into an
2d1d7cd @hadley Doc tweakings and file fixes
authored
4 #' array.
5 #'
6 #' \code{laply} is similar in spirit to \code{\link{sapply}} except
3ee18e5 @hadley Move functions into own files
authored
7 #' that it will always return an array, and the output is transposed with
a9430ff @hadley laply doc fix. Closes #108
authored
8 #' respect \code{sapply} - each element of the list corresponds to a row,
9 #' not a column.
3ee18e5 @hadley Move functions into own files
authored
10 #'
11 #' @template ply
12 #' @template l-
13 #' @template -a
14 #' @export
15 #' @examples
16 #' laply(baseball, is.factor)
17 #' # cf
18 #' ldply(baseball, is.factor)
19 #' colwise(is.factor)(baseball)
20 #'
21 #' laply(seq_len(10), identity)
22 #' laply(seq_len(10), rep, times = 4)
23 #' laply(seq_len(10), matrix, nrow = 2, ncol = 2)
b665d95 @hadley Add .inform argument to **ply.
authored
24 laply <- function(.data, .fun = NULL, ..., .progress = "none", .inform = FALSE,
25 .drop = TRUE, .parallel = FALSE, .paropts = NULL) {
3ee18e5 @hadley Move functions into own files
authored
26 if (is.character(.fun)) .fun <- do.call("each", as.list(.fun))
27 if (!is.function(.fun)) stop(".fun is not a function.")
28
29 if (!inherits(.data, "split")) .data <- as.list(.data)
30 res <- llply(.data = .data, .fun = .fun, ...,
b665d95 @hadley Add .inform argument to **ply.
authored
31 .progress = .progress, .inform = .inform,
32 .parallel = .parallel, .paropts = .paropts)
3ee18e5 @hadley Move functions into own files
authored
33
34 list_to_array(res, attr(.data, "split_labels"), .drop)
35 }
Something went wrong with that request. Please try again.