You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In the R for Reproducible Scientific Analysis when discussing for loops or vectorization it might be important to let people know about the apply family of functions. For loops are generally slow in R and it might be handy to at least acknowledge the apply family of functions.
The text was updated successfully, but these errors were encountered:
Apply functions do not vectorize. They are for-loops, sometimes called from C (lapply() and sapply()), in the case of apply, it's straight within R. From the source code of apply():
if (length(d.call) <2L) {
if (length(dn.call))
dimnames(newX) <- c(dn.call, list(NULL))
for (iin1L:d2) {
tmp<- forceAndCall(1, FUN, newX[, i], ...)
if (!is.null(tmp))
ans[[i]] <-tmp
}
}
elsefor (iin1L:d2) {
tmp<- forceAndCall(1, FUN, array(newX[, i], d.call,
dn.call), ...)
if (!is.null(tmp))
ans[[i]] <-tmp
}
The crucial bit is that they all are evaluating R code in the loop body, which is not really what is commonly meant by vectorization within R. Patrick Burns discusses this in his R Inferno, circle 4.
In the R for Reproducible Scientific Analysis when discussing for loops or vectorization it might be important to let people know about the apply family of functions. For loops are generally slow in R and it might be handy to at least acknowledge the apply family of functions.
The text was updated successfully, but these errors were encountered: