Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When discussing Vectorization or For Loops introduce apply functions #801

Open
victorfeagins opened this issue Sep 12, 2022 · 1 comment

Comments

@victorfeagins
Copy link

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.

@bschiffthaler
Copy link

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 (i in 1L:d2) {
          tmp <- forceAndCall(1, FUN, newX[, i], ...)
          if (!is.null(tmp))
              ans[[i]] <- tmp
      }
  }
  else for (i in 1L: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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants