<!DOCTYPE HTML>
<html lang="en-US">
    <head prefix="og: http://ogp.me/ns# book: http://ogp.me/ns/book#">
        
        <meta charset="UTF-8">
        <title>Control Structures | Introduction to R</title>

        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="robots" content="index, follow">
        <meta name="author" content="idocs">
        <meta name="description" content="These are notes for an introductory R workshop I am teaching for Python Programmers.">
        <meta name="keywords" content="gitbook,github" >
        <meta name="generator" content="www.gitbook.io">

        
        <link rel="next" href="../learn/functions.html" />
        
        
        <link rel="prev" href="../learn/subsetting.html" />
        

        <meta property="og:title" content="Control Structures | Introduction to R">
        <meta property="og:site_name" content="Introduction to R">
        <meta property="og:type" content="book">
        <meta property="og:locale" content="en_US">

        <meta property="book:author" content="https://github.com/idocs">
        <meta property="book:tag" content="GitBook">

        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">

        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

        
        <link rel="stylesheet" href="../gitbook/style.css">
        
        
    </head>
    <body>
        
<div class="book" data-github="idocs/test1" data-level="1.3" data-basepath=".." data-revision="1397510917583">
    <div class="book-header">
    <!-- Actions Left -->
    <a href="https://github.com/idocs/test1" target="_blank" class="btn pull-left"><i class="fa fa-github-alt"></i></a>
    <a href="#" class="btn pull-left toggle-summary"><i class="fa fa-align-justify"></i></a>
    <a href="#" class="btn pull-left toggle-search"><i class="fa fa-search"></i></a>

    <!-- Actions Right -->
    <a href="#" target="_blank" class="btn pull-right" data-sharing="google-plus"><i class="fa fa-google-plus"></i></a>
    <a href="#" target="_blank" class="btn pull-right" data-sharing="facebook"><i class="fa fa-facebook"></i></a>
    <a href="#" target="_blank" class="btn pull-right" data-sharing="twitter"><i class="fa fa-twitter"></i></a>

    <a href="https://github.com/idocs/test1/stargazers" target="_blank" class="btn pull-right count-star"><i class="fa fa-star-o"></i> Star (<span>-</span>)</a>
    <a href="https://github.com/idocs/test1/watchers" target="_blank" class="btn pull-right count-watch"><i class="fa fa-eye"></i> Watch (<span>-</span>)</a>


    <!-- Title -->
    <h1><a href="../" >Introduction to R</a></h1>
