<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/introduction-to-r-for-data-science-session-4/"></div></div><div><a data-url="http://www.r-bloggers.com/introduction-to-r-for-data-science-session-4/" data-counturl="http://www.r-bloggers.com/introduction-to-r-for-data-science-session-4/" data-count="horizontal" data-text="Introduction to R for Data Science :: Session 4" 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://www.exactness.net/post/145045592605"> The Exactness of Mind</a></strong>, and kindly contributed to <a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><br></p>
<p>Welcome to Introduction to R for Data Science Session 4! The course is co-organized by <a target="_blank" href="http://www.datascience.rs/">Data Science Serbia</a> and <a target="_blank" href="http://en.startit.rs/">Startit</a>. You will find all course material (R scripts, data sets, SlideShare presentations, readings) on these pages.</p>
<p>Welcome to the fourth session of Introduction to R for Data Science! Check out the <a target="_blank" href="http://www.exactness.net/post/143651799365/introduction-to-r-for-data-science">Course Overview</a> to acess the learning material presented thus far.</p>
<p><a target="_blank" href="http://www.datascience.rs/kurs-r-za-data-science/">Data Science Serbia Course Pages [in Serbian]</a></p>
<p><a target="_blank" href="http://startit.rs/uvod-u-r-za-data-science/">Startit Course Pages [in Serbian]</a></p>
<h2><strong>Lecturers</strong></h2>
<ul>
<li><a target="_blank" href="https://rs.linkedin.com/in/kovacbranko">dipl. ing Branko Kovač</a>, Data Analyst at <a target="_blank" href="http://cube.rs/">CUBE</a>, Data Science Mentor at <a target="_blank" href="https://www.springboard.com/">Springboard</a>, Data Science Serbia</li>
<li><a target="_blank" href="http://www.exactness.net/">Goran S. Milovanović, Phd</a>, <a target="_blank" href="http://www.diplomacy.edu/">DataScientist@DiploFoundation</a>, Data Science Serbia</li>
</ul>
<h2><strong>Summary of Session 4, 19. may 2016 :: Introduction to R: Control Flow + Data Structures = R Programs</strong></h2>
<p><em>Control Flow + Data Structures = R Programs; and then vectorize! <span>Loops: for, while, repeat + break, next etc. Branching with if, else, the vectorized ifelse() construct, and switch(). When and why do loops slow down the R interpreter code execution. Introducing vectorization and some data science terminology: features in a classification toy model. The power of vectorization: why does it matter.</span></em></p>
<ul>
<li><a target="_blank" href="http://www.exactness.net/post/145045592605#slides">Session 4 SlideShare</a></li>
<li><a target="_blank" href="http://www.exactness.net/post/145045592605#script">Session 4 R Script</a></li>
<li><a target="_blank" href="http://www.exactness.net/post/145045592605#readings">Further Readings</a></li>
<li><a target="_blank" href="http://www.exactness.net/post/145045592605#photos">Session Photos</a></li>
</ul>
<h2><strong>Intro to R for Data Science SlideShare :: Session 4</strong></h2>
<p><iframe height="485" width="595" allowfullscreen src="http://www.slideshare.net/slideshow/embed_code/key/to1xVOyAge2ewh"> </iframe>
<div> <strong> <a title="Introduction to R for Data Science :: Session 4" target="_blank" href="http://www.slideshare.net/Krule/introduction-to-r-for-data-science-session-4">Introduction to R for Data Science :: Session 4</a> </strong> from <strong><a target="_blank" href="http://www.slideshare.net/Krule">Goran S. Milovanovic</a></strong> </div>
<p><br><br>
<h2><strong>R script :: Session 4</strong></h2>
<div>
<div>
<pre><span>########################################################</span>
<span># Introduction to R for Data Science</span>
<span># SESSION 4 :: 19 May, 2016</span>
<span># Data Science Community Serbia + Startit</span>
<span># :: Branko Kovač and Goran S. Milovanović ::</span>
<span>########################################################</span>
 
<span># Starting with simple 'if'</span>
num <span>&lt;-</span> <span>2</span> <span># some value to test with</span>
<span>if</span> <span>(</span>num <span>></span> <span>0</span><span>)</span> <a target="_blank" href="http://inside-r.org/r-doc/base/print"><span>print</span></a><span>(</span><span>&quot;num is positive&quot;</span><span>)</span> <span># if condition num > 0 stands than print()</span>
                                      <span># is executed</span>
 
<span># Sometimes 'if' has its 'else'</span>
<span>if</span> <span>(</span>num <span>></span> <span>0</span><span>)</span> <span>{</span> <span># test to see if it's positive</span>
  <a target="_blank" href="http://inside-r.org/r-doc/base/print"><span>print</span></a><span>(</span><span>&quot;num is positive&quot;</span><span>)</span> <span># print in case of positive number</span>
<span>}</span> <span>else</span> <span>{</span>
  <a target="_blank" href="http://inside-r.org/r-doc/base/print"><span>print</span></a><span>(</span><span>&quot;num is negative&quot;</span><span>)</span> <span># it's negative if not positive</span>
<span>}</span>
 
