Sorry, that I don't made it clear enough, last time. I had been thinking about a redesign of your original code, to make it faster or in other words find out why your loop performance is so bad...?
The fact is that removing loops will almost always run your code faster and may more simple. Wherever you find yourself writing a for loop in R, stop and try to rethink. If you really do need to use a loop, try and keep as much outside of it as possible, e.g. the easy stuff, build your empty objects (vectors, matrix etc.) beforehand and rather than growing it with each iteration, assign an empty object of the correct size in the beginning and fill it up using fast subscripting. Just using an apply-function does not necessarily improve your performance, but good planned functionals are quite effective. You are dealing with a quite special case, or like Hugh mentioned it before 'you want the side effect' of the loop. There are performance technics in R everyone can use and learn (me too )... Below you will find some interesting links, try it out!
- http://adv-r.had.co.nz/Performance.html#language-performance
- http://adv-r.had.co.nz/Functionals.html
- http://adv-r.had.co.nz/Functional-programming.html
- http://adv-r.had.co.nz/memory.html#modification
Best regards
Adam