Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should "while loops" be preferred to "for loops" for large, necessary loops in R?

Realizing that loops are usually not ideal in R, sometimes they are necessary.

When writing large loops, doesn't

for (i in 1:large_number) 

waste memory, since a vector of size large_number must be created?

Would this make while loops the best choice for large, necessary loops?

like image 586
Dan Goldstein Avatar asked Aug 06 '09 13:08

Dan Goldstein


1 Answers

First off, a lot of that 'loops are bad' chatter stems from the dark ages when loops where in fact less efficiently implemented, in particular in some versions of S-Plus.

That said, and while your comment about the need for a large index object is correct, you could also use

  • functions from the apply family such as sapply, lapply or tapply to unroll your structures

  • the relatively new iterators package which also avoids the large vector you mentioned as a memory constraint

  • the Ra 'accelerated R' variant and its jit package which can significantly accelerate simple loops.

As added bonus, options one and two give a path towards parallel execution of the loops on suitable systems using tools from the CRAN packages snow, multicore, or NWS just to name a few.

like image 184
Dirk Eddelbuettel Avatar answered Nov 08 '22 16:11

Dirk Eddelbuettel