<span># Multiple 'else's are also possible</span>
<span>if</span> <span>(</span><span>!</span><a target="_blank" href="http://inside-r.org/r-doc/base/is.numeric"><span>is.numeric</span></a><span>(</span>num<span>)</span><span>)</span> <span>{</span> <span># check if it's numeric</span>
  <a target="_blank" href="http://inside-r.org/r-doc/base/print"><span>print</span></a><span>(</span><span>&quot;this is not a number&quot;</span><span>)</span> <span># print this if it isn't numeric</span>
<span>}</span> <span>else</span> <span>if</span> <span>(</span>num <span>></span> <span>0</span><span>)</span> <span>{</span> <span># check if it's greater than 0</span>
  <a target="_blank" href="http://inside-r.org/r-doc/base/print"><span>print</span></a><span>(</span><span>&quot;num is positive&quot;</span><span>)</span> <span># print if it's positive</span>
<span>}</span> <span>else</span> <span>if</span> <span>(</span>num <span>&lt;</span> <span>0</span><span>)</span> <span>{</span> <span># maybe it is negative</span>
  <a target="_blank" href="http://inside-r.org/r-doc/base/print"><span>print</span></a><span>(</span><span>&quot;num is negative&quot;</span><span>)</span> <span># print it if it's &lt; 0</span>
<span>}</span> <span>else</span> <a target="_blank" href="http://inside-r.org/r-doc/base/print"><span>print</span></a><span>(</span><span>&quot;ZERO!&quot;</span><span>)</span> <span># who knows, maybe it is zero</span>
 
<span># R is vectorized so there's vectorized if-else</span>
simple_vect <span>&lt;-</span> <a target="_blank" href="http://inside-r.org/r-doc/base/c"><span>c</span></a><span>(</span><span>1</span><span>,</span> <span>3</span><span>,</span> <span>12</span><span>,</span> <span>NA</span><span>,</span> <span>2</span><span>,</span> <span>NA</span><span>,</span> <span>4</span><span>)</span> <span># just another num vector with NAs</span>
<a target="_blank" href="http://inside-r.org/r-doc/base/ifelse"><span>ifelse</span></a><span>(</span><a target="_blank" href="http://inside-r.org/r-doc/base/is.na"><span>is.na</span></a><span>(</span>simple_vect<span>)</span><span>,</span> <span>&quot;nothing here&quot;</span><span>,</span> <span>&quot;some number&quot;</span><span>)</span> <span># nothing here if it's</span>
                                                          <span># NA or it's a number</span>
 
<span># For loop is always working same way</span>
<span>for</span> <span>(</span>i <span>in</span> simple_vect<span>)</span> <a target="_blank" href="http://inside-r.org/r-doc/base/print"><span>print</span></a><span>(</span>i<span>)</span> <span># iterate in set of values</span>
 
<span># Be aware that loops can be slow if</span>
vec  <span>&lt;-</span>  <a target="_blank" href="http://inside-r.org/r-doc/base/numeric"><span>numeric</span></a><span>(</span><span>)</span>
<a target="_blank" href="http://inside-r.org/r-doc/base/system.time"><span>system.time</span></a><span>(</span>
  <span>for</span><span>(</span>i <span>in</span> <a target="_blank" href="http://inside-r.org/r-doc/base/seq_len"><span>seq_len</span></a><span>(</span><span>50000</span><span>-</span><span>1</span><span>)</span><span>)</span> <span>{</span>
    some_calc <span>&lt;-</span> <a target="_blank" href="http://inside-r.org/r-doc/base/sqrt"><span>sqrt</span></a><span>(</span>i<span>/</span><span>10</span><span>)</span>
    vec <span>&lt;-</span> <a target="_blank" href="http://inside-r.org/r-doc/base/c"><span>c</span></a><span>(</span>vec<span>,</span> some_calc<span>)</span> <span># this is what makes it slow</span>
  <span>}</span>  
<span>)</span>
 
<span># This solution is slightly faster</span>
iter <span>&lt;-</span> <span>50000</span>
vec <span>&lt;-</span> <a target="_blank" href="http://inside-r.org/r-doc/base/numeric"><span>numeric</span></a><span>(</span><a target="_blank" href="http://inside-r.org/r-doc/base/length"><span>length</span></a>=iter<span>)</span> <span># this makes it faster...</span>
<a target="_blank" href="http://inside-r.org/r-doc/base/system.time"><span>system.time</span></a><span>(</span>
  <span>for</span><span>(</span>i <span>in</span> <a target="_blank" href="http://inside-r.org/r-doc/base/seq_len"><span>seq_len</span></a><span>(</span>iter<span>-</span><span>1</span><span>)</span><span>)</span> <span>{</span>
    some_calc <span>&lt;-</span> <a target="_blank" href="http://inside-r.org/r-doc/base/sqrt"><span>sqrt</span></a><span>(</span>i<span>/</span><span>10</span><span>)</span>
    vec<span>[</span>i<span>]</span> <span>&lt;-</span> some_calc <span># ...not this!</span>
  <span>}</span>
<span>)</span>
 
