The debate regarding the use of
forloops versus the
apply()function family (
vapply(), etc., along with their purrr counterparts:
map_chr(), etc.), has been a longstanding one in the R community.
While you may occasionally hear that
forloops are slower, this notion has already been debunked in other posts. When utilized correctly, a
forloop can achieve performance on par with
However, there are still lesser-known reasons to prefer
forloops, which we will explore in this post.
Read on for an important caveat, and then several reasons to prefer
apply() (or purrr’s counterparts). H/T R-Bloggers.