</div>

    <div class="book-summary">
    <div class="book-search">
        <input type="text" placeholder="Search" class="form-control" />
    </div>
    <ul class="summary">
        <li>
            <a href="https://github.com/idocs" target="blank">About the author</a>
        </li>
        <li>
            <a href="https://github.com/idocs/test1/issues" target="blank">Questions and Issues</a>
        </li>
        <li>
            <a href="https://github.com/idocs/test1/edit/master/learn/controls.md" target="blank">Edit and Contribute</a>
        </li>
        <li class="divider"></li>
        <li data-level="0" data-path="index.html">
            <a href="../"><i class="fa fa-check"></i> Introduction</a>
        </li>
        
            <li  data-level="1" data-path="learn/README.html">
                
                <a href="../learn/README.html">
                    <i class="fa fa-check"></i> <b>1.</b> Introduction to R
                </a>
                
                
                <ul class="articles">
                    
                        <li  data-level="1.1" data-path="learn/structures.html">
                            
                            <a href="../learn/structures.html">
                                <i class="fa fa-check"></i> <b>1.1.</b> Data Structures
                            </a>
                            
                        </li>
                    
                        <li  data-level="1.2" data-path="learn/subsetting.html">
                            
                            <a href="../learn/subsetting.html">
                                <i class="fa fa-check"></i> <b>1.2.</b> Subsetting
                            </a>
                            
                        </li>
                    
                        <li  data-level="1.3" data-path="learn/controls.html">
                            
                            <a href="../learn/controls.html">
                                <i class="fa fa-check"></i> <b>1.3.</b> Control Structures
                            </a>
                            
                        </li>
                    
                        <li  data-level="1.4" data-path="learn/functions.html">
                            
                            <a href="../learn/functions.html">
                                <i class="fa fa-check"></i> <b>1.4.</b> Functions
                            </a>
                            
                        </li>
                    
                        <li  data-level="1.5" data-path="learn/vectorization.html">
                            
                            <a href="../learn/vectorization.html">
                                <i class="fa fa-check"></i> <b>1.5.</b> Vectorization
                            </a>
                            
                        </li>
                    
                </ul>
                
            </li>
        
            <li  data-level="2" data-path="explore/README.html">
                
                <a href="../explore/README.html">
                    <i class="fa fa-check"></i> <b>2.</b> Data Wrangling
                </a>
                
                
                <ul class="articles">
                    
                        <li  data-level="2.1" data-path="explore/tidy.html">
                            
                            <a href="../explore/tidy.html">
                                <i class="fa fa-check"></i> <b>2.1.</b> Tidy Data
                            </a>
                            
                        </li>
                    
                        <li  data-level="2.2" data-path="explore/reshape.html">
                            
                            <a href="../explore/reshape.html">
                                <i class="fa fa-check"></i> <b>2.2.</b> Reshape
                            </a>
                            
                        </li>
                    
                        <li  data-level="2.3" data-path="explore/summarize.html">
                            
                            <a href="../explore/summarize.html">
                                <i class="fa fa-check"></i> <b>2.3.</b> Summarize
                            </a>
                            
                        </li>
                    
                        <li  data-level="2.4" data-path="explore/sac.html">
                            
                            <a href="../explore/sac.html">
                                <i class="fa fa-check"></i> <b>2.4.</b> Split Apply Combine
                            </a>
                            
                        </li>
                    
                        <li  data-level="2.5" data-path="explore/sac2.html">
                            
                            <a href="../explore/sac2.html">
                                <i class="fa fa-check"></i> <b>2.5.</b> Split Apply Combine 2
                            </a>
                            
                        </li>
                    
                </ul>
                
            </li>
        
            <li  data-level="3" data-path="visualize/README.html">
                
                <a href="../visualize/README.html">
                    <i class="fa fa-check"></i> <b>3.</b> Data Visualization
                </a>
                
                
                <ul class="articles">
                    
                        <li  data-level="3.1" data-path="visualize/base_graphics.html">
                            
                            <a href="../visualize/base_graphics.html">
                                <i class="fa fa-check"></i> <b>3.1.</b> Base Graphics
                            </a>
                            
                        </li>
                    
                        <li  data-level="3.2" data-path="visualize/ggplot2.html">
                            
                            <a href="../visualize/ggplot2.html">
                                <i class="fa fa-check"></i> <b>3.2.</b> Grammar of Graphics
                            </a>
                            
                        </li>
                    
                        <li  data-level="3.3" data-path="visualize/interactive.html">
                            
                            <a href="../visualize/interactive.html">
                                <i class="fa fa-check"></i> <b>3.3.</b> Interactive Graphics
                            </a>
                            
                        </li>
                    
                </ul>
                
            </li>
        
    </ul>
