Skip to content
Prev 361247 / 398503 Next

Dynamically populate a vector by iteratively applying a function to its previous element.

You have set yourself an impossible goal. Either you can reformulate your problem as non-iterative and can process your data as arrays, or you have to use some kind of for loop. The lapply and Vectorize functions are popular "pretty" ways to do this, but they amount to hidden for loops. 

Note that certain instances of iterative algorithms may have optimized compiled calculation functions ready for your use,  but unless someone has figured out some algorithm-specific optimization they won't be significantly faster than an R for loop. 

The best you can do is avoid allocating growing data structures... pre-allocate your result and fill it in as you go.