<span># This solution is even more faster</span>
iter <span>&lt;-</span> <span>50000</span>
vec <span>&lt;-</span> <a target="_blank" href="http://inside-r.org/r-doc/base/numeric"><span>numeric</span></a><span>(</span><a target="_blank" href="http://inside-r.org/r-doc/base/length"><span>length</span></a>=iter<span>)</span> <span># not because of this...</span>
<a target="_blank" href="http://inside-r.org/r-doc/base/system.time"><span>system.time</span></a><span>(</span>
  <span>for</span><span>(</span>i <span>in</span> <a target="_blank" href="http://inside-r.org/r-doc/base/seq_len"><span>seq_len</span></a><span>(</span>iter<span>-</span><span>1</span><span>)</span><span>)</span> <span>{</span>
    vec<span>[</span>i<span>]</span> <span>&lt;-</span> <a target="_blank" href="http://inside-r.org/r-doc/base/sqrt"><span>sqrt</span></a><span>(</span>i<span>/</span><span>10</span><span>)</span> <span># ...but this!</span>
  <span>}</span>
<span>)</span>
 
<span># Another example how loops can be slow (loop vs vectorized function)</span>
iter <span>&lt;-</span> <span>50000</span>
 
<a target="_blank" href="http://inside-r.org/r-doc/base/system.time"><span>system.time</span></a><span>(</span>
  <span>for</span> <span>(</span>i <span>in</span> <span>1</span><span>:</span>iter<span>)</span> <span>{</span>
    vec<span>[</span>i<span>]</span> <span>&lt;-</span> <a target="_blank" href="http://inside-r.org/r-doc/stats/rnorm"><span>rnorm</span></a><span>(</span>n=<span>1</span><span>,</span> <a target="_blank" href="http://inside-r.org/r-doc/base/mean"><span>mean</span></a>=<span>0</span><span>,</span> <a target="_blank" href="http://inside-r.org/r-doc/stats/sd"><span>sd</span></a>=<span>1</span><span>)</span> <span># approach from previous example</span>
  <span>}</span>
<span>)</span>
 
<a target="_blank" href="http://inside-r.org/r-doc/base/system.time"><span>system.time</span></a><span>(</span>y <span>&lt;-</span> <a target="_blank" href="http://inside-r.org/r-doc/stats/rnorm"><span>rnorm</span></a><span>(</span>iter<span>,</span> <span>0</span><span>,</span> <span>1</span><span>)</span><span>)</span> <span># but this is much much faster</span>
 
<span># R also knows about while loop</span>
r <span>&lt;-</span> <span>1</span> <span># initializing some variable</span>
<span>while</span> <span>(</span>r <span>&lt;</span> <span>5</span><span>)</span> <span>{</span> <span># while r &lt; 5</span>
  <a target="_blank" href="http://inside-r.org/r-doc/base/print"><span>print</span></a><span>(</span>r<span>)</span> <span># print r</span>
  r <span>&lt;-</span> r <span>+</span> <span>1</span> <span># increase r by 1</span>
<span>}</span>
 
<span># Loops can be nested</span>
<span>for</span><span>(</span>i <span>in</span> <span>1</span><span>:</span><span>5</span><span>)</span> <span>{</span> <span># outer loop</span>
  <span>for</span><span>(</span>j <span>in</span> <span>1</span><span>:</span><span>5</span><span>)</span> <span>{</span> <span># inner loop</span>
    <a target="_blank" href="http://inside-r.org/r-doc/base/print"><span>print</span></a><span>(</span>paste0<span>(</span>i<span>,</span>j<span>)</span><span>)</span> <span># some code</span>
  <span>}</span>
<span>}</span>
 
<span># Loops can be altered using break and next</span>
<span>for</span><span>(</span>i <span>in</span> <span>1</span><span>:</span><span>5</span><span>)</span> <span>{</span>
  <span>if</span> <span>(</span>i <span>==</span> <span>4</span><span>)</span> <span>break</span> <span># jump out of loop if condition is true</span>
  <a target="_blank" href="http://inside-r.org/r-doc/base/print"><span>print</span></a><span>(</span>i<span>)</span>
<span>}</span>
 
<span>for</span><span>(</span>i <span>in</span> <span>1</span><span>:</span><span>5</span><span>)</span> <span>{</span>
  <span>if</span> <span>(</span>i <span>==</span> <span>4</span><span>)</span> <span>next</span> <span># just skip current iteration if condition is true</span>
  <a target="_blank" href="http://inside-r.org/r-doc/base/print"><span>print</span></a><span>(</span>i<span>)</span>
<span>}</span>
 
<span># Nope, we didn't forget 'repeat' loop</span>
i <span>&lt;-</span> <span>1</span>
<span>repeat</span> <span>{</span> <span># there is no condition...</span>
  <a target="_blank" href="http://inside-r.org/r-doc/base/print"><span>print</span></a><span>(</span>i<span>)</span>
  i <span>&lt;-</span> i <span>+</span> <span>1</span>
  <span>if</span> <span>(</span>i <span>==</span> <span>10</span><span>)</span> <span>break</span> <span># ...so we have to break it if we don't want infinite loop</span>
<span>}</span>
 