</div>

    <div class="book-body" tabindex="-1">
        <div class="body-inner">
            <div class="page-wrapper">
                <div class="book-progress">
    <div class="bar">
        <div class="inner" style="width: 25%;min-width: 18.75%;"></div>
    </div>
    <div class="chapters">
    
        <a href="../index.html" title="Introduction" class="chapter done new-chapter" data-progress="0" style="left: 0%;"></a>
    
        <a href="../learn/README.html" title="Introduction to R" class="chapter done new-chapter" data-progress="1" style="left: 6.25%;"></a>
    
        <a href="../learn/structures.html" title="Data Structures" class="chapter done " data-progress="1.1" style="left: 12.5%;"></a>
    
        <a href="../learn/subsetting.html" title="Subsetting" class="chapter done " data-progress="1.2" style="left: 18.75%;"></a>
    
        <a href="../learn/controls.html" title="Control Structures" class="chapter done " data-progress="1.3" style="left: 25%;"></a>
    
        <a href="../learn/functions.html" title="Functions" class="chapter  " data-progress="1.4" style="left: 31.25%;"></a>
    
        <a href="../learn/vectorization.html" title="Vectorization" class="chapter  " data-progress="1.5" style="left: 37.5%;"></a>
    
        <a href="../explore/README.html" title="Data Wrangling" class="chapter  new-chapter" data-progress="2" style="left: 43.75%;"></a>
    
        <a href="../explore/tidy.html" title="Tidy Data" class="chapter  " data-progress="2.1" style="left: 50%;"></a>
    
        <a href="../explore/reshape.html" title="Reshape" class="chapter  " data-progress="2.2" style="left: 56.25%;"></a>
    
        <a href="../explore/summarize.html" title="Summarize" class="chapter  " data-progress="2.3" style="left: 62.5%;"></a>
    
        <a href="../explore/sac.html" title="Split Apply Combine" class="chapter  " data-progress="2.4" style="left: 68.75%;"></a>
    
        <a href="../explore/sac2.html" title="Split Apply Combine 2" class="chapter  " data-progress="2.5" style="left: 75%;"></a>
    
        <a href="../visualize/README.html" title="Data Visualization" class="chapter  new-chapter" data-progress="3" style="left: 81.25%;"></a>
    
        <a href="../visualize/base_graphics.html" title="Base Graphics" class="chapter  " data-progress="3.1" style="left: 87.5%;"></a>
    
        <a href="../visualize/ggplot2.html" title="Grammar of Graphics" class="chapter  " data-progress="3.2" style="left: 93.75%;"></a>
    
        <a href="../visualize/interactive.html" title="Interactive Graphics" class="chapter  " data-progress="3.3" style="left: 100%;"></a>
    
    </div>
</div>

                <div class="page-inner">
                
                    <section class="normal" id="section-gitbook_9">
                    
                        <h1 id="control-structures">Control Structures</h1>
<p>These allow you to control the flow of execution of a script typically inside of a function.
Common ones include:</p>
<ul>
<li>if, else</li>
<li>for</li>
<li>while</li>
<li>repeat</li>
<li>break</li>
<li>next</li>
<li>return</li>
</ul>
<p>We don&#39;t use these while working with R interactively but rather inside functions. </p>
<h2 id="if">If</h2>
<pre><code class="lang-r"><span class="hljs-keyword">if</span> (condition) {
    <span class="hljs-comment"># do something</span>
} <span class="hljs-keyword">else</span> {
    <span class="hljs-comment"># do something else</span>
}
</code></pre>
<p>e.g. </p>
<pre><code class="lang-r">x &lt;- <span class="hljs-number">1</span>:<span class="hljs-number">15</span>
<span class="hljs-keyword">if</span> (sample(x, <span class="hljs-number">1</span>) &lt;= <span class="hljs-number">10</span>) {
    print(<span class="hljs-string">"x is less than 10"</span>)
} <span class="hljs-keyword">else</span> {
    print(<span class="hljs-string">"x is greater than 10"</span>)
}
</code></pre>
<p><strong>Vectorization with ifelse</strong></p>
<pre><code class="lang-r">ifelse(x &lt;= <span class="hljs-number">10</span>, <span class="hljs-string">"x less than 10"</span>, <span class="hljs-string">"x greater than 10"</span>)
</code></pre>
<p>Other valid ways of writing if/else</p>
<pre><code class="lang-r"><span class="hljs-keyword">if</span> (sample(x, <span class="hljs-number">1</span>) &lt; <span class="hljs-number">10</span>) {
    y &lt;- <span class="hljs-number">5</span>
} <span class="hljs-keyword">else</span> {
    y &lt;- <span class="hljs-number">0</span>
}
</code></pre>
<pre><code class="lang-r">y &lt;- <span class="hljs-keyword">if</span> (sample(x, <span class="hljs-number">1</span>) &lt; <span class="hljs-number">10</span>) {
    <span class="hljs-number">5</span>
} <span class="hljs-keyword">else</span> {
    <span class="hljs-number">0</span>
}
</code></pre>
<h2 id="for">for</h2>
<p>A <code>for</code> loop works on an iterable variable and assigns successive values till the end of a sequence.</p>
<pre><code class="lang-r"><span class="hljs-keyword">for</span> (i <span class="hljs-keyword">in</span> <span class="hljs-number">1</span>:<span class="hljs-number">10</span>) {
    print(i)
}
</code></pre>
<pre><code class="lang-r">x &lt;- c(<span class="hljs-string">"apples"</span>, <span class="hljs-string">"oranges"</span>, <span class="hljs-string">"bananas"</span>, <span class="hljs-string">"strawberries"</span>)

