The question title may betray some misconception which you're welcome to point out. Nonetheless, there is something mysterious about some of the plyr
functions. For example, look at the laply
function, which is scripted as:
function (.data, .fun = NULL, ..., .progress = "none", .inform = FALSE,
.drop = TRUE, .parallel = FALSE, .paropts = NULL)
{
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)
res <- llply(.data = .data, .fun = .fun, ..., .progress = .progress,
.inform = .inform, .parallel = .parallel, .paropts = .paropts)
list_to_array(res, attr(.data, "split_labels"), .drop)
}
<environment: namespace:plyr>
The last call, which formats the result, uses a function list_to_array
. That function is not defined inside laply
and neither does it exist outside (check exists("list_to_array")
). Yet laply
works (of course - and I have used it personally)! How is this possible?
I would like to know how laply
combines list elements to an array so I can know how efficient it is. There are alternative ways, for example do.call(rbind, res)
(in the 2D case). Knowing how laply
does this would help me make an informed decision.
Other examples include (non-exhaustively): splitter_a
within aaply
and alply
, splitter_d
within ddply
and list_to_dataframe
within ldply
. By contrast, simplify2array
in sapply
is explicitly defined outside sapply
, and is occasionally useful on its own.