<span># And there's something called 'switch' <img alt=":)" class="wp-smiley" src="http://i2.wp.com/www.r-bloggers.com/wp-includes/images/smilies/simple-smile.png?w=456" data-recalc-dims="1"></span>
<a target="_blank" href="http://inside-r.org/r-doc/base/switch"><span>switch</span></a><span>(</span><span>2</span><span>,</span> <span>&quot;data&quot;</span><span>,</span> <span>&quot;science&quot;</span><span>,</span> <span>&quot;serbia&quot;</span><span>)</span> <span># choose one option based on value</span>
 
<span># More on switch:</span>
switchIndicator <span>&lt;-</span> <span>&quot;A&quot;</span>
switchIndicator <span>&lt;-</span> <span>&quot;switchIndicator&quot;</span>
switchIndicator <span>&lt;-</span> <span>&quot;AvAvAv&quot;</span>
<span># rare situations where you do not need to enclose strings: ' ', or &quot; &quot;</span>
<a target="_blank" href="http://inside-r.org/r-doc/base/switch"><span>switch</span></a><span>(</span>switchIndicator<span>,</span>
       <span>'A'</span> = <span>{</span><a target="_blank" href="http://inside-r.org/r-doc/base/print"><span>print</span></a><span>(</span>switchIndicator<span>)</span><span>}</span><span>,</span>
       <span>'switchIndicator'</span> = <span>{</span><a target="_blank" href="http://inside-r.org/r-doc/base/unlist"><span>unlist</span></a><span>(</span><a target="_blank" href="http://inside-r.org/r-doc/base/strsplit"><span>strsplit</span></a><span>(</span>switchIndicator<span>,</span><span>&quot;h&quot;</span><span>)</span><span>)</span><span>}</span><span>,</span>
       <span>'AvAvAv'</span> = <span>{</span><a target="_blank" href="http://inside-r.org/r-doc/base/print"><span>print</span></a><span>(</span><a target="_blank" href="http://inside-r.org/r-doc/base/nchar"><span>nchar</span></a><span>(</span>switchIndicator<span>)</span><span>)</span><span>}</span>
<span>)</span>
<span># is the same as:</span>
<a target="_blank" href="http://inside-r.org/r-doc/base/switch"><span>switch</span></a><span>(</span>switchIndicator<span>,</span>
       A = <span>{</span><a target="_blank" href="http://inside-r.org/r-doc/base/print"><span>print</span></a><span>(</span>switchIndicator<span>)</span><span>}</span><span>,</span>
       switchIndicator = <span>{</span><a target="_blank" href="http://inside-r.org/r-doc/base/unlist"><span>unlist</span></a><span>(</span><a target="_blank" href="http://inside-r.org/r-doc/base/strsplit"><span>strsplit</span></a><span>(</span>switchIndicator<span>,</span><span>&quot;h&quot;</span><span>)</span><span>)</span><span>}</span><span>,</span>
       AvAvAv = <span>{</span><a target="_blank" href="http://inside-r.org/r-doc/base/print"><span>print</span></a><span>(</span><a target="_blank" href="http://inside-r.org/r-doc/base/nchar"><span>nchar</span></a><span>(</span>switchIndicator<span>)</span><span>)</span><span>}</span>
<span>)</span>
 
<span># now:</span>
type = <span>2</span>
cc <span>&lt;-</span> <a target="_blank" href="http://inside-r.org/r-doc/base/c"><span>c</span></a><span>(</span><span>&quot;A&quot;</span><span>,</span> <span>&quot;B&quot;</span><span>,</span> <span>&quot;C&quot;</span><span>)</span><span>;</span>
<a target="_blank" href="http://inside-r.org/r-doc/base/switch"><span>switch</span></a><span>(</span>type<span>,</span>
       c1 = <span>{</span><a target="_blank" href="http://inside-r.org/r-doc/base/print"><span>print</span></a><span>(</span>cc<span>[</span><span>1</span><span>]</span><span>)</span><span>}</span><span>,</span>
       c2 = <span>{</span><a target="_blank" href="http://inside-r.org/r-doc/base/print"><span>print</span></a><span>(</span>cc<span>[</span><span>2</span><span>]</span><span>)</span><span>}</span><span>,</span>
       c3 = <span>{</span><a target="_blank" href="http://inside-r.org/r-doc/base/print"><span>print</span></a><span>(</span>cc<span>[</span><span>3</span><span>]</span><span>)</span><span>}</span><span>,</span>
       <span>{</span><a target="_blank" href="http://inside-r.org/r-doc/base/print"><span>print</span></a><span>(</span><span>&quot;Beyond C...&quot;</span><span>)</span><span>}</span> <span># default choice</span>
       <span>)</span>
 
<span># BUT if you do this, R will miss the default choice, so be careful w. switch:</span>
type = <span>4</span>
cc <span>&lt;-</span> <a target="_blank" href="http://inside-r.org/r-doc/base/c"><span>c</span></a><span>(</span><span>&quot;A&quot;</span><span>,</span> <span>&quot;B&quot;</span><span>,</span> <span>&quot;C&quot;</span><span>)</span><span>;</span>
<a target="_blank" href="http://inside-r.org/r-doc/base/switch"><span>switch</span></a><span>(</span>type<span>,</span>
       <a target="_blank" href="http://inside-r.org/r-doc/base/print"><span>print</span></a><span>(</span>cc<span>[</span><span>1</span><span>]</span><span>)</span><span>,</span>
       <a target="_blank" href="http://inside-r.org/r-doc/base/print"><span>print</span></a><span>(</span>cc<span>[</span><span>2</span><span>]</span><span>)</span><span>,</span>
       <a target="_blank" href="http://inside-r.org/r-doc/base/print"><span>print</span></a><span>(</span>cc<span>[</span><span>3</span><span>]</span><span>)</span><span>,</span>
       <span>{</span><a target="_blank" href="http://inside-r.org/r-doc/base/print"><span>print</span></a><span>(</span><span>&quot;Beyond C...&quot;</span><span>)</span><span>}</span> <span># the unnamed default choice works only if previous choices are named!</span>
<span>)</span>
 
<span># Switch and if-else are similar, but switch is faster (believe us!)</span>
i <span>&lt;-</span> <span>2</span>
<span>if</span><span>(</span>i <span>==</span> <span>1</span><span>)</span> <span>{</span>
  <a target="_blank" href="http://inside-r.org/r-doc/base/print"><span>print</span></a><span>(</span><span>&quot;data&quot;</span><span>)</span>
<span>}</span> <span>else</span> <span>if</span><span>(</span>i <span>==</span> <span>2</span><span>)</span> <span>{</span>
  <a target="_blank" href="http://inside-r.org/r-doc/base/print"><span>print</span></a><span>(</span><span>&quot;science&quot;</span><span>)</span>
<span>}</span> <span>else</span> <a target="_blank" href="http://inside-r.org/r-doc/base/print"><span>print</span></a><span>(</span><span>&quot;serbia&quot;</span><span>)</span>
 
 
<span>#########################################################</span>
<span>### Exercise, exercise...</span>
 
<a target="_blank" href="http://inside-r.org/r-doc/base/library"><span>library</span></a><span>(</span><a target="_blank" href="http://inside-r.org/r-doc/datasets"><span>datasets</span></a><span>)</span>
<a target="_blank" href="http://inside-r.org/r-doc/utils/head"><span>head</span></a><span>(</span><a target="_blank" href="http://inside-r.org/r-doc/datasets/iris"><span>iris</span></a><span>)</span>
<a target="_blank" href="http://inside-r.org/r-doc/base/is.data.frame"><span>is.data.frame</span></a><span>(</span><a target="_blank" href="http://inside-r.org/r-doc/datasets/iris"><span>iris</span></a><span>)</span>
 
<span># suming up Sepal.Length + Sepal.Width</span>
<span>for</span> <span>(</span>i <span>in</span> <span>1</span><span>:</span><a target="_blank" href="http://inside-r.org/r-doc/base/length"><span>length</span></a><span>(</span><a target="_blank" href="http://inside-r.org/r-doc/datasets/iris"><span>iris</span></a><span>$</span>Sepal.Length<span>)</span><span>)</span> <span>{</span>
  <a target="_blank" href="http://inside-r.org/r-doc/datasets/iris"><span>iris</span></a><span>$</span>Sepal.LW<span>[</span>i<span>]</span> <span>&lt;-</span> <a target="_blank" href="http://inside-r.org/r-doc/datasets/iris"><span>iris</span></a><span>$</span>Sepal.Length<span>[</span>i<span>]</span> <span>+</span> <a target="_blank" href="http://inside-r.org/r-doc/datasets/iris"><span>iris</span></a><span>$</span>Sepal.Width<span>[</span>i<span>]</span>
<span>}</span>
<a target="_blank" href="http://inside-r.org/r-doc/datasets/iris"><span>iris</span></a><span>$</span>Sepal.LW <span>&lt;-</span> NULL<span>;</span>
 
<span># remember: this is a vector programming language:</span>
<a target="_blank" href="http://inside-r.org/r-doc/datasets/iris"><span>iris</span></a><span>$</span>Sepal.LW <span>&lt;-</span> <a target="_blank" href="http://inside-r.org/r-doc/datasets/iris"><span>iris</span></a><span>$</span>Sepal.Length <span>+</span> <a target="_blank" href="http://inside-r.org/r-doc/datasets/iris"><span>iris</span></a><span>$</span>Sepal.Width <span># avoid loops anytime when possible!</span>
<a target="_blank" href="http://inside-r.org/r-doc/datasets/iris"><span>iris</span></a><span>$</span>Sepal.LW <span>&lt;-</span> NULL<span>;</span>
 
out <span>&lt;-</span> <a target="_blank" href="http://inside-r.org/r-doc/base/length"><span>length</span></a><span>(</span><a target="_blank" href="http://inside-r.org/r-doc/datasets/iris"><span>iris</span></a><span>$</span>Sepal.Length<span>)</span><span>;</span>
i <span>&lt;-</span> <span>0</span><span>;</span>
<span>repeat</span>
  <span>{</span>
    i <span>&lt;-</span> i<span>+</span><span>1</span>
    <a target="_blank" href="http://inside-r.org/r-doc/datasets/iris"><span>iris</span></a><span>$</span>Sepal.LW<span>[</span>i<span>]</span> <span>&lt;-</span> <a target="_blank" href="http://inside-r.org/r-doc/datasets/iris"><span>iris</span></a><span>$</span>Sepal.Length<span>[</span>i<span>]</span> <span>+</span> <a target="_blank" href="http://inside-r.org/r-doc/datasets/iris"><span>iris</span></a><span>$</span>Sepal.Width<span>[</span>i<span>]</span><span>;</span>
    <span>if</span><span>(</span>i<span>==</span>out<span>)</span> break<span>;</span>
  <span>}</span>
<span># there are many more stupid ways to do this, all in order to avoid the simple and fast:</span>
<a target="_blank" href="http://inside-r.org/r-doc/datasets/iris"><span>iris</span></a><span>$</span>Sepal.LW <span>&lt;-</span> <a target="_blank" href="http://inside-r.org/r-doc/datasets/iris"><span>iris</span></a><span>$</span>Sepal.Length <span>+</span> <a target="_blank" href="http://inside-r.org/r-doc/datasets/iris"><span>iris</span></a><span>$</span>Sepal.Width
<a target="_blank" href="http://inside-r.org/r-doc/datasets/iris"><span>iris</span></a><span>$</span>Sepal.LW <span>&lt;-</span> NULL<span>;</span>
 
<span># for example, by avoiding loops altogether...</span>
<a target="_blank" href="http://inside-r.org/r-doc/datasets/iris"><span>iris</span></a><span>$</span>Sepal.LW <span>&lt;-</span> <a target="_blank" href="http://inside-r.org/r-doc/base/apply"><span>apply</span></a><span>(</span><a target="_blank" href="http://inside-r.org/r-doc/base/data.frame"><span>data.frame</span></a><span>(</span><a target="_blank" href="http://inside-r.org/r-doc/datasets/iris"><span>iris</span></a><span>$</span>Sepal.Length<span>,</span> <a target="_blank" href="http://inside-r.org/r-doc/datasets/iris"><span>iris</span></a><span>$</span>Sepal.Width<span>)</span><span>,</span> <span>1</span><span>,</span> <a target="_blank" href="http://inside-r.org/r-doc/base/function"><span>function</span></a><span>(</span>x<span>)</span> <span>{</span><a target="_blank" href="http://inside-r.org/r-doc/base/sum"><span>sum</span></a><span>(</span>x<span>)</span><span>}</span><span>)</span><span>;</span>
<a target="_blank" href="http://inside-r.org/r-doc/datasets/iris"><span>iris</span></a><span>$</span>Sepal.LW <span>&lt;-</span> NULL<span>;</span>
 
<span># ifelse in lapply in unlist</span>
sepalLengthMean <span>&lt;-</span> <a target="_blank" href="http://inside-r.org/r-doc/base/mean"><span>mean</span></a><span>(</span><a target="_blank" href="http://inside-r.org/r-doc/datasets/iris"><span>iris</span></a><span>$</span>Sepal.Length<span>)</span><span>;</span>
<a target="_blank" href="http://inside-r.org/r-doc/datasets/iris"><span>iris</span></a><span>$</span>Sepal.LW <span>&lt;-</span> <a target="_blank" href="http://inside-r.org/r-doc/base/unlist"><span>unlist</span></a><span>(</span><a target="_blank" href="http://inside-r.org/r-doc/base/lapply"><span>lapply</span></a><span>(</span><a target="_blank" href="http://inside-r.org/r-doc/datasets/iris"><span>iris</span></a><span>$</span>Sepal.Length<span>,</span> <a target="_blank" href="http://inside-r.org/r-doc/base/function"><span>function</span></a><span>(</span>x<span>)</span><span>{</span><a target="_blank" href="http://inside-r.org/r-doc/base/ifelse"><span>ifelse</span></a><span>(</span>x<span>>=</span>sepalLengthMean<span>,</span>T<span>,</span>F<span>)</span><span>}</span><span>)</span><span>)</span><span>;</span>
<a target="_blank" href="http://inside-r.org/r-doc/datasets/iris"><span>iris</span></a><span>$</span>Sepal.LW <span>&lt;-</span> NULL<span>;</span>
<span># however...</span>
<a target="_blank" href="http://inside-r.org/r-doc/datasets/iris"><span>iris</span></a><span>$</span>Sepal.LW <span>&lt;-</span> <a target="_blank" href="http://inside-r.org/r-doc/base/ifelse"><span>ifelse</span></a><span>(</span><a target="_blank" href="http://inside-r.org/r-doc/datasets/iris"><span>iris</span></a><span>$</span>Sepal.Length<span>>=</span><a target="_blank" href="http://inside-r.org/r-doc/base/mean"><span>mean</span></a><span>(</span><a target="_blank" href="http://inside-r.org/r-doc/datasets/iris"><span>iris</span></a><span>$</span>Sepal.Length<span>)</span><span>,</span>T<span>,</span>F<span>)</span><span>;</span>
<span># now leave the iris data set alone...</span>
<a target="_blank" href="http://inside-r.org/r-doc/datasets/iris"><span>iris</span></a><span>$</span>Sepal.LW <span>&lt;-</span> NULL<span>;</span>
 
