Skip to content

Often overlooked reason why for-loops can be better than apply-functions

July 13, 2012

When learning R (or S) for the first time, it doesn’t take long to encounter the apply family of functions (e.g. apply, lapply, sapply, tapply, rapply). These apply-functions provide an alternative syntax to the standard for-loop. There are many many comparisons between the apply and for approach to loops in R. Usually the discussion is around speed or code readability.

One often overlooked benefit of for loops occurs when you have to interrupt a simulation with many iterations. With for-loops, the completed iterations will be saved — but for apply-functions, the completed iterations will be lost (unless you do something special).

Advertisements
No comments yet

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: