<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="http://www.r-bloggers.com/scripting-loops-in-r/"></div></div><div><a data-url="http://www.r-bloggers.com/scripting-loops-in-r/" data-counturl="http://www.r-bloggers.com/scripting-loops-in-r/" data-count="horizontal" data-text="Scripting Loops In R" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://r-exercises.com/2016/06/01/scripting-loops-in-r/"> R-exercises</a></strong>, and kindly contributed to <a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><a target="_blank" href="http://i1.wp.com/r-exercises.com/wp-content/uploads/2016/06/450px-Invertigoloop.jpg"><img alt="450px-Invertigoloop" class="wp-image-858" src="http://i1.wp.com/r-exercises.com/wp-content/uploads/2016/06/450px-Invertigoloop-225x300.jpg?resize=170%2C220" data-recalc-dims="1"></a>An R programmer can determine the order of processing of commands, via use of the control statements; <code>repeat{}</code>, <code>while()</code>, <code>for()</code>, <code>break</code>, and <code>next</code></p>
<p>Answers to the exercises are available <a target="_blank" href="http://r-exercises.com/2016/May/26/scripting-loops-in-R-solutions/">here</a>.</p>
<p><strong>Exercise 1</strong><br>
The <code>repeat{}</code> loop processes a block of code until the condition specified by the <code>break</code> statement, (that is mandatory within the <code>repeat{}</code> loop), is met.</p>
<p>The structure of a <code>repeat{}</code> loop is:<br>
<code>repeat {<br>
  commands<br>
  if(condition) {<br>
    break<br>
  }<br>
}</code></p>
<p>For the first exercise, write a <code>repeat{}</code> loop that prints all the even numbers from <code>2</code> – <code>10</code>, via incrementing the variable, “<code>i &lt;- 0</code>“.</p>
<p><strong>Exercise 2</strong><br>
Using the following variables:</p>
<p><code>msg &lt;- c(&quot;Hello&quot;)</code><br>
<code>i &lt;- 1</code></p>
<p>Write a <code>repeat{}</code> loop that breaks off the incrementation of, “<code>i</code>“, after <code>5</code> loops, and prints “<code>msg</code>” at every increment.</p>
<p><strong>Exercise 3</strong><br>
<code>while()</code> loop will repeat a group of commands until the condition ceases to apply. The structure of a <code>while()</code> loop is:</p>
<p><code>while(condition) {<br>
  commands<br>
  }</code></p>
<p>With, <code>i &lt;- 1</code>, write a <code>while()</code> loop that prints the odd numbers from <code>1</code> through <code>7</code>.</p>
<p><strong>Exercise 4</strong><br>
Using the following variables:</p>
<p><code>msg &lt;- c(&quot;Hello&quot;)</code><br>
<code>i &lt;- 1</code></p>
<p>Write a <code>while()</code> loop that increments the variable, “<code>i</code>“, <code>6</code> times, and prints “<code>msg</code>” at every iteration.</p>
<p><strong>Exercise 5</strong><br>
The <code>for()</code> loop repeats commands until the specified length of the condition is met. The structure of a <code>for()</code> loop is:<br>
<code>for(condition) { commands }</code> </p>
<p>For example:<br>
<code>for(i in 1:4) {<br>
  print(&quot;variable&quot;[i])<br>
}</code></p>
<p><code>for(i in seq(&quot;variable&quot;)) {<br>
  print(i)<br>
}</code></p>
<p><code>for(i in seq_along(&quot;variable&quot;)) {<br>
  print(&quot;variable&quot;[i])<br>
  }</code></p>
<p><code>for(letter in &quot;variable&quot;) {<br>
  print(letter)<br>
  }</code></p>
<p>For this exercise, write a <code>for()</code> loop that prints the first four numbers of this sequence: <code>x &lt;- c(7, 4, 3, 8, 9, 25)</code></p>
<p><strong>Exercise 6</strong><br>
For the next exercise, write a <code>for()</code> loop that prints all the letters in <code>y &lt;- c(&quot;q&quot;, &quot;w&quot;, &quot;e&quot;, &quot;r&quot;, &quot;z&quot;, &quot;c&quot;).</code></p>
<p><strong>Exercise 7</strong><br>
The <code>break</code> statement is used within loops to exit from the loop. If the <code>break</code> statement is within a nested loop, the inner loop is exited, and the outer loop is resumed.</p>
<p>Using <code>i &lt;- 1</code>, write a <code>while()</code> loop that prints the variable, “<code>i</code>“, (that is incremented from <code>1</code> – <code>5</code>), and uses <code>break</code> to exit the loop if “<code>i</code>” equals <code>3</code>.</p>
<p><strong>Exercise 8</strong><br>
Write a nested loop, where the outer <code>for()</code> loop increments “<code>a</code>” <code>3</code> times, and the inner <code>for()</code> loop increments “<code>b</code>” <code>3</code> times. The <code>break</code> statement exits the inner <code>for()</code> loop after <code>2</code> incrementations. The nested loop prints the values of variables, “<code>a</code>” and “<code>b</code>“.</p>
<p><strong>Exercise 9</strong><br>
The <code>next</code> statement is used within loops in order to skip the current evaluation, and instead proceed to the next evaluation.</p>
<p>Therefore, write a <code>while()</code> loop that prints the variable, “<code>i</code>“, that is incremented from <code>2</code> – <code>5</code>, and uses the <code>next</code> statement, to skip the printing of the number <code>3</code>.</p>
<p><strong>Exercise 10</strong><br>
Finally, write a <code>for()</code> loop that uses next to print all values except “<code>3</code>” in the following variable: <code>i &lt;- 1:5</code></p>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="http://www.r-bloggers.com/scripting-loops-in-r/"></div></div><div><a data-url="http://www.r-bloggers.com/scripting-loops-in-r/" data-counturl="http://www.r-bloggers.com/scripting-loops-in-r/" data-count="vertical" data-text="Scripting Loops In R" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://r-exercises.com/2016/06/01/scripting-loops-in-r/"> R-exercises</a></strong>.</div>
<hr>
<a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="http://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="http://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="http://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="http://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="http://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="http://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="http://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="http://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="http://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="http://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="http://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="http://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="http://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="http://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="http://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="http://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/A0m3wDM_4Ps"></a></div>