<span>#########################################################</span>
<span>### vectorization in R</span>
 
dataSet <span>&lt;-</span> USArrests<span>;</span>
<a target="_blank" href="http://inside-r.org/r-doc/utils/head"><span>head</span></a><span>(</span>dataSet<span>)</span>
 
<span># data$Murder, data$Assault, data$Rape: columns of data</span>
 
<span># in behavioral sciences (psychology or biomedical sciences, for example) we would call them:</span>
<span># variables (or factors, even more often)</span>
<span># in data science and machine learning, we usually call them: FEATURES</span>
<span># in psychology and behavioral sciences, the usage of the term &quot;feature&quot; is usually constrained</span>
<span># to theories of categorization and concept learning</span>
 
<span># Task: classify the US states according to some global indicator of violent crime</span>
<span># Two categories (simplification): more dangerous and less dangerous (F)</span>
<span># We have three features: Murder, Rape, Assault, all per 100,000 inhabitants</span>
<span># The idea is to combine the three available features.</span>
 
<span># Let's assume that we arbitrarily assign the following preference order over the features:</span>
<span># Murder > Rape > Assault</span>
<span># in terms of the severity of the consequences of the associated criminal acts</span>
 
<span># Let's first isolate the features from the data.frame</span>
featureMatrix <span>&lt;-</span> <a target="_blank" href="http://inside-r.org/r-doc/base/as.matrix"><span>as.matrix</span></a><span>(</span>dataSet<span>[</span><span>,</span> <a target="_blank" href="http://inside-r.org/r-doc/base/c"><span>c</span></a><span>(</span><span>1</span><span>,</span><span>4</span><span>,</span><span>2</span><span>)</span><span>]</span><span>)</span><span>;</span>
 
<span># Let's WEIGHT the features in accordance with the imposed preference order:</span>
weigthsVector <span>&lt;-</span> <a target="_blank" href="http://inside-r.org/r-doc/base/c"><span>c</span></a><span>(</span><span>3</span><span>,</span><span>2</span><span>,</span><span>1</span><span>)</span><span>;</span> <span># mind the order of the columns in featureMatrix</span>
 
<span># Essentially, we want our global indicator to be a linear combination of all three selected features</span>
<span># Where each feature is weighted by the corresponding element of the weigthsVector:</span>
 
featureMatrix <span>&lt;-</span> <a target="_blank" href="http://inside-r.org/r-doc/base/cbind"><span>cbind</span></a><span>(</span>featureMatrix<span>,</span><a target="_blank" href="http://inside-r.org/r-doc/base/numeric"><span>numeric</span></a><span>(</span><a target="_blank" href="http://inside-r.org/r-doc/base/length"><span>length</span></a><span>(</span>featureMatrix<span>[</span><span>,</span><span>1</span><span>]</span><span>)</span><span>)</span><span>)</span><span>;</span>
<span>for</span> <span>(</span>i <span>in</span> <span>1</span><span>:</span><a target="_blank" href="http://inside-r.org/r-doc/base/length"><span>length</span></a><span>(</span>featureMatrix<span>[</span><span>,</span><span>1</span><span>]</span><span>)</span><span>)</span> <span>{</span>
  featureMatrix<span>[</span>i<span>,</span><span>4</span><span>]</span> <span>&lt;-</span> <a target="_blank" href="http://inside-r.org/r-doc/base/sum"><span>sum</span></a><span>(</span>weigthsVector<span>*</span>featureMatrix<span>[</span>i<span>,</span><span>1</span><span>:</span><span>3</span><span>]</span><span>)</span><span>;</span>
  <span># don't forget: this &quot;*&quot; multiplication in R is vectorized and operates element-wise</span>
  <span># we have a 1x3 weightsVector and a 1x3 featureMatrix[i,1:3], Ok</span>
  <span># sum() then produces the desired linear combination</span>
<span>}</span>
 
