By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
SmartData Collective
  • Analytics
    AnalyticsShow More
    construction analytics
    5 Benefits of Analytics to Manage Commercial Construction
    5 Min Read
    benefits of data analytics for financial industry
    Fascinating Changes Data Analytics Brings to Finance
    7 Min Read
    analyzing big data for its quality and value
    Use this Strategic Approach to Maximize Your Data’s Value
    6 Min Read
    data-driven seo for product pages
    6 Tips for Using Data Analytics for Product Page SEO
    11 Min Read
    big data analytics in business
    5 Ways to Utilize Data Analytics to Grow Your Business
    6 Min Read
  • Big Data
  • BI
  • Exclusive
  • IT
  • Marketing
  • Software
Search
© 2008-23 SmartData Collective. All Rights Reserved.
Reading: Counting with iterators
Share
Notification Show More
Latest News
cloud-centric companies using network relocation
Cloud-Centric Companies Discover Benefits & Pitfalls of Network Relocation
Cloud Computing
construction analytics
5 Benefits of Analytics to Manage Commercial Construction
Analytics
database compliance guide
Four Strategies For Effective Database Compliance
Data Management
Digital Security From Weaponized AI
Fortifying Enterprise Digital Security Against Hackers Weaponizing AI
Security
DevOps on cloud
Optimizing Cost with DevOps on the Cloud
Cloud Computing Development Exclusive IT
Aa
SmartData Collective
Aa
Search
  • About
  • Help
  • Privacy
Follow US
© 2008-23 SmartData Collective. All Rights Reserved.
SmartData Collective > Uncategorized > Counting with iterators
Uncategorized

Counting with iterators

DavidMSmith
Last updated: 2009/07/17 at 12:04 PM
DavidMSmith
4 Min Read
SHARE
- Advertisement -

Ever wanted to do a loop in R over a million elements, but felt bad that for (i in 1e6) do.stuff(i) allocated an 8Mb vector of indices you didn’t actually need to store? That’s where iterators come in. Iterators are new to R (REvolution Computing just released the iterators package to CRAN last month), but will be familiar to programmers of languages like Java or Python. You can think of an iterator as something like a cursor or pointer to a predefined sequence of elements. Each time you access the iterator, it returns the current element being pointed to, and…

- Advertisement -

Ever wanted to do a loop in R over a million elements, but felt bad that

for (i in 1e6) do.stuff(i)

More Read

big data improves

3 Ways Big Data Improves Leadership Within Companies

IT Is Not Analytics. Here’s Why.
Romney Invokes Analytics in Rebuke of Trump
WEF Davos 2016: Top 100 CEO bloggers
In Memoriam: Robin Fray Carey
allocated an 8Mb vector of indices you didn't actually need to store? That's where iterators come in.

Iterators are new to R (REvolution Computing just released the iterators package to CRAN last month), but will be familiar to programmers of languages like Java or Python. You can think of an iterator as something like a cursor or pointer to a predefined sequence of elements. Each time you access the iterator, it returns the current element being pointed to, and advances to the next one. 

This is probably easier to explain with an example. We can create an iterator for a sequence of integers 1 to 5 with the icount function:

- Advertisement -
> require(iterators)
Loading required package: iterators
> i <- icount(5)

The function nextElem returns the current value of the iterator, and advances it to the next. Iterators created with icount always start at 1:

> nextElem(i)
[1] 1
> nextElem(i)
[1] 2
> nextElem(i)
[1] 3
 
When an iterator runs out of values to return, it signals an error:

> nextElem(i)
[1] 4
> nextElem(i)
[1] 5
> nextElem(i)
Error: StopIteration

So, if we wanted to make a loop of a million iterations, all we need to do is make an iterator and then loop using the foreach function (from the foreach package):

> require(foreach)

Loading required package: foreach
> m <- icount(1e6)
> foreach (i = m) %do% { do.stuff(i) }

- Advertisement -
One nice thing about this construction is that m is a very small object: you don't need to waste a bunch of RAM on index values you only need one at a time. The other nice thing is that by replacing %do% with %dopar% you can run multiple iterations in parallel. Because the iterator m is shared amongst all the parallel instances, it guarantees that i takes each value between one and a million exactly once across all the iterations, even if they don't necessarily complete in sequence.

An iterator isn't constrained to simply return integers, either. You can set up an iterator on a matrix, so that each call to nextElem returns the next row (or column) as a vector. Or, you can set up an iterator on a MySQL or Oracle database, so that each call to nextElem returns the next record in the table. Iterators can even return infinite, irregular sequences — the sequence of all primes, for examples. You can see examples of all these kinds of iterators in my recent UseR! talk. 

Link to original post

DavidMSmith July 17, 2009
Share this Article
Facebook Twitter Pinterest LinkedIn
Share
- Advertisement -

Follow us on Facebook

Latest News

cloud-centric companies using network relocation
Cloud-Centric Companies Discover Benefits & Pitfalls of Network Relocation
Cloud Computing
construction analytics
5 Benefits of Analytics to Manage Commercial Construction
Analytics
database compliance guide
Four Strategies For Effective Database Compliance
Data Management
Digital Security From Weaponized AI
Fortifying Enterprise Digital Security Against Hackers Weaponizing AI
Security

Stay Connected

1.2k Followers Like
33.7k Followers Follow
222 Followers Pin

You Might also Like

big data improves
Big DataJobsKnowledge ManagementUncategorized

3 Ways Big Data Improves Leadership Within Companies

6 Min Read
Image
Uncategorized

IT Is Not Analytics. Here’s Why.

7 Min Read

Romney Invokes Analytics in Rebuke of Trump

4 Min Read

WEF Davos 2016: Top 100 CEO bloggers

14 Min Read

SmartData Collective is one of the largest & trusted community covering technical content about Big Data, BI, Cloud, Analytics, Artificial Intelligence, IoT & more.

ai is improving the safety of cars
From Bolts to Bots: How AI Is Fortifying the Automotive Industry
Artificial Intelligence Chatbots Exclusive
giveaway chatbots
How To Get An Award Winning Giveaway Bot
Big Data Chatbots Exclusive

Quick Link

  • About
  • Contact
  • Privacy
Follow US

© 2008-23 SmartData Collective. All Rights Reserved.

Removed from reading list

Undo
Go to mobile version
Welcome Back!

Sign in to your account

Lost your password?