Functions creating iterators for efficient looping in R