Hugo Gruson draws some comparisons:
The debate regarding the use of
for
loops versus theapply()
function family (apply()
,lapply()
,vapply()
, etc., along with their purrr counterparts:map()
,map2()
,map_lgl()
,map_chr()
, etc.), has been a longstanding one in the R community.While you may occasionally hear that
for
loops are slower, this notion has already been debunked in other posts. When utilized correctly, afor
loop can achieve performance on par withapply()
functions.However, there are still lesser-known reasons to prefer
apply()
functions overfor
loops, 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.