<span># Classification; in the simplest case, let's simply take a look at the distribution of our global indicator:</span>
<a target="_blank" href="http://inside-r.org/r-doc/graphics/hist"><span>hist</span></a><span>(</span>featureMatrix<span>[</span><span>,</span><span>4</span><span>]</span><span>,</span><span>20</span><span>)</span><span>;</span> <span># it's multimodal and not too symmetric; go for median</span>
criterion <span>&lt;-</span> <a target="_blank" href="http://inside-r.org/r-doc/stats/median"><span>median</span></a><span>(</span>featureMatrix<span>[</span><span>,</span><span>4</span><span>]</span><span>)</span><span>;</span>
<span># And classify:</span>
dataSet<span>$</span>Dangerous <span>&lt;-</span> <a target="_blank" href="http://inside-r.org/r-doc/base/ifelse"><span>ifelse</span></a><span>(</span>featureMatrix<span>[</span><span>,</span><span>4</span><span>]</span><span>>=</span>criterion<span>,</span>T<span>,</span>F<span>)</span><span>;</span>
 
<span># Ok. You will never do this before you have a model that has actually *learned* the</span>
<span># most adequate feature weights. This is an exercise only.</span>
 
<span># ***Important***: have you seen the for loop above? Well...</span>
<span># N e v e r d o t h a t.</span>
dataSet<span>$</span>Dangerous <span>&lt;-</span> NULL<span>;</span>
 
<span># In Data Science, you will be working with huge amounts of quantitative data.</span>
<span># For loops are slow. But in vector programming languages like R...</span>
<span># matrix computations are seriously fast.</span>
 
<span># What you ***want to do*** is the following:</span>
 
<span># Let's first isolate the features from the data.frame</span>
featureMatrix <span>&lt;-</span> <a target="_blank" href="http://inside-r.org/r-doc/base/as.matrix"><span>as.matrix</span></a><span>(</span>dataSet<span>[</span><span>,</span> <a target="_blank" href="http://inside-r.org/r-doc/base/c"><span>c</span></a><span>(</span><span>1</span><span>,</span><span>4</span><span>,</span><span>2</span><span>)</span><span>]</span><span>)</span><span>;</span>
<span># Let's WEIGHT the features in accordance with the imposed preference order:</span>
weigthsVector <span>&lt;-</span> <a target="_blank" href="http://inside-r.org/r-doc/base/c"><span>c</span></a><span>(</span><span>3</span><span>,</span><span>2</span><span>,</span><span>1</span><span>)</span><span>;</span> <span># mind the order of the columns in featureMatrix</span>
 
<span># Feature weighting:</span>
wF <span>&lt;-</span> weigthsVector <span>%*%</span> <a target="_blank" href="http://inside-r.org/r-doc/base/t"><span>t</span></a><span>(</span>featureMatrix<span>)</span><span>;</span>
<span># In R, t() is for: transpose</span>
<span># In R, %*% is matrix multiplication</span>
 
<span># oh yes: R knows about row and column vectors - and you want to put this one</span>
<span># as a COLUMN in your dataSet data.frame, while wF is currently a ROW vector, look:</span>
wF
<a target="_blank" href="http://inside-r.org/r-doc/base/length"><span>length</span></a><span>(</span>wF<span>)</span><span>;</span>
wF <span>&lt;-</span> <a target="_blank" href="http://inside-r.org/r-doc/base/t"><span>t</span></a><span>(</span>wF<span>)</span><span>;</span> 
 
<span># and classify:</span>
dataSet<span>$</span>Dangerous <span>&lt;-</span> <a target="_blank" href="http://inside-r.org/r-doc/base/ifelse"><span>ifelse</span></a><span>(</span>wF<span>>=</span><a target="_blank" href="http://inside-r.org/r-doc/stats/median"><span>median</span></a><span>(</span>wF<span>)</span><span>,</span>T<span>,</span>F<span>)</span><span>;</span></pre>
</div>
</div>
<p><a title="Created by Pretty R at inside-R.org" target="_blank" href="http://www.inside-r.org/pretty-r">Created by Pretty R at inside-R.org</a></p>
<p><br></p>
<h2><strong>Readings :: Session 5 [26. May, 2016, @Startit.rs, 19h CET]</strong></h2>
<p><a target="_blank" href="http://r4ds.had.co.nz/"><i>R for Data Science</i>, Garrett Grolemund &amp; Hadley Wickham: <b>Chapter 10: Strings</b></a></p>
<h2><strong>Session 4 Photos</strong></h2>
<p><img alt="20160428_204815" class="wp-image-907" src="http://i0.wp.com/www.datascience.rs/wp-content/uploads/S4_3-300x225.jpg?resize=300%2C180" data-recalc-dims="1"><img alt="image" class="wp-image-922" src="http://i2.wp.com/www.datascience.rs/wp-content/uploads/S4_6-300x225.jpg?resize=300%2C180" data-recalc-dims="1"></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/introduction-to-r-for-data-science-session-4/"></div></div><div><a data-url="http://www.r-bloggers.com/introduction-to-r-for-data-science-session-4/" data-counturl="http://www.r-bloggers.com/introduction-to-r-for-data-science-session-4/" data-count="vertical" data-text="Introduction to R for Data Science :: Session 4" 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://www.exactness.net/post/145045592605"> The Exactness of Mind</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/kOTUPa41EQc"></a></div>