<span class="hljs-keyword">for</span> (i <span class="hljs-keyword">in</span> x) {
    print(x[i])
}

<span class="hljs-keyword">for</span> (i <span class="hljs-keyword">in</span> <span class="hljs-number">1</span>:<span class="hljs-number">4</span>) {
    print(x[i])
}

<span class="hljs-keyword">for</span> (i <span class="hljs-keyword">in</span> seq(x)) {
    print(x[i])
}

<span class="hljs-keyword">for</span> (i <span class="hljs-keyword">in</span> <span class="hljs-number">1</span>:<span class="hljs-number">4</span>) print(x[i])
</code></pre>
<h2 id="nested-loops">Nested loops</h2>
<pre><code class="lang-r">m &lt;- matrix(<span class="hljs-number">1</span>:<span class="hljs-number">10</span>, <span class="hljs-number">2</span>)
<span class="hljs-keyword">for</span> (i <span class="hljs-keyword">in</span> seq(nrow(m))) {
    <span class="hljs-keyword">for</span> (j <span class="hljs-keyword">in</span> seq(ncol(m))) {
        print(m[i, j])
    }
}
</code></pre>
<h2 id="while">While</h2>
<pre><code class="lang-r">i &lt;- <span class="hljs-number">1</span>
<span class="hljs-keyword">while</span> (i &lt; <span class="hljs-number">10</span>) {
    print(i)
    i &lt;- i + <span class="hljs-number">1</span>
}
</code></pre>
<p>Be sure there is a way to exit out of a <code>while</code> loop.</p>
<h2 id="repeat-and-break">Repeat and break</h2>
<pre><code class="lang-r"><span class="hljs-keyword">repeat</span> {
    <span class="hljs-comment"># simulations; generate some value have an expectation if within some range,</span>
    <span class="hljs-comment"># then exit the loop</span>
    <span class="hljs-keyword">if</span> ((value - expectation) &lt;= threshold) {
        <span class="hljs-keyword">break</span>
    }
}
</code></pre>
<h2 id="next">Next</h2>
<pre><code class="lang-r"><span class="hljs-keyword">for</span> (i <span class="hljs-keyword">in</span> <span class="hljs-number">1</span>:<span class="hljs-number">20</span>) {
    <span class="hljs-keyword">if</span> (i%%<span class="hljs-number">2</span> == <span class="hljs-number">1</span>) {
        <span class="hljs-keyword">next</span>
    } <span class="hljs-keyword">else</span> {
        print(i)
    }
}
</code></pre>
<p>This loop will only print even numbers and skip over odd numbers. Later we&#39;ll learn other functions that will help us avoid these types of slow control flows as much as possible (mostly the <code>while</code> and <code>for</code> loops).</p>

                    
                    </section>
                
                </div>
            </div>
        </div>

        
        <a href="../learn/subsetting.html" class="navigation navigation-prev"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../learn/functions.html" class="navigation navigation-next"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
        <script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/ace.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/mode-javascript.js"></script>
        <script src="../gitbook/jsrepl/jsrepl.js" id="jsrepl-script"></script>
        <script src="../gitbook/app.js"></script>
        
    </body>
</html>
