<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>The Devil is in the Data on Lucid Manager</title>
    <link>https://lucidmanager.org/categories/data-science/</link>
    <description>Recent content in The Devil is in the Data on Lucid Manager</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en-gb</language>
    <managingEditor>Peter Prevos</managingEditor>
    <webMaster>Peter Prevos</webMaster>
    
    
    <lastBuildDate>Thu, 01 Dec 2016 00:00:00 +0000</lastBuildDate>
    
	<atom:link href="https://lucidmanager.org/categories/data-science/index.xml" rel="self" type="application/rss+xml" />
    
    
    
    <item>
      <title>Visualise and Analyse Bathymetric Survey Data with R</title>
      <link>https://lucidmanager.org/data-science/analyse-bathymetric-survey/</link>
      <pubDate>Sun, 14 Dec 2025 00:00:00 +0000</pubDate>
      <author>Peter Prevos</author>
      <guid>https://lucidmanager.org/data-science/analyse-bathymetric-survey/</guid>
      <description>
	
&lt;p&gt;
The first step in the value chain for water utilities is extracting water from natural sources, such as rivers, groundwater, oceans, or reservoirs. Water managers and the public alike are interested in knowing how much water a reservoir currently holds. This is not an easy problem to solve, as it requires some analysis to move from survey to volume measurement. This article shows how to visualise and analyse bathymetric survey data using the R language and data from the Prettyboy Reservoir in Maryland, USA.&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-1&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-1&#34;&gt;
The Prettyboy reservoir
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-1&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;While searching the web for a publicly available bathymetric survey, I stumbled across the Maryland Geological Survey (MGS) website. This organisation investigates the geology and water resources of the American state of Maryland. The Prettyboy Reservoir helps provide water for nearly two million residents in Baltimore City and parts of five neighbouring Maryland counties.&lt;/p&gt;
&lt;p&gt;
The MGS publishes bathymetric data for their reservoirs, including the &lt;a href=&#34;http://www.mgs.md.gov/coastal_geology/prettyboy.html&#34;&gt;Prettyboy reservoir&lt;/a&gt; in the county of Baltimore.&lt;/p&gt;
&lt;figure&gt;
&lt;iframe width=&#34;600&#34; height=&#34;400&#34; style=&#34;height:400px !important; border:0;&#34; frameborder=&#34;0&#34; scrolling=&#34;no&#34; marginheight=&#34;0&#34; marginwidth=&#34;0&#34; src=&#34;https://www.openstreetmap.org/export/embed.html?bbox=-76.79778099%2C39.60%2C-76.684%2C39.66709406770333&amp;amp;layer=mapnik&amp;amp;marker=39.6371580705754%2C-76.74113273620605&#34; style=&#34;border: 0px solid black&#34;&gt;&lt;/iframe&gt;&lt;br/&gt;&lt;small&gt;&lt;a href=&#34;https://www.openstreetmap.org/?mlat=39.6372&amp;amp;mlon=-76.7411#map=14/39.6372/-76.7411&#34; target=&#34;_blank&#34;&gt;View Larger Map&lt;/a&gt;&lt;/small&gt;
&lt;figcaption&gt;
Pretty Boy reservoir in Maryland.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-2&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-2&#34;&gt;
Extract, Transfer and Load
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-2&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The &lt;a href=&#34;http://www.mgs.md.gov/publications/data_pages/reservoir_bathymetry.html&#34;&gt;Maryland Geological Survey website&lt;/a&gt; hosts the raw data as a zipped CSV file. &lt;/p&gt;
&lt;p&gt;
The file has a double header, the first row contains the field names and the second row the units. The data has three columns:&lt;/p&gt;
&lt;figure&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th class=&#34;align-right&#34;&gt;Easting&lt;/th&gt;
&lt;th class=&#34;align-right&#34;&gt;Northing&lt;/th&gt;
&lt;th class=&#34;align-right&#34;&gt;Depth&lt;/th&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;th class=&#34;align-right&#34;&gt;UTM-NAD83-Meters&lt;/th&gt;
&lt;th class=&#34;align-right&#34;&gt;UTM-NAD83-Meters&lt;/th&gt;
&lt;th class=&#34;align-right&#34;&gt;Feet&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td class=&#34;align-right&#34;&gt;352606.8&lt;/td&gt;
&lt;td class=&#34;align-right&#34;&gt;4386507&lt;/td&gt;
&lt;td class=&#34;align-right&#34;&gt;9.7&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=&#34;align-right&#34;&gt;352606.8&lt;/td&gt;
&lt;td class=&#34;align-right&#34;&gt;4386507&lt;/td&gt;
&lt;td class=&#34;align-right&#34;&gt;8.8&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=&#34;align-right&#34;&gt;352606.5&lt;/td&gt;
&lt;td class=&#34;align-right&#34;&gt;4386507&lt;/td&gt;
&lt;td class=&#34;align-right&#34;&gt;11.4&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;figcaption&gt;
Prettyboy reservoir bathymetry data structure.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
The Easting and Northing are in the Universal Transverse Mercator projection. As a demonstration of unit confusion, the reservoir&amp;#39;s depth in feet. As usual in bathymetry surveys, depth is measured from the datum as a positive number. The zero datum is the reservoir&amp;#39;s Full Supply Level.&lt;/p&gt;
&lt;p&gt;
The script below extracts, transforms and loads (ETL) the data. The file contains duplicates and a zero point at each of the four corners, which are removed from the data. This leaves us a clean file to work from.&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;# Bathymetry visualisation and analysis&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;# libraries&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(tidyverse)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(RColorBrewer)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;rm&lt;/span&gt;(list &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ls&lt;/span&gt;())
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;# Data source&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;# http://www.mgs.md.gov/publications/data_pages/reservoir_bathymetry.html&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (&lt;span style=&#34;color:#333&#34;&gt;!&lt;/span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;file.exists&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;data/PrettyBoy1998.dat&amp;#34;&lt;/span&gt;)) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;download.file&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;http://www.mgs.md.gov/output/reservoir_bathy/PrettyBoy1998.zip&amp;#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                  destfile &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;data/prettyboy-1998.zip&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;unzip&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;data/prettyboy-1998.zip&amp;#34;&lt;/span&gt;, exdir &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;data&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;file.remove&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;data/prettyboy-1998.zip&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;# Read data&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  rawdata &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;read.csv&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;data/PrettyBoy1998.dat&amp;#34;&lt;/span&gt;, skip &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  fsl &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;145&lt;/span&gt; &lt;span style=&#34;color:#888&#34;&gt;# Full Supply Level&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;# Data cleaning&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  prettyboy &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; rawdata &lt;span style=&#34;color:#333&#34;&gt;|&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;rename&lt;/span&gt;(easting &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;, northing &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;, depth_ft &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;3&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;|&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;filter&lt;/span&gt;(&lt;span style=&#34;color:#333&#34;&gt;!&lt;/span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;duplicated&lt;/span&gt;(rawdata) &lt;span style=&#34;color:#333&#34;&gt;&amp;amp;&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;!&lt;/span&gt;(easting &lt;span style=&#34;color:#333&#34;&gt;%in%&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;range&lt;/span&gt;(easting))) &lt;span style=&#34;color:#333&#34;&gt;|&amp;gt;&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;mutate&lt;/span&gt;(depth_m &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; depth_ft &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0.304&lt;/span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-3&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-3&#34;&gt;
Visualise the Data
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-3&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;This visualisation shows the lines that the survey vessel sailed to capture the sonar soundings. The edge of the reservoir was measured with a terrestrial survey and has a depth of zero.&lt;/p&gt;
&lt;p&gt;
The code defines a colour palette for this data using green and blue. The colours combine two palettes from the green and blue palettes so that only zero values are green and positive values are blue.&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;# Basic Visualisation&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  bathymetry_colours &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;rev&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;brewer.pal&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;3&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Greens&amp;#34;&lt;/span&gt;))[&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;-2&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;-3&lt;/span&gt;], 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                          &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;brewer.pal&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;9&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Blues&amp;#34;&lt;/span&gt;)[&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;-1&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;-3&lt;/span&gt;])
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ggplot&lt;/span&gt;(prettyboy) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;aes&lt;/span&gt;(easting, northing, colour &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; depth_m) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;geom_point&lt;/span&gt;(size &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;.1&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;scale_x_continuous&lt;/span&gt;(labels &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; scales&lt;span style=&#34;color:#333&#34;&gt;::&lt;/span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;label_comma&lt;/span&gt;()) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;scale_y_continuous&lt;/span&gt;(labels &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; scales&lt;span style=&#34;color:#333&#34;&gt;::&lt;/span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;label_comma&lt;/span&gt;()) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;coord_equal&lt;/span&gt;() &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;labs&lt;/span&gt;(colour &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Depth [m]&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;scale_colour_gradientn&lt;/span&gt;(colors &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; bathymetry_colours) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;labs&lt;/span&gt;(title &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Prettyboy Reservoir Bathymetry (1988)&amp;#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;         caption &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Source: Maryland Geological Survey&amp;#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;         x &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Easting (UTM NAD83)&amp;#34;&lt;/span&gt;, y &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Northing (UTM NAD83)&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;theme_minimal&lt;/span&gt;(base_size &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;8&lt;/span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/hydroinformatics/prettyboy-bathymetry.png&#34; alt=&#34;Prettyboy Reservoir bathymetry&#34; title=&#34;Prettyboy Reservoir bathymetry&#34; width=&#34;600&#34;/&gt;
&lt;figcaption&gt;
Prettyboy Reservoir bathymetry.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-4&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-4&#34;&gt;
Mapping the data
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-4&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The coordinates are provided in the North American UTM / NAD83 projection. To map this survey data onto a map tile, we need to convert these to longitude and latitudes for the WGS84 spheroid.&lt;/p&gt;
&lt;p&gt;
The &lt;a href=&#34;https://r-spatial.github.io/sf/&#34;&gt;SF (Simple Features) package&lt;/a&gt; converts the coordinates for use in Google maps. To achieve this we need the CRS (Coordinate Reference System) with number 26918, as Maryland is in zone 18N: &lt;a href=&#34;https://spatialreference.org/ref/epsg/26918/&#34;&gt;EPSG:26918 NAD83 / UTM zone 18N&lt;/a&gt;. The second CRS (4326) is the system used by Google maps: &lt;a href=&#34;https://spatialreference.org/ref/epsg/4326/&#34;&gt;EPSG:4326 WGS 84&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;
We can now plot the data on a Google Maps tile using the &lt;a href=&#34;https://cran.r-project.org/web/packages/ggmap/index.html&#34;&gt;ggmap package&lt;/a&gt;. This functionality requires a Google API key, which I am not sharing here. You can also use this data in other spatial systems, such as the interactive tooling in &lt;a href=&#34;https://r-charts.com/spatial/interactive-maps-leaflet/&#34;&gt;the Leaflet package&lt;/a&gt;.&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;# Spatial mapping&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(sf)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;# Convert prettyboy tibble to an sf object&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  prettyboy_sf &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;st_as_sf&lt;/span&gt;(prettyboy,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                           coords &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;easting&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;northing&amp;#34;&lt;/span&gt;),
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                           crs &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;26918&lt;/span&gt;)   &lt;span style=&#34;color:#888&#34;&gt;# NAD83 / UTM zone 18N&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;# Transform to latitude/longitude (WGS84)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  prettyboy_wgs &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;st_transform&lt;/span&gt;(prettyboy_sf, crs &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;4326&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;# Convert back to tibble with lat/lon columns&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  coords &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;st_coordinates&lt;/span&gt;(prettyboy_wgs)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  prettyboy_wgs &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;cbind&lt;/span&gt;(prettyboy_wgs, coords) &lt;span style=&#34;color:#333&#34;&gt;|&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;rename&lt;/span&gt;(lon &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; X, lat &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; Y) &lt;span style=&#34;color:#333&#34;&gt;|&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;st_drop_geometry&lt;/span&gt;()
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(ggmap)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Register Google Maps API&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  api &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;readLines&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;data/google-maps.api&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;register_google&lt;/span&gt;(key &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; api)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  prettyboy_map &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;get_map&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;mean&lt;/span&gt;(prettyboy_wgs&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;lon), 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                             &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;mean&lt;/span&gt;(prettyboy_wgs&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;lat)),
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                           zoom &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;14&lt;/span&gt;, color &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;bw&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ggmap&lt;/span&gt;(prettyboy_map, extent &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;device&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;geom_point&lt;/span&gt;(data &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;as_tibble&lt;/span&gt;(prettyboy_wgs), 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;               &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;aes&lt;/span&gt;(lon, lat, col &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; depth_m), size &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0.1&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;scale_colour_gradientn&lt;/span&gt;(colors &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; bathymetry_colours, name &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Depth [m]&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;labs&lt;/span&gt;(title &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Prettyboy Reservoir Bathymetry (1988)&amp;#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;         caption &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Source: Maryland Geological Survey&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;theme_void&lt;/span&gt;(base_size &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/hydroinformatics/prettyboy-bathymetry-map.png&#34; alt=&#34;Prettyboy bathymetry on Google map tile&#34; title=&#34;Prettyboy bathymetry on Google map tile&#34; width=&#34;400&#34;/&gt;
&lt;figcaption&gt;
Prettyboy bathymetry on Google map tile.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-5&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-5&#34;&gt;
Analyse Bathymetric Survey: Full Supply Volume
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-5&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;Two methods are available to estimate the total volume of a reservoir using bathymetric data. The analogue method uses contour lines to draw cross-sections on graph paper at regular intervals. Then use a &lt;a href=&#34;https://en.wikipedia.org/wiki/Planimeter&#34;&gt;planimeter&lt;/a&gt; or the &lt;a href=&#34;https://en.wikipedia.org/wiki/Shoelace_formula&#34;&gt;shoelace formula&lt;/a&gt; to measure the surface area of each section and calculate the total volume by multiplying the surface area by the spacing between sections. I used this method more than thirty years ago when working on dredging projects. But technology has moved on.&lt;/p&gt;
&lt;p&gt;
The second method is more numeric, and there are two methods: interpolate a grid or build a Triangular Irregular Network.&lt;/p&gt;
&lt;p&gt;
The bathymetry provides data only for the lines on the map, as visualised above. To estimate the full supply analytically, we can overlay the reservoir with a grid and interpolate the depth for each cell. We can then estimate the Full Supply Volume by summing the product of each cell area and depth for each grid item. When we have a grid with &lt;em&gt;n&lt;/em&gt; elements, and &lt;em&gt;x&lt;/em&gt; and &lt;em&gt;y&lt;/em&gt; indicate the element size and &lt;em&gt;d&lt;sub&gt;i&lt;/sub&gt;&lt;/em&gt; the depth of an element, the volume is calculated with:&lt;/p&gt;
&lt;p&gt;
$$V = \sum_{i=1}^{n} {x \times y \times d_i}$$&lt;/p&gt;
&lt;p&gt;
The &lt;a href=&#34;https://cran.r-project.org/web/packages/akima/index.html&#34;&gt;akima package&lt;/a&gt; provides functionality for interpolation of irregularly and regularly spaced data, making it ideal for our problem. First, we set the spacing to 10 meters. The smaller the interval, the more accurate the estimation. But small spacing comes at a cost, as the interpolation will take some time to finalise.&lt;/p&gt;
&lt;p&gt;
Once we have a grid with a depth for each location, we can estimate the Full Supply Volume by multiplying each location&amp;#39;s depth by its grid area and summing across all grids.&lt;/p&gt;
&lt;p&gt;
This analysis provides a volume of  71.50 × 10&lt;sup&gt;6&lt;/sup&gt; m&lt;sup&gt;3&lt;/sup&gt;, which is close to the 19 billion US gallons (71.92 × 10&lt;sup&gt;6&lt;/sup&gt; m&lt;sup&gt;3&lt;/sup&gt;) reported by the Maryland Geological Survey.&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;# Extrapolate gridded version&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(akima)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;# Grid size [m]&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  dx &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  dy &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;# Regular grid coordinates&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  x_seq &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;seq&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;min&lt;/span&gt;(prettyboy&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;easting), &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;max&lt;/span&gt;(prettyboy&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;easting), by &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; dx)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  y_seq &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;seq&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;min&lt;/span&gt;(prettyboy&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;northing), &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;max&lt;/span&gt;(prettyboy&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;northing), by &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; dy)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;# Interpolate scattered (x, y, depth) -&amp;gt; regular grid&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;# no extrapolation outside convex hull&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  interp_res &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;with&lt;/span&gt;(prettyboy,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                     &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;interp&lt;/span&gt;(x &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; easting,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                            y &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; northing,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                            z &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; depth_m,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                            xo &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; x_seq,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                            yo &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; y_seq,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                            duplicate &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;strip&amp;#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                            linear &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;TRUE&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                            extrap &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;FALSE&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;# Volume&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  prettyboy_grid &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;interp2xyz&lt;/span&gt;(interp_res, data.frame &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;TRUE&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;|&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;as_tibble&lt;/span&gt;() &lt;span style=&#34;color:#333&#34;&gt;|&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;rename&lt;/span&gt;(easting &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; x, northing &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; y, depth_m &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; z)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  grid_volume_m3 &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sum&lt;/span&gt;(prettyboy_grid&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;depth_m &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; dx &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; dy, na.rm &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;TRUE&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  grid_volume_m3 &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;100&lt;/span&gt;^3&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
We can use the interpolated values to create a nice map of the reservoir with contour lines and cross-sections. The cross sections are taken at four random points in the grid.&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;# Visualise Cross sections&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;set.seed&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1234&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;northing_sections &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sample&lt;/span&gt;(prettyboy_grid&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;northing, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;4&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;prettyboy_sections &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;filter&lt;/span&gt;(prettyboy_grid, northing &lt;span style=&#34;color:#333&#34;&gt;%in%&lt;/span&gt; northing_sections)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;max_depth &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;round&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;max&lt;/span&gt;(prettyboy_sections&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;depth_m, na.rm &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;TRUE&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;map &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ggplot&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;filter&lt;/span&gt;(prettyboy_grid)) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;aes&lt;/span&gt;(easting, northing) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;geom_raster&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;aes&lt;/span&gt;(fill &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; depth_m), interpolate &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;TRUE&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;geom_contour&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;aes&lt;/span&gt;(z &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; depth_m), colour &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;white&amp;#34;&lt;/span&gt;, alpha &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0.4&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;geom_hline&lt;/span&gt;(yintercept &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; northing_sections, col &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;orange&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;coord_equal&lt;/span&gt;() &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;scale_x_continuous&lt;/span&gt;(labels &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; scales&lt;span style=&#34;color:#333&#34;&gt;::&lt;/span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;label_comma&lt;/span&gt;()) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;scale_y_continuous&lt;/span&gt;(labels &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; scales&lt;span style=&#34;color:#333&#34;&gt;::&lt;/span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;label_comma&lt;/span&gt;()) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;scale_fill_gradientn&lt;/span&gt;(
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    colours &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;NA&lt;/span&gt;, bathymetry_colours[&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;-1&lt;/span&gt;]),
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    na.value &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;transparent&amp;#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    guide &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;guide_colourbar&lt;/span&gt;(reverse &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;TRUE&lt;/span&gt;),
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    name &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Depth [m]&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;theme_minimal&lt;/span&gt;(base_size &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;28&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;labs&lt;/span&gt;(title &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Prettyboy Reservoir&amp;#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;       caption &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Source: Maryland Geological Survey&amp;#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;       x &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Easting [m]&amp;#34;&lt;/span&gt;, y &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Northing [m]&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;theme&lt;/span&gt;(legend.position &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;bottom&amp;#34;&lt;/span&gt;, 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        legend.key.width &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;unit&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#39;cm&amp;#39;&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;sections &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ggplot&lt;/span&gt;(prettyboy_sections) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;aes&lt;/span&gt;(easting, (fsl &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; depth_m)) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;geom_area&lt;/span&gt;(fill &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; bathymetry_colours[1], alpha &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0.5&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;  
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;scale_x_continuous&lt;/span&gt;(labels &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; scales&lt;span style=&#34;color:#333&#34;&gt;::&lt;/span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;label_comma&lt;/span&gt;()) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;facet_wrap&lt;/span&gt;(&lt;span style=&#34;color:#333&#34;&gt;~&lt;/span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;rev&lt;/span&gt;(northing), ncol &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;coord_cartesian&lt;/span&gt;(ylim &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(fsl &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1.1&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;max&lt;/span&gt;(prettyboy_sections&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;depth_m, na.rm &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;TRUE&lt;/span&gt;), fsl)) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;theme_minimal&lt;/span&gt;(base_size &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;28&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;labs&lt;/span&gt;(x &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Easting [m]&amp;#34;&lt;/span&gt;, y &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Elevation [m]&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(gridExtra)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;png&lt;/span&gt;(filename &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;prettyboy-sections.png&amp;#34;&lt;/span&gt;, width &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1920&lt;/span&gt;, height &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;940&lt;/span&gt;, units &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;px&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;grid.arrange&lt;/span&gt;(map, sections, ncol &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;dev.off&lt;/span&gt;()&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/hydroinformatics/prettyboy-sections.png&#34; alt=&#34;Prettyboy Reservoir bathemetry and cross sections&#34; title=&#34;Prettyboy Reservoir cross sections&#34; width=&#34;800&#34;/&gt;
&lt;figcaption&gt;
Prettyboy Reservoir cross sections.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
The second method for calculating the total volume is to use a &lt;a href=&#34;https://en.wikipedia.org/wiki/Triangulated_irregular_network&#34;&gt;Triangulated Irregular Network&lt;/a&gt; (TIN). A TIN is a representation of a surface consisting of triangular facets. The main difference with the interpolated values is that this model is irregular, so each triangle has a different projected area.&lt;/p&gt;
&lt;p&gt;
The &lt;a href=&#34;https://cran.r-project.org/web/packages/geometry/index.html&#34;&gt;geometry package&lt;/a&gt; implements Delaunay triangulation, where each row contains the indices of three points forming a triangle. In the next step, we use the &lt;code&gt;map_dfr()&lt;/code&gt; function to create a data frame with the index number of each triangle, the three coordinates and the associated depths.&lt;/p&gt;
&lt;p&gt;
To measure the volume, we can use the shoelace formula to calculate the area of each triangle:&lt;/p&gt;
&lt;p&gt;
$$A_i = \frac{1}{2}| x_{i_1} (y_{1_2} - y_{i_3}) + x_{i_2} (y_{1_3} - y_{i_1}) + x_{i_3} (y_{1_1} - y_{i_2})|$$&lt;/p&gt;
&lt;p&gt;
The volume of the element is the area multiplied by the average depth:&lt;/p&gt;
&lt;p&gt;
$$V = \sum{A_i \frac{\sum d_i}{3}}$$&lt;/p&gt;
&lt;p&gt;
This method provides the same volume but is a bit faster to process because we are not creating new data.&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;# Triangulated Irregular Network&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(geometry) 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(purrr)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;# 2D coordinates&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  coords2d &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;as.matrix&lt;/span&gt;(prettyboy[, &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;easting&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;northing&amp;#34;&lt;/span&gt;)])
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;# Delaunay triangulation&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  tri &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;delaunayn&lt;/span&gt;(coords2d)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;# Convert the triangles into a long data frame for ggplot&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  tin_df &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;map_dfr&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;seq_len&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;nrow&lt;/span&gt;(tri)), &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;function&lt;/span&gt;(i) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    idx &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; tri[i, ]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;data.frame&lt;/span&gt;(
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      tri_id   &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; i,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      easting  &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; prettyboy&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;easting[idx],
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      northing &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; prettyboy&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;northing[idx],
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      depth_m  &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; prettyboy&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;depth_m[idx])})
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  triangle_area_shoelace &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;function&lt;/span&gt;(x, y) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;abs&lt;/span&gt;(x[1] &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; (y[2] &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; y[3]) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        x[2] &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; (y[3] &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; y[1]) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        x[3] &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; (y[1] &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; y[2])) &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  tin_volume &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; tin_df &lt;span style=&#34;color:#333&#34;&gt;|&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    dplyr&lt;span style=&#34;color:#333&#34;&gt;::&lt;/span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;group_by&lt;/span&gt;(tri_id) &lt;span style=&#34;color:#333&#34;&gt;|&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    dplyr&lt;span style=&#34;color:#333&#34;&gt;::&lt;/span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;summarise&lt;/span&gt;(
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;             area       &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;triangle_area_shoelace&lt;/span&gt;(easting, northing),
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;             mean_depth &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;mean&lt;/span&gt;(depth_m),
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;             volume     &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; area &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; mean_depth,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;             .groups    &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;drop&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  tin_volume_m3 &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sum&lt;/span&gt;(tin_volume&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;volume)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  tin_volume_m3 &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1e6&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-6&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-6&#34;&gt;
Generate Capacity Charts
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-6&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;Last but not least, we need a chart that converts the measured reservoir level to a volume. The easiest method to develop this curve is to use the grid. The TIN becomes mathematically problematic when not all three triangle points are underwater.&lt;/p&gt;
&lt;p&gt;
While bathymetry measures depth from the top down, with higher numbers indicating greater depth, reservoir levels are expressed in elevation. I could not find the official elevation of the Full Supply Level. Google Maps provides an elevation of 145 meters for the dam, so let&amp;#39;s use this.&lt;/p&gt;
&lt;p&gt;
The measured values closely matched a cubic regression, so we can calculate the volume of the reservoir using the level with:&lt;/p&gt;
&lt;p&gt;
$$V(\ell) =-3.463 \times 10^{9} + 8.883 \times 10^{7}\,\ell - 7.640 \times 10^{5}\,\ell^{2} + 2.203 \times 10^{3}\,\ell^{3}$$&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;# Capacity Curve&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;# levels to evaluate&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  levels &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;seq&lt;/span&gt;(fsl &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;15&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;round&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;max&lt;/span&gt;(prettyboy&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;depth_m) &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt;, fsl, by &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  grid_capacity &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;tibble&lt;/span&gt;(level &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; levels) &lt;span style=&#34;color:#333&#34;&gt;|&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;mutate&lt;/span&gt;(volume_m3 &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;map_dbl&lt;/span&gt;(level, &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;\&lt;/span&gt;(L) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      bed &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; fsl &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; prettyboy_grid&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;depth_m
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      depth_L &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;pmax&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;, L &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; bed)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sum&lt;/span&gt;(depth_L &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; dx &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; dy, na.rm &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;TRUE&lt;/span&gt;)}))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;# Cubic regression&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  cubic &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;lm&lt;/span&gt;(volume_m3 &lt;span style=&#34;color:#333&#34;&gt;~&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;poly&lt;/span&gt;(level, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;3&lt;/span&gt;, raw &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;TRUE&lt;/span&gt;), data &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; grid_capacity)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  grid_capacity&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;cubic &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;predict&lt;/span&gt;(cubic)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ggplot&lt;/span&gt;(grid_capacity) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;aes&lt;/span&gt;(level, volume_m3 &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1E3&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;geom_point&lt;/span&gt;(col &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;grey&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;geom_line&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;aes&lt;/span&gt;(level, cubic &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1E3&lt;/span&gt;)) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;geom_hline&lt;/span&gt;(yintercept &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; grid_volume_m3 &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1E3&lt;/span&gt;, linetype &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;6&lt;/span&gt;, col &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;red&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;scale_y_continuous&lt;/span&gt;(labels &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; scales&lt;span style=&#34;color:#333&#34;&gt;::&lt;/span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;label_comma&lt;/span&gt;()) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;theme_minimal&lt;/span&gt;() &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;labs&lt;/span&gt;(title &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Prettyboy Reservoir Capacity Curve&amp;#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;         x &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Water level&amp;#34;&lt;/span&gt;, y &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;expression&lt;/span&gt;(Volume&lt;span style=&#34;color:#333&#34;&gt;~&lt;/span&gt;km^3))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/hydroinformatics/prettyboy-capacity-curve.png&#34; alt=&#34;Prettyboy Reservoir capacity curve&#34; title=&#34;Prettyboy capacity curve&#34; width=&#34;800&#34;/&gt;
&lt;figcaption&gt;
Prettyboy Capacity Curve.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;/div&gt;
&lt;/div&gt;

      </description>
    </item>
    
    
    
    <item>
      <title>A Brief Guide to Providing Insights as a Service</title>
      <link>https://lucidmanager.org/data-science/insights-as-a-service/</link>
      <pubDate>Sat, 21 Jun 2025 00:00:00 +0000</pubDate>
      <author>Peter Prevos</author>
      <guid>https://lucidmanager.org/data-science/insights-as-a-service/</guid>
      <description>
	
&lt;p&gt;
Analysing data requires a high level of technical skill, which Drew Conway summarised in his often-cited &lt;a href=&#34;http://drewconway.com/zia/2013/3/26/the-data-science-venn-diagram&#34;&gt;Venn diagram&lt;/a&gt;. Following this model, data scientists, individually or as a team, require competencies in computer science, statistics, and knowledge of the domain they are analysing. There is, however, more to providing value from data than these three technical skills. This article introduces the concept of &lt;em&gt;Insights as a Service&lt;/em&gt; (IaaS), a framework to assist data science teams with maximising the value they deliver to their internal or external customers.&lt;/p&gt;
&lt;p&gt;
With the ongoing improvement of AI capabilities, the technical skills of data professionals are becoming increasingly automated. This development requires data engineers, analysts, and scientists to embrace the elusive softer skills and use insights from &lt;a href=&#34;https://lucidmanager.org/categories/marketing/&#34;&gt;marketing&lt;/a&gt; and the &lt;a href=&#34;https://lucidmanager.org/tags/humanities/&#34;&gt;humanities&lt;/a&gt;. The &lt;em&gt;insights as a Service Framework&lt;/em&gt; enables data professionals to plan and execute their projects, creating greater value for their clients.&lt;/p&gt;
&lt;p&gt;
This model incorporates lessons I learned while managing a data team for over ten years, as well as my research into &lt;a href=&#34;https://lucidmanager.org/tags/invisible-water-utility/&#34;&gt;applying Service-Dominant Logic&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;
The content of this article was recently discussed in the &lt;a href=&#34;https://valuedrivendatascience.com/68&#34;&gt;Value Driven Data Science podcast&lt;/a&gt; by Genevieve Hayes.&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-1&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-1&#34;&gt;
Introducing the Insights as a Service Framework
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-1&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;Technology managers regularly apply the acronym &amp;#34;PPT&amp;#34; (People, Process, and Technology) as a mnemonic. The &lt;a href=&#34;https://www.forbes.com/councils/forbestechcouncil/2022/12/29/is-the-60-year-old-people-process-technology-framework-still-useful/&#34;&gt;golden triangle of technology management&lt;/a&gt; dates back to the early 1960s when Harold Leavitt developed his Diamond Model. Bruce Schneier modified and popularised the tool in the 1990s.&lt;/p&gt;
&lt;p&gt;
From a service delivery perspective, we need to add some more dimensions. The &lt;a href=&#34;https://www.marketing91.com/service-marketing-mix/&#34;&gt;Services Marketing Mix&lt;/a&gt;, first developed by Bernard Booms and Mary Bitner in 1981, employs seven dimensions as a model for delivering services. A marketing mix is not a model that guarantees success. You need to use it as a heuristic device to think more deeply about the service your team provides.&lt;/p&gt;
&lt;p&gt;
These two models conceptually overlap, which leads to the IaaS framework. Insights as a Service recognises the importance of the core elements of PPT and adds elements from the services marketing mix. This combination leads to six dimensions: Product, Price, Promotion, People, Process, and Platform (technology is renamed for a lovely alliteration of six Ps).&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/data-science/six-ps-of-insights-as-a-service.jpg&#34; alt=&#34;Six Ps of delivering Insights as a Service&#34; title=&#34;Six Ps of delivering Insights as a Service&#34; width=&#34;600&#34;/&gt;
&lt;figcaption&gt;
Six Ps of delivering Insights as a Service.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
Insights as a Service (IaaS) is in most cases used exclusively in a B2B (&lt;a href=&#34;https://en.wikipedia.org/wiki/Business-to-business&#34;&gt;Business-to-Business&lt;/a&gt;) context. In this scenario a commercial service provider generates insights for a client using a cloud-based platform. This article reinterprets the IaaS model to include contexts where an internal data team provides electronic services to colleges (&lt;a href=&#34;https://en.wikipedia.org/wiki/Business-to-employee&#34;&gt;B2E, Business-to-Employee&lt;/a&gt;).&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-2&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-2&#34;&gt;
The Insights as a Service Framework
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-2&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The remainder of this article briefly discusses the significance of each of these framework dimensions and their practical relevance.&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-3&#34; class=&#34;outline-3&#34;&gt;
&lt;h3 id=&#34;headline-3&#34;&gt;
Product
&lt;/h3&gt;
&lt;div id=&#34;outline-text-headline-3&#34; class=&#34;outline-text-3&#34;&gt;
&lt;p&gt;The data product, which is, in most cases, a report, either static or interactive, is the vehicle that delivers value to the end-user. We can use an analogy from architecture by following the Vitruvian rules for buildings and translating them to our context:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;em&gt;Sound&lt;/em&gt;: Is the analysis mathematically and statistically correct?&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Useful&lt;/em&gt;: Can the client generate insights from it?&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Aesthetic&lt;/em&gt;: Is the data product well-designed and, as such, easy to understand? &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This previous article about &lt;a href=&#34;https://lucidmanager.org/data-science/strategic-data-science/&#34;&gt;strategic data science&lt;/a&gt; discusses this model of good data science in more depth, which is also explained in detail in my book on the topic.&lt;/p&gt;
&lt;p&gt;

  &lt;div class=&#34;box&#34;&gt;
    &lt;div class=&#34;media&#34;&gt;
      &lt;figure class=&#34;media-left&#34;&gt;
        &lt;p class=&#34;image is-128x128&#34;&gt;
          &lt;img src=&#34;https://lucidmanager.org/images/books/2019_sds.jpg&#34; alt = &#34;Principles of Strategic Data Science&#34; title = &#34;Principles of Strategic Data Science&#34;&gt;
        &lt;/p&gt;
      &lt;/figure&gt;
      &lt;div class=&#34;media-content&#34;&gt;
        &lt;div class=&#34;content&#34;&gt;
          &lt;p class=&#34;is-size-5 has-text-weight-bold&#34;&gt;Principles of Strategic Data Science&lt;/p&gt;
          &lt;p class=&#34;mb-2&#34;&gt;Principles of Strategic Data Science helps you join the dots between mathematics, programming, and business analysis. With a unique approach that bridges the gap between mathematics and computer science, this book takes you through the entire data science pipeline.&lt;/p&gt;
          &lt;div class=&#34;buttons&#34;&gt;
            &lt;a class=&#34;button is-info&#34; href=&#34;https://www.packtpub.com/en-us/product/principles-of-strategic-data-science-9781838985509&#34; target=&#34;_blank&#34;&gt;
              &lt;span class=&#34;icon&#34;&gt;
                &lt;i class=&#34;fas fa-shopping-cart&#34;&gt;&lt;/i&gt;
              &lt;/span&gt;
              &lt;span&gt;Packt Publishing&lt;/span&gt;
            &lt;/a&gt;
          &lt;/div&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/p&gt;
&amp;nbsp;
&lt;p&gt;
The value of a service product, in simplest terms, is the difference between the benefits and the cost. The product provides the benefits of the value equation, visualised below. While the product delivers value to the customer, there is also a cost, which is the next dimension in the framework.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/data-science/value-equation.png&#34; alt=&#34;The value equation, balancing benefits and cost&#34; title=&#34;The value equation, balancing benefits and cost&#34; width=&#34;300&#34;/&gt;
&lt;figcaption&gt;
The value equation, balancing benefits and cost.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-4&#34; class=&#34;outline-3&#34;&gt;
&lt;h3 id=&#34;headline-4&#34;&gt;
Price
&lt;/h3&gt;
&lt;div id=&#34;outline-text-headline-4&#34; class=&#34;outline-text-3&#34;&gt;
&lt;p&gt;Whether you are an internal data science team or a commercial service provider that assists other organisations with creating value from data, all development has a price. However, the monetary cost of a data product is only one dimension of the total Price equation.&lt;/p&gt;
&lt;p&gt;
Firstly, consumers of services incur a time cost. The time cost of a service is the amount of time a consumer (in our case, the user of a data product) spends using the tool. The time we spend using a service is an opportunity cost because we could be doing something else.&lt;/p&gt;
&lt;p&gt;
The time cost also extends to the development phase as the input from end users is essential. A data professional relies on the detailed, contextual knowledge of the end-user to create insights. &lt;/p&gt;
&lt;p&gt;
An extension of the time cost to use a service is the mental cost. How much mental effort does an end-user need to use to understand the output of your data product? The cognitive cost of consuming insights can be minimised in two ways. Firstly, the design of the report needs to be aesthetic. Not in the sense of beatification, but in making the end product easy to understand.&lt;/p&gt;
&lt;p&gt;
The mental cost to users of a report depends a lot on their data literacy. The advanced specialisation of data science has created a knowledge gap between subject-matter experts who use the report and the advanced statistical methods used by analysts. Reducing this knowledge gap requires the service provider to educate their customers, which falls under the next dimension of the IaaS framework.&lt;/p&gt;
&amp;nbsp;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-5&#34; class=&#34;outline-3&#34;&gt;
&lt;h3 id=&#34;headline-5&#34;&gt;
Promotion
&lt;/h3&gt;
&lt;div id=&#34;outline-text-headline-5&#34; class=&#34;outline-text-3&#34;&gt;
&lt;p&gt;When working in a B2B environment, the necessity for promotion is easily understood. However, promotion is also essential for internal teams working in B2E.&lt;/p&gt;
&lt;p&gt;
A data science service provider is often &lt;a href=&#34;https://lucidmanager.org/tags/invisible-water-utility/&#34;&gt;invisible to the consumer&lt;/a&gt; of its services. Being invisible is a good thing because it means our data products are working flawlessly, but this great uptime also comes at a cost, as your services might be taken for granted. The most effective way to combat invisibility is promotion.&lt;/p&gt;
&lt;p&gt;
For teams providing services to external entities (B2B), promotion is an essential business activity. However, this also applies to internal data teams. In my workplace, we ensure that our colleagues have regular visibility of the tools we develop through intranet articles and other internal communication means. These promotional activities ensure that management understands the relevance of the team and improves data literacy across the organisation.&lt;/p&gt;
&lt;p&gt;
To promote data science skills in my industry, I wrote a textbook for water professionals who want to &lt;a href=&#34;https://lucidmanager.org/data-science/spreadsheets-for-data-science/&#34;&gt;move away from their spreadsheets&lt;/a&gt;, the content of which I also deliver as a course. One objective of this course is to equip water professionals with the skills to communicate effectively with data scientists. Increasing data literacy among those who work with data scientists is essential to ensure the full value of our services is realised.&lt;/p&gt;
&lt;p&gt;

  &lt;div class=&#34;box&#34;&gt;
    &lt;div class=&#34;media&#34;&gt;
      &lt;figure class=&#34;media-left&#34;&gt;
        &lt;p class=&#34;image is-128x128&#34;&gt;
          &lt;img src=&#34;https://lucidmanager.org/images/books/2023_ds4wu.jpg&#34; alt = &#34;Data Science for Water Utilities&#34; title = &#34;Data Science for Water Utilities&#34;&gt;
        &lt;/p&gt;
      &lt;/figure&gt;
      &lt;div class=&#34;media-content&#34;&gt;
        &lt;div class=&#34;content&#34;&gt;
          &lt;p class=&#34;is-size-5 has-text-weight-bold&#34;&gt;Data Science for Water Utilities&lt;/p&gt;
          &lt;p class=&#34;mb-2&#34;&gt;Data Science for Water Utilities published by CRC Press is an applied, practical guide that shows water professionals how to use data science to solve urban water management problems using the R language for statistical computing.&lt;/p&gt;
          &lt;div class=&#34;buttons&#34;&gt;
            &lt;a class=&#34;button is-info&#34; href=&#34;https://routledge.com/9781032354545&#34; target=&#34;_blank&#34;&gt;
              &lt;span class=&#34;icon&#34;&gt;
                &lt;i class=&#34;fas fa-shopping-cart&#34;&gt;&lt;/i&gt;
              &lt;/span&gt;
              &lt;span&gt;Routledge&lt;/span&gt;
            &lt;/a&gt;
            &lt;a class=&#34;button is-info&#34; href=&#34;https://www.amazon.com/Data-Science-Water-Utilities-Chapman/dp/1032354550&#34; target=&#34;_blank&#34;&gt;
              &lt;span class=&#34;icon&#34;&gt;
                &lt;i class=&#34;fas fa-shopping-cart&#34;&gt;&lt;/i&gt;
              &lt;/span&gt;
              &lt;span&gt;Amazon&lt;/span&gt;
            &lt;/a&gt;
          &lt;/div&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
Another important aspect of providing valuable data services is that your client knows the team. Don&amp;#39;t hide your experts behind the veil of technology. This thought segways nicely onto the next dimension of the framework.&lt;/p&gt;
&amp;nbsp;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-6&#34; class=&#34;outline-3&#34;&gt;
&lt;h3 id=&#34;headline-6&#34;&gt;
People
&lt;/h3&gt;
&lt;div id=&#34;outline-text-headline-6&#34; class=&#34;outline-text-3&#34;&gt;
&lt;p&gt;Enough has been said and written about the technical requirements of being a data scientist. The &lt;a href=&#34;http://drewconway.com/&#34;&gt;Conway Venn diagram&lt;/a&gt; is a widely cited model of the technical skills required for data science. In this model, a data scientist is somebody who understands the subject matter under consideration in mathematical terms and writes computer code to solve problems.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/data-science/autodesk-water-unicorns.jpg&#34; alt=&#34;The Data Science Venn Diagram&#34; title=&#34;The Data Science Venn Diagram&#34; width=&#34;300&#34;/&gt;
&lt;figcaption&gt;
The Data Science Venn Diagram.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
Many alternatives have been proposed that incorporate soft skills into this diagram, and the Insights as a Service Framework also does so. Especially with the steady advancement of AI developments, non-technical skills will become increasingly crucial for a data scientist to thrive in this industry.&lt;/p&gt;
&lt;p&gt;
The importance of domain knowledge cannot be underestimated. The time price of a service suggests that the client needs to invest time in explaining the context of the data problem to the developer. However, the development team also needs to have sufficient domain knowledge to advise the client. I have been in many situations where an end-user knows what problem to solve but is unable to articulate what they want clearly. It is then the task of the service provider to understand their customer and provide them with what they need.&lt;/p&gt;
&lt;p&gt;
The well-worn &amp;#34;faster horses&amp;#34; marketing cliché also applies to providing &lt;em&gt;Insights as a service&lt;/em&gt;. A data service provider needs to be able to provide the client with a vision (in this analogy, a car) that solves or prevents problems.&lt;/p&gt;
&amp;nbsp;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-7&#34; class=&#34;outline-3&#34;&gt;
&lt;h3 id=&#34;headline-7&#34;&gt;
Process
&lt;/h3&gt;
&lt;div id=&#34;outline-text-headline-7&#34; class=&#34;outline-text-3&#34;&gt;
&lt;p&gt;The process of service delivery relates to your pipeline from ideation to production. Each organisation will have a governance model to decide which projects are executed and how they are implemented. &lt;/p&gt;
&lt;p&gt;
From a service perspective, your process needs to be seamless and easy to understand while also minimising the time the client has to invest in development and testing. Service Blueprinting is a commonly used technique to map a service. The primary difference between this approach and standard process maps is that a service blueprint also considers the customer&amp;#39;s perspective. A good service blueprint adds equal weight to the inputs of both the service provider and the service beneficiary.&lt;/p&gt;
&lt;p&gt;
Another important consideration in the service provision process is the method for measuring service quality. Service Quality goes beyond customer satisfaction and seeks to provide deep insight into which aspects of a service need improving. At a basic level, it consists of four dimensions:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;em&gt;Reliability&lt;/em&gt;: The ability to provide the service(s) accurately and dependably.&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Assurance&lt;/em&gt;: The ability to project trust and confidence.&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Tangibles&lt;/em&gt;: The physical or digital proof of the service provider&amp;#39;s status.&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Empathy&lt;/em&gt;: The ability to provide empathetic attention to customers.&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Responsiveness&lt;/em&gt;: The ability to provide timely services.&lt;/li&gt;
&lt;/ul&gt;
&amp;nbsp;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-8&#34; class=&#34;outline-3&#34;&gt;
&lt;h3 id=&#34;headline-8&#34;&gt;
Platform
&lt;/h3&gt;
&lt;div id=&#34;outline-text-headline-8&#34; class=&#34;outline-text-3&#34;&gt;
&lt;p&gt;Last but not least is the technology platform that you use for developing the data product. Data scientists use a complex web of applications to transform raw data into the final product. The data consumption platform is the interface between this technology stack and the end-user. &lt;/p&gt;
&lt;p&gt;
When choosing a data consumption platform, two factors are essential to consider: complexity and degrees of freedom.&lt;/p&gt;
&lt;p&gt;
Firstly, complexity relates to the level of coding required to create data products, ranging from no-code to high-code. No-code means that everything can be done with a mouse and a few keystrokes. High code implies that the developer needs to use scripting languages. For example, the R Shiny platform provides interactive web applications using R and Python. This package is a high-code solution that requires a substantial amount of training to utilise effectively. &lt;a href=&#34;https://www.knime.com/&#34;&gt;KNIME&lt;/a&gt; is a no-code solution with a node-based  graphical interface to build analytical &lt;/p&gt;
&lt;p&gt;
The less coding is involved, the more democratised access to the tool is, as more people can use it. A no or low-code platform can provide a self-service interface for subject-matter experts to analyse data without needing data science skills. Low-code platforms, such as &lt;a href=&#34;https://lucidmanager.org/data-science/spreadsheets-for-data-science/&#34;&gt;spreadsheets&lt;/a&gt; also have disadvantages, most importantly that they are often not reproducible and hard to reverse-engineer when updates are required.&lt;/p&gt;
&lt;p&gt;
The second dimension is the degree of freedom available to the end user. Tools such as Power BI offer a high degree of freedom, whereas a report in MS Word provides little to no freedom for the user. While Power BI or Tableau provide a &amp;#39;create-your-own-adventure&amp;#39; interface, other systems are more limited and provide &lt;/p&gt;
&lt;p&gt;
The choice of platform should align with the end-user&amp;#39;s needs. A no-code solution with a high level of freedom is not always best.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-9&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-9&#34;&gt;
Conclusion
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-9&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;This article summarises the &lt;em&gt;Insights-as-a-Service Framework&lt;/em&gt;. I am currently working on a book on the topic that will dive much deeper into the model, including practical examples.&lt;/p&gt;
&lt;p&gt;
&lt;a href=&#34;https://www.linkedin.com/in/peterprevos/&#34;&gt;Follow or connect with me on LinkedIn&lt;/a&gt; if you&amp;#39;d like to stay informed about my progress and learn how this concept develops.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;

      </description>
    </item>
    
    
    
    <item>
      <title>Project Euler 35: Circular Primes below One Million</title>
      <link>https://lucidmanager.org/data-science/project-euler-35/</link>
      <pubDate>Thu, 12 Jun 2025 00:00:00 +0000</pubDate>
      <author>Peter Prevos</author>
      <guid>https://lucidmanager.org/data-science/project-euler-35/</guid>
      <description>
	
&lt;p&gt;
Euler Problem 35 takes us back to the world of prime numbers, in particular &lt;a href=&#34;https://en.wikipedia.org/wiki/Circular_prime&#34;&gt;circular primes&lt;/a&gt;. The Online Encyclopedia of Integers (&lt;a href=&#34;https://oeis.org/A068652&#34;&gt;A068652&lt;/a&gt;) lists the first 46 decimal numbers for which every cyclic permutation is a prime.&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-1&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-1&#34;&gt;
Project Euler 35 Definition
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-1&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The number 197 is a decimal circular prime because all rotations of the digits: 197, 971, and 719, are themselves prime.&lt;/p&gt;
&lt;p&gt;
There are thirteen such primes below 100: 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, and 97.&lt;/p&gt;
&lt;p&gt;
&lt;a href=&#34;https://projecteuler.net/problem=35&#34;&gt;How many circular primes are there below one million&lt;/a&gt;?&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-2&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-2&#34;&gt;
Proposed Solution in R
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-2&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;This code reuses the function that strips all digits for a number, presented in &lt;a href=&#34;https://lucidmanager.org/data-science/project-euler-30/&#34;&gt;Euler Problem 30&lt;/a&gt;. The solution for this problem also use the &lt;code class=&#34;verbatim&#34;&gt;is_prime&lt;/code&gt; function to test whether a number is a prime, developed for &lt;a href=&#34;https://lucidmanager.org/data-science/project-euler-7/&#34;&gt;Euler Problem 7&lt;/a&gt;. &lt;/p&gt;
&lt;p&gt;
The following auxiliary functions help to solve the problem:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;prime_sieve&lt;/code&gt;: Generate primes&lt;/li&gt;
&lt;li&gt;&lt;code&gt;is_prime&lt;/code&gt;: Checks whether a number is prime&lt;/li&gt;
&lt;li&gt;&lt;code&gt;rotate_left&lt;/code&gt;: Rotates the a number&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The &lt;code&gt;is_circular_prime&lt;/code&gt; function first removes any numbers greater than 9 that contains either a 0, 2, 4, 5, 6 or 8, as at least one rotation will be &lt;a href=&#34;https://t5k.org/glossary/page.php?sort=CircularPrime&#34;&gt;divisible by either 2 or 5&lt;/a&gt;, which drastically reduces the search space.&lt;/p&gt;
&lt;p&gt;
The main program tests each of the 78,498 primes below one million and counts the number of circular ones.&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://github.com/pprevos/ProjectEuler/&#34; target=&#34;_blank&#34;
   title=&#34;Download ProjectEuler from GitHub&#34;
   alt=&#34;Download ProjectEuler from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;ProjectEuler&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  prime_sieve &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;function&lt;/span&gt;(x) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (x &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;) &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;return&lt;/span&gt;(&lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;NULL&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (x &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;) &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;return&lt;/span&gt;(n)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    l &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;x
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    i &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    p &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;while&lt;/span&gt; (p^2 &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;=&lt;/span&gt; x) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      l &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; l[l &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; p &lt;span style=&#34;color:#333&#34;&gt;|&lt;/span&gt; l &lt;span style=&#34;color:#333&#34;&gt;%%&lt;/span&gt; p&lt;span style=&#34;color:#333&#34;&gt;!=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      i &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; i &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      p &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; l[i]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;return&lt;/span&gt;(l)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  is_prime &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;function&lt;/span&gt;(x) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (x &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;) &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;return&lt;/span&gt;(&lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;FALSE&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (x &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;|&lt;/span&gt; x &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;3&lt;/span&gt;) &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;return&lt;/span&gt;(&lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;TRUE&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    primes &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;prime_sieve&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ceiling&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sqrt&lt;/span&gt;(x)))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;return&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;all&lt;/span&gt;(x &lt;span style=&#34;color:#333&#34;&gt;%%&lt;/span&gt; primes &lt;span style=&#34;color:#333&#34;&gt;!=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  x &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;101&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  rotate_left &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;function&lt;/span&gt;(x) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (x &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt;) &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;return&lt;/span&gt;(x)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    n &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;floor&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;log10&lt;/span&gt;(x)) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    first_digit &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; x &lt;span style=&#34;color:#333&#34;&gt;%/%&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;^&lt;/span&gt;(n &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    rest &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; x &lt;span style=&#34;color:#333&#34;&gt;%%&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;^&lt;/span&gt;(n &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    rotated &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; rest &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; first_digit
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;return&lt;/span&gt;(rotated)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  is_circular_prime &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;function&lt;/span&gt;(x) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (x &lt;span style=&#34;color:#333&#34;&gt;&amp;gt;&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;9&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;&amp;amp;&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;any&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;grepl&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;[024568]&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;as.character&lt;/span&gt;(x)))) &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;return&lt;/span&gt;(&lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;FALSE&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    n &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;floor&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;log10&lt;/span&gt;(x)) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    p &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;vector&lt;/span&gt;(length &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; n)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;for&lt;/span&gt; (r &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;in&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;n) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      p[r] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;is_prime&lt;/span&gt;(x)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (&lt;span style=&#34;color:#333&#34;&gt;!&lt;/span&gt;p[r] ) &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;return&lt;/span&gt;(&lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;FALSE&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      x &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;rotate_left&lt;/span&gt;(x)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;return&lt;/span&gt;(&lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;TRUE&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  primes &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;prime_sieve&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1E6&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;system.time&lt;/span&gt;({
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    circular_primes &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; primes&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;[sapply&lt;/span&gt;(primes, is_circular_prime)]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;print&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;length&lt;/span&gt;(circular_primes))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  })&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
&lt;a href = &#34;https://www.r-bloggers.com/&#34; target=&#34;_blank&#34; title=&#34;Proudly associated with R-Bloggers&#34;&gt;
  &lt;button class=&#34;button is-link is-medium&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-r-project&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span&gt;As seen on R Bloggers&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;

      </description>
    </item>
    
    
    
    <item>
      <title>Mapping the Ancient World: A Digital Odyssey through Ptolemy&#39;s Geography</title>
      <link>https://lucidmanager.org/data-science/mapping-ptolemy-geography/</link>
      <pubDate>Fri, 17 Nov 2023 00:00:00 +0000</pubDate>
      <author>Peter Prevos</author>
      <guid>https://lucidmanager.org/data-science/mapping-ptolemy-geography/</guid>
      <description>
	
&lt;p&gt;
In the chronicles of cartographic history, few works have captivated the imagination, such as Claudius Ptolemy&amp;#39;s &amp;#34;&lt;a href=&#34;https://en.wikipedia.org/wiki/Geography_(Ptolemy)&#34;&gt;Geography&lt;/a&gt;&amp;#34;. This monumental second-century creation is the oldest fully preserved theoretical treatise on cartography, written almost 2000 years ago. This article discusses Mapping Ptolemy&amp;#39;s Geography with &lt;em&gt;ggplot2&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;
His cartography is not just a collection of maps of the known world in Hellenic times. Ptolemy also provides latitudes and longitudes with fantastic accuracy. Interestingly, he wrote that the best way to store geographic information is as a collection of numbers because copying maps leads to distortions over time. Thus, he created one of the world&amp;#39;s first geographic information systems. His book contains the locations of thousands of towns, rivers, mountains and other points of interest.&lt;/p&gt;
&lt;p&gt;
Unfortunately, no freely available database of these coordinates exists in electronic format, but there are some &lt;a href=&#34;https://babel.hathitrust.org/cgi/pt?id=mdp.39015022717592&amp;amp;seq=7&#34;&gt;textual versions&lt;/a&gt;, such as the example below.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/geography/ptolemy-extract.jpg&#34; alt=&#34;Extract from Ptolemy&amp;#39;s Geography (Nobbe edition, 1843)&#34; title=&#34;Extract from Ptolemy&amp;#39;s Geography (Nobbe edition, 1843)&#34; width=&#34;600&#34;/&gt;
&lt;figcaption&gt;
Extract from Ptolemy&amp;#39;s Geography (Nobbe edition, 1843).
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
I thus set forth to scrape these sources and create a Ptolemaic geographic database. The code and data for this project are available on GitHub.&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://github.com/pprevos/geography/&#34; target=&#34;_blank&#34;
   title=&#34;Download geography from GitHub&#34;
   alt=&#34;Download geography from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;geography&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-1&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-1&#34;&gt;
Obtaining the Data
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-1&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The process begins with the scraping of Ptolemy’s text from &lt;a href=&#34;https://topostext.org/work/209&#34;&gt;ToposText&lt;/a&gt;, which is a treasure trove of classical literature. This website contains an almost complete transcription of Ptolemy&amp;#39;s tome. I saved the webpage to disk to prevent hitting the ToposText server too often. The source data is available on the Github repo in RDS format.&lt;/p&gt;
&lt;p&gt;
The website provides an accurate transcription of a large portion of Ptolemy&amp;#39;s work. Some typographical quirks and curious coordinates were standardised using regular expressions.&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(stringr)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(dplyr)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(ggplot2)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;# library(rvest)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;# Scrape the website and save it to disk&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;#&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;# url &amp;lt;- &amp;#34;https://topostext.org/work/209&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;#&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;# raw_html &amp;lt;- read_html(url) %&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;#     html_nodes(&amp;#34;p&amp;#34;) %&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;#     html_text()&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;# saveRDS(raw_html, &amp;#34;topostext.org-work-209.rds&amp;#34;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  raw &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;readRDS&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;topostext.org-work-209.rds&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;# Fix issues and typos&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  raw &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;str_replace_all&lt;/span&gt;(raw, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;\\. equator&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;0°0&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#888&#34;&gt;# Equator&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  raw &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;str_replace_all&lt;/span&gt;(raw, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;0°0 and&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;0°0&amp;#39;&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  raw &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;str_replace_all&lt;/span&gt;(raw, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;(\\d+.\\d+&amp;#39;) S &amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;-\\1&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#888&#34;&gt;# Southern hemisphere&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  raw &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;str_replace_all&lt;/span&gt;(raw, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;(\\d) c&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;\\1&amp;#39;&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  raw &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;str_replace_all&lt;/span&gt;(raw, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;(\\d) d&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;\\1&amp;#39; \\.&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  raw &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;str_replace_all&lt;/span&gt;(raw, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;(\\d) e&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;\\1&amp;#39; \\.&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  raw &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;str_replace_all&lt;/span&gt;(raw, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;5°00&amp;#39; . 3°00&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;45°15&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#888&#34;&gt;# Estimated lat&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  raw &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;str_replace_all&lt;/span&gt;(raw, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;4°00&amp;#39; . 3°00&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;43°00&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#888&#34;&gt;# Estimated lat&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  raw &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;str_replace_all&lt;/span&gt;(raw, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;(\\d+)&amp;#39;(\\d+)&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;\\1°\\2&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  raw &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;str_replace_all&lt;/span&gt;(raw, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;(Elateia . 51°38&amp;#39; )&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;\\1. 38°15&amp;#39;&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  raw &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;str_replace_all&lt;/span&gt;(raw, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;(Kambyses)&amp;#39;&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;\\1&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  raw &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;str_replace_all&lt;/span&gt;(raw, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;(Libison)&amp;#39;&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;\\1&amp;#34;&lt;/span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
The script then loops through all paragraphs that include coordinates and extracts the place names, longitude and latitude in degrees and minutes.&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;# Detect place names and coordinates&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  locs &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;str_split&lt;/span&gt;(raw&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;[str_detect&lt;/span&gt;(raw, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;°|º&amp;#34;&lt;/span&gt;)], &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;&amp;#39;&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  p_regex &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;\\b[A-Za-z ,:\\-]+\\b&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  c_regex &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;(-?[0-9]{1,3}[°|º][0-9]{1,3})&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  place &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;vector&lt;/span&gt;()
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  lon_raw &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;vector&lt;/span&gt;()
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  lat_raw &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;vector&lt;/span&gt;()
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  i &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;for&lt;/span&gt; (j &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;in&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;seq_along&lt;/span&gt;(locs)) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    t &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;unlist&lt;/span&gt;(locs[j])
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    n &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ceiling&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sum&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;str_detect&lt;/span&gt;(t,  &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;°|º&amp;#34;&lt;/span&gt;)) &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    k &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; i &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; (n &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    place[i&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;k] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;str_extract&lt;/span&gt;(t, p_regex)&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;[&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;n) &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    lon_txt &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;unlist&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;str_extract_all&lt;/span&gt;(t&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;[&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;n) &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;], c_regex))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    lat_txt &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;unlist&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;str_extract_all&lt;/span&gt;(t&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;[&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;n) &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;], c_regex))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;length&lt;/span&gt;(lat_txt) &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;) lat_txt &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;NA&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;length&lt;/span&gt;(lon_txt) &lt;span style=&#34;color:#333&#34;&gt;!=&lt;/span&gt; n) &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;stop&lt;/span&gt;(j)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    lon_raw[i&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;k] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; lon_txt
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    lat_raw[i&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;k] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; lat_txt
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    i &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; i &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; n
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    j &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; j &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
The next phase creates a table of coordinates by converting the coordinates into numeric values and cleaning the place names. The degrees and minutes of longitude and latitude are stored in separate variables. Regular expressions split the geographic coordinates (e.g. 14°20&amp;#39; . 54°30&amp;#39;) into four numbers (14, 20, 54, 30).&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;# Generate data frame&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  extract_nums &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;function&lt;/span&gt;(x, regex) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;as.numeric&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;unlist&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;str_extract_all&lt;/span&gt;(x, regex)))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  d_regex &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;(-?[0-9]+)(?=°|º)&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  m_regex &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;(?&amp;lt;=°|º)([0-9]+)&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  locations &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;tibble&lt;/span&gt;(place, lon_raw, lat_raw) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;mutate&lt;/span&gt;(lon &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;extract_nums&lt;/span&gt;(lon_raw, d_regex),
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;           lon_min &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;extract_nums&lt;/span&gt;(lon_raw, m_regex),
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;           lat &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;extract_nums&lt;/span&gt;(lat_raw, d_regex),
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;           lat_min &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;extract_nums&lt;/span&gt;(lat_raw, m_regex),
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;           place &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;str_squish&lt;/span&gt;(place),
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;           place &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;str_to_sentence&lt;/span&gt;(place)) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;select&lt;/span&gt;(placename &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; place, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;4&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;7&lt;/span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
The table of place names and their coordinates is available on the GitHub repository.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-2&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-2&#34;&gt;
Mapping Ptolemy&amp;#39;s Geography
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-2&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The visualisation was realised through the &lt;a href=&#34;https://ggplot2.tidyverse.org/reference/geom_map.html&#34;&gt;ggplot2 mapping geometry&lt;/a&gt;. Ptolemy&amp;#39;s ancient world is reborn; its cities and landmarks emerge from the mists of time on this digital canvas.&lt;/p&gt;
&lt;p&gt;
This map uses the Conic projection, similar to one of the projections used by Ptolemy himself. This projection distorts the southern part of Africa slightly.&lt;/p&gt;
&lt;p&gt;
The longitudes are corrected to better match them with modern coordinates. Ptolemy used the Canary Islands as the zero meridian of his coordinate system, which is 18 degrees west of the Greenwich meridian. Research also shows that Ptolemy&amp;#39;s longitudes are stretched by a factor of 1.428.&lt;/p&gt;
&lt;p&gt;
Ptolemy&amp;#39;s work might seem full of mistakes from our modern perspective, but in fact, &lt;a href=&#34;https://www.openculture.com/2021/03/why-every-world-map-is-wrong.html&#34;&gt;every world map we have ever produced is essentially wrong&lt;/a&gt;. Turning a globe into a two-dimensional object always results in distortions, no matter how hard you try.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/geography/ptolemaeus-geography-ii-vi.png&#34; alt=&#34;Mapping Ptolemaic coordinates on a modern map&#34; title=&#34;Mapping Ptolemaic coordinates on a modern map&#34;/&gt;
&lt;figcaption&gt;
Mapping Ptolemaic coordinates on a modern map.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#888&#34;&gt;# Visualise&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;world &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;map_data&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;world&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ggplot&lt;/span&gt;() &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;geom_map&lt;/span&gt;(data &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; world, map &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; world,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;             &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;aes&lt;/span&gt;(long, lat, map_id &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; region),
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            fill &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;darkgreen&amp;#34;&lt;/span&gt;, colour &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;lightgrey&amp;#34;&lt;/span&gt;, linewidth &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;.1&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;geom_point&lt;/span&gt;(data &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; locations,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;               &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;aes&lt;/span&gt;(x &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; (lon &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; lon_min &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;60&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;18&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1.428&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                   y &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; (lat &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; lat_min &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;60&lt;/span&gt;)),
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;               col &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;orange&amp;#34;&lt;/span&gt;, alpha &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0.7&lt;/span&gt;, size &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;.1&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;scale_x_continuous&lt;/span&gt;(limits &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;-15&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;70&lt;/span&gt;)) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;scale_y_continuous&lt;/span&gt;(limits &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;-10&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;60&lt;/span&gt;)) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;coord_map&lt;/span&gt;(projection &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;conic&amp;#34;&lt;/span&gt;, lat0 &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;37&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;labs&lt;/span&gt;(title &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Ptolemaeus, Geography (II-VI)&amp;#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;         subtitle &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Longitude correction: 0.7(lon - 18)&amp;#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;         caption &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Source: topostext.org/work/209&amp;#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;         x &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;NULL&lt;/span&gt;, y &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;NULL&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;theme_minimal&lt;/span&gt;(base_size &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;8&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;theme&lt;/span&gt;(panel.background &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;element_rect&lt;/span&gt;(fill &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;lightblue&amp;#34;&lt;/span&gt;, colour &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;NA&lt;/span&gt;))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
Ptolemy knew the Earth was spherical – a fact established by Greek philosophers well before his time. In his &lt;em&gt;Geographic&lt;/em&gt;, Ptolemy provides a cornucopia of coordinates, illustrating the known world from Britain to Southeast Asia and from the northern reaches of Scandinavia to below the equator. But their knowledge only covered a small part of the whole globe. &lt;/p&gt;
&lt;p&gt;
The Greeks blended mythology, philosophy, and early scientific speculation to theorise what lies beyond the world they mapped. They imagined a vast, unending ocean encircling the world. Beyond Oceanus, some thought, might be other worlds or unknown lands. The concept of &amp;#34;&lt;a href=&#34;https://lucidmanager.org/data-science/mapping-antipodes/&#34;&gt;antipodes&lt;/a&gt;&amp;#34; was also bandied about - these are  places where people&amp;#39;s feet are directly opposite to ours. Though the existence of such lands or peoples was often debated or outright denied.&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://www.r-bloggers.com/&#34; target=&#34;_blank&#34; title=&#34;Proudly associated with R-Bloggers&#34;&gt;
  &lt;button class=&#34;button is-link is-medium&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-r-project&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span&gt;As seen on R Bloggers&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;

      </description>
    </item>
    
    
    
    <item>
      <title>Analysing the Customer Experience</title>
      <link>https://lucidmanager.org/data-science/analysing-the-customer-experience/</link>
      <pubDate>Wed, 10 May 2023 00:00:00 +0000</pubDate>
      <author>Peter Prevos</author>
      <guid>https://lucidmanager.org/data-science/analysing-the-customer-experience/</guid>
      <description>
	
&lt;p&gt;
Summarising data through descriptive statistics such as averages, percentiles or variances does not add new knowledge but merely summarises what you already know. &lt;a href=&#34;https://dx.doi.org/10.1201/9781003326977-8&#34;&gt;This chapter&lt;/a&gt; of &lt;em&gt;Data Science for Water Utilities&lt;/em&gt; introduces basic inferential statistics, a set of methods for finding patterns in data to create new knowledge. Technical professionals often focus on the tangible aspects of services and measure performance in physical units. Technical data does, however, only tell part of the story of the customer experience. Analysing the customer experience requires different techniques to analyse data compared to physical processes. This chapter discusses how to use surveys to learn how customers experience services and demonstrates some techniques to explore this type of data using the R language. The learning objectives for this chapter are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Understand the principles of measuring the customer experience with surveys&lt;/li&gt;
&lt;li&gt;Assess the reliability of surveys with a correlation matrix&lt;/li&gt;
&lt;li&gt;Assess the validity of surveys using factor analysis&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;

  &lt;div class=&#34;box&#34;&gt;
    &lt;div class=&#34;media&#34;&gt;
      &lt;figure class=&#34;media-left&#34;&gt;
        &lt;p class=&#34;image is-128x128&#34;&gt;
          &lt;img src=&#34;https://lucidmanager.org/images/books/2023_ds4wu.jpg&#34; alt = &#34;Data Science for Water Utilities&#34; title = &#34;Data Science for Water Utilities&#34;&gt;
        &lt;/p&gt;
      &lt;/figure&gt;
      &lt;div class=&#34;media-content&#34;&gt;
        &lt;div class=&#34;content&#34;&gt;
          &lt;p class=&#34;is-size-5 has-text-weight-bold&#34;&gt;Data Science for Water Utilities&lt;/p&gt;
          &lt;p class=&#34;mb-2&#34;&gt;Data Science for Water Utilities published by CRC Press is an applied, practical guide that shows water professionals how to use data science to solve urban water management problems using the R language for statistical computing.&lt;/p&gt;
          &lt;div class=&#34;buttons&#34;&gt;
            &lt;a class=&#34;button is-info&#34; href=&#34;https://routledge.com/9781032354545&#34; target=&#34;_blank&#34;&gt;
              &lt;span class=&#34;icon&#34;&gt;
                &lt;i class=&#34;fas fa-shopping-cart&#34;&gt;&lt;/i&gt;
              &lt;/span&gt;
              &lt;span&gt;Routledge&lt;/span&gt;
            &lt;/a&gt;
            &lt;a class=&#34;button is-info&#34; href=&#34;https://www.amazon.com/Data-Science-Water-Utilities-Chapman/dp/1032354550&#34; target=&#34;_blank&#34;&gt;
              &lt;span class=&#34;icon&#34;&gt;
                &lt;i class=&#34;fas fa-shopping-cart&#34;&gt;&lt;/i&gt;
              &lt;/span&gt;
              &lt;span&gt;Amazon&lt;/span&gt;
            &lt;/a&gt;
          &lt;/div&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
The data and code used in this chapter are available on GitHub:&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://github.com/pprevos/r4h2o/&#34; target=&#34;_blank&#34;
   title=&#34;Download r4h2o from GitHub&#34;
   alt=&#34;Download r4h2o from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;r4h2o&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-1&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-1&#34;&gt;
Measuring Mental States
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-1&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;Suppose we like to know how somebody feels or what they think about something, such as the quality of service their water utility provides. In that case, we need to measure their mental state. There is currently no direct method to measure mental states, so we use indirect methods such as &lt;a href=&#34;https://en.wikipedia.org/wiki/Survey_methodology&#34;&gt;customer surveys&lt;/a&gt;. The basic idea is the way somebody completes a survey is causally related to their state of mind.&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;https://lucidmanager.org/images/r4h2o/mental-state.png&#34; alt=&#34;/images/r4h2o/mental-state.png&#34; title=&#34;/images/r4h2o/mental-state.png&#34; /&gt;&lt;/p&gt;
&lt;p&gt;
This chapter analysis a scale for consumer involvement where customers answered the following question bank of ten items:&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/hydroinformatics/involvement-pii-scale.jpg&#34; alt=&#34;Personal Involvement Inventory semantic-differential scale&#34; title=&#34;Personal Involvement Inventory semantic-differential scale&#34;/&gt;
&lt;figcaption&gt;
Personal Involvement Inventory semantic-differential scale (Zaichowsky 1994).
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-2&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-2&#34;&gt;
Reliability and Validity
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-2&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The basic principle of measuring a mental state using a survey is that we have a group of questions that relate to the same underlying mental construct. We need to evaluate reliability and validity to assess whether the survey items measure the underlying latent variable.&lt;/p&gt;
&lt;p&gt;
The reliability of a survey relates to the extent to which the answers to the survey items are internally consistent. The internal reliability of a survey can be informally assessed by reviewing the extent to which each of the items in the survey correlates with each other. The &lt;a href=&#34;https://personality-project.org/r/psych/&#34;&gt;psych package&lt;/a&gt; provides more advanced methods, such as Cronbach Alpha, to assess reliability.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/hydroinformatics/involvement-correlation.png&#34; alt=&#34;Correlation matrix for the Personal Involvement Index&#34; title=&#34;Correlation matrix for the Personal Involvement Index&#34; width=&#34;600&#34;/&gt;
&lt;figcaption&gt;
Correlation matrix for the Personal Involvement Index.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
The validity of a survey is broadly the extent to which it measures the underlying mental states. One of the methods to assess validity is factor analysis.&lt;/p&gt;
&lt;p&gt;
This previous article about using factor analysis to &lt;a href=&#34;https://lucidmanager.org/data-science/measuring-consumer-involvement/&#34;&gt;measure consumer involvement&lt;/a&gt; summarises how to undertake exploratory factor analysis.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-3&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-3&#34;&gt;
Analysing the Customer Experience Screencast
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-3&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;Chapter eight of &lt;em&gt;Data Science for Water Utilities&lt;/em&gt; explains the theory of measuring the customer experience and how to implement it in R in more detail. This screencast runs through the code in the customer experience chapter.&lt;/p&gt;
&lt;figure&gt;
&lt;div style=&#34;position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden;&#34;&gt;
      &lt;iframe allow=&#34;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share; fullscreen&#34; loading=&#34;eager&#34; referrerpolicy=&#34;strict-origin-when-cross-origin&#34; src=&#34;https://www.youtube.com/embed/6PVaIf5T8Qw?autoplay=0&amp;amp;controls=1&amp;amp;end=0&amp;amp;loop=0&amp;amp;mute=0&amp;amp;start=0&#34; style=&#34;position: absolute; top: 0; left: 0; width: 100%; height: 100%; border:0;&#34; title=&#34;YouTube video&#34;&gt;&lt;/iframe&gt;
    &lt;/div&gt;

&lt;figcaption&gt;
Analysing the Customer Experience screencast.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
The data and code used in this chapter are available on GitHub:&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://github.com/pprevos/r4h2o/&#34; target=&#34;_blank&#34;
   title=&#34;Download r4h2o from GitHub&#34;
   alt=&#34;Download r4h2o from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;r4h2o&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-4&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-4&#34;&gt;
Additional Resources
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-4&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;Surveys are a useful tool to analyse what customers think about tap water. Still, it can be better to move outside the formalities of the survey and use the customer&amp;#39;s own words. &lt;a href=&#34;https://lucidmanager.org/data-science/tap-water-sentiment-analysis/&#34;&gt;Sentiment analysis using tweets about tap water&lt;/a&gt; can provide illuminating insights.&lt;/p&gt;
&lt;p&gt;
My other book &lt;em&gt;Customer Experience Management for Water Utilities&lt;/em&gt; by IWA publishing delves deeper into using marketing theory for urban water supplies.&lt;/p&gt;
&lt;p&gt;

  &lt;div class=&#34;box&#34;&gt;
    &lt;div class=&#34;media&#34;&gt;
      &lt;figure class=&#34;media-left&#34;&gt;
        &lt;p class=&#34;image is-128x128&#34;&gt;
          &lt;img src=&#34;https://lucidmanager.org/images/books/2017b_wum.jpg&#34; alt = &#34;Customer Experience Management for Water Utilities: Marketing Urban Water Supply&#34; title = &#34;Customer Experience Management for Water Utilities: Marketing Urban Water Supply&#34;&gt;
        &lt;/p&gt;
      &lt;/figure&gt;
      &lt;div class=&#34;media-content&#34;&gt;
        &lt;div class=&#34;content&#34;&gt;
          &lt;p class=&#34;is-size-5 has-text-weight-bold&#34;&gt;Customer Experience Management for Water Utilities: Marketing Urban Water Supply&lt;/p&gt;
          &lt;p class=&#34;mb-2&#34;&gt;Practical framework for water utilities to become more focused on their customers following Service-Dominant Logic.&lt;/p&gt;
          &lt;div class=&#34;buttons&#34;&gt;
            &lt;a class=&#34;button is-info&#34; href=&#34;https://iwaponline.com/ebooks/book/327/Customer-Experience-Management-for-Water&#34; target=&#34;_blank&#34;&gt;
              &lt;span class=&#34;icon&#34;&gt;
                &lt;i class=&#34;fas fa-shopping-cart&#34;&gt;&lt;/i&gt;
              &lt;/span&gt;
              &lt;span&gt;IWA Publishing&lt;/span&gt;
            &lt;/a&gt;
          &lt;/div&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-5&#34; class=&#34;outline-3&#34;&gt;
&lt;h3 id=&#34;headline-5&#34;&gt;
Other Chapters
&lt;/h3&gt;
&lt;div id=&#34;outline-text-headline-5&#34; class=&#34;outline-text-3&#34;&gt;
&lt;p&gt;&lt;a href=&#34;https://lucidmanager.org/data-science/managing-dirty-data/&#34;&gt;Previous Chapter&lt;/a&gt;: &lt;em&gt;Managing Dirty Data&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;
&lt;a href=&#34;https://lucidmanager.org/data-science/basic-linear-regression/&#34;&gt;Next Chapter&lt;/a&gt;: &lt;em&gt;Basic Linear Regression&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;
Feel free to &lt;a href=&#34;https://lucidmanager.org/contact/&#34;&gt;contact me&lt;/a&gt; if you have any comments, suggestions or questions about this book.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

      </description>
    </item>
    
    
    
    <item>
      <title>Basic Linear Regression</title>
      <link>https://lucidmanager.org/data-science/basic-linear-regression/</link>
      <pubDate>Wed, 10 May 2023 00:00:00 +0000</pubDate>
      <author>Peter Prevos</author>
      <guid>https://lucidmanager.org/data-science/basic-linear-regression/</guid>
      <description>
	
&lt;p&gt;
Regression analysis is one of the most common methods to investigate relationships between variables. Understanding basic linear regression is the first step toward predictive analysis and machine learning. &lt;a href=&#34;https://dx.doi.org/10.1201/9781003326977-9&#34;&gt;This chapter&lt;/a&gt; of &lt;em&gt;Data Science for Water Utilities&lt;/em&gt; explores possible linear relationships between the responses in the customer survey and uses these results to explain the theory and practice of building and assessing linear models. The learning objectives for this chapter are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Understand the principles of linear regression&lt;/li&gt;
&lt;li&gt;Perform a linear regression of the customer survey data&lt;/li&gt;
&lt;li&gt;Assess the significance of a linear regression&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;

  &lt;div class=&#34;box&#34;&gt;
    &lt;div class=&#34;media&#34;&gt;
      &lt;figure class=&#34;media-left&#34;&gt;
        &lt;p class=&#34;image is-128x128&#34;&gt;
          &lt;img src=&#34;https://lucidmanager.org/images/books/2023_ds4wu.jpg&#34; alt = &#34;Data Science for Water Utilities&#34; title = &#34;Data Science for Water Utilities&#34;&gt;
        &lt;/p&gt;
      &lt;/figure&gt;
      &lt;div class=&#34;media-content&#34;&gt;
        &lt;div class=&#34;content&#34;&gt;
          &lt;p class=&#34;is-size-5 has-text-weight-bold&#34;&gt;Data Science for Water Utilities&lt;/p&gt;
          &lt;p class=&#34;mb-2&#34;&gt;Data Science for Water Utilities published by CRC Press is an applied, practical guide that shows water professionals how to use data science to solve urban water management problems using the R language for statistical computing.&lt;/p&gt;
          &lt;div class=&#34;buttons&#34;&gt;
            &lt;a class=&#34;button is-info&#34; href=&#34;https://routledge.com/9781032354545&#34; target=&#34;_blank&#34;&gt;
              &lt;span class=&#34;icon&#34;&gt;
                &lt;i class=&#34;fas fa-shopping-cart&#34;&gt;&lt;/i&gt;
              &lt;/span&gt;
              &lt;span&gt;Routledge&lt;/span&gt;
            &lt;/a&gt;
            &lt;a class=&#34;button is-info&#34; href=&#34;https://www.amazon.com/Data-Science-Water-Utilities-Chapman/dp/1032354550&#34; target=&#34;_blank&#34;&gt;
              &lt;span class=&#34;icon&#34;&gt;
                &lt;i class=&#34;fas fa-shopping-cart&#34;&gt;&lt;/i&gt;
              &lt;/span&gt;
              &lt;span&gt;Amazon&lt;/span&gt;
            &lt;/a&gt;
          &lt;/div&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
The data and code used in this chapter are available on GitHub:&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://github.com/pprevos/r4h2o/&#34; target=&#34;_blank&#34;
   title=&#34;Download r4h2o from GitHub&#34;
   alt=&#34;Download r4h2o from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;r4h2o&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-1&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-1&#34;&gt;
Principles of Linear Regression
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-1&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The purpose of a regression model is to predict one variable by measuring one or more other variables through a linear relationship. For example, predict water consumption based on the forecast temperature or investigate how customer complaints relate to the pressure level.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/r4h2o/regression-principles.png&#34; alt=&#34;/images/r4h2o/regression-principles.png&#34; title=&#34;Principles of linear regression&#34; alyt=&#34;Principles of linear regression&#34;/&gt;
&lt;figcaption&gt;
Principles of linear regression.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
The task of linear regression analysis is to find the line that minimises the difference between the observed values $(y)$ and the predicted values $(\hat{y})$.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-2&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-2&#34;&gt;
Linear Regressions in R
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-2&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The &lt;code&gt;lm()&lt;/code&gt; function is the regression workhorse that provides detailed output to assess the hypothesised relationship stored in a list.&lt;/p&gt;
&lt;p&gt;
Plotting the output of the linear model provides a detailed graphical assessment of the model.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;The first plot reviews the residuals versus the fitter (predicted) values. Ideally, the red line overlaps perfectly with the &lt;em&gt;x&lt;/em&gt;-axis.&lt;/li&gt;
&lt;li&gt;The QQ-Residuals Plot tests whether the distribution of the residuals is normal, which is the case when the observations are all on the diagonal line.&lt;/li&gt;
&lt;li&gt;The scale location plot tests for the assumption of homoscedasticity, which is the case when the red line is horizontal.&lt;/li&gt;
&lt;li&gt;Lastly, the leverage plot tests the data for outliers. The numbers relate to the rows with outliers. Leverage is the extent to which coefficients would change of these values are removed.&lt;/li&gt;
&lt;/ol&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/r4h2o/linear-regression-review.png&#34; alt=&#34;Linear regression graphical review&#34; title=&#34;Linear regression graphical review&#34;/&gt;
&lt;figcaption&gt;
Linear regression graphical review.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-3&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-3&#34;&gt;
Basic Linear Regression Screencast
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-3&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;Chapter nine of &lt;em&gt;Data Science for Water Utilities&lt;/em&gt; explains the theory of linear regression and how to implement it in R in more detail. This screencast runs through the code in the basic linear regression chapter.&lt;/p&gt;
&lt;figure&gt;
&lt;div style=&#34;position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden;&#34;&gt;
      &lt;iframe allow=&#34;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share; fullscreen&#34; loading=&#34;eager&#34; referrerpolicy=&#34;strict-origin-when-cross-origin&#34; src=&#34;https://www.youtube.com/embed/X0SRaZu9lp0?autoplay=0&amp;amp;controls=1&amp;amp;end=0&amp;amp;loop=0&amp;amp;mute=0&amp;amp;start=0&#34; style=&#34;position: absolute; top: 0; left: 0; width: 100%; height: 100%; border:0;&#34; title=&#34;YouTube video&#34;&gt;&lt;/iframe&gt;
    &lt;/div&gt;

&lt;figcaption&gt;
Basic Linear Regression Screencast.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
The data and code used in this chapter are available on GitHub:&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://github.com/pprevos/r4h2o/&#34; target=&#34;_blank&#34;
   title=&#34;Download r4h2o from GitHub&#34;
   alt=&#34;Download r4h2o from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;r4h2o&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-4&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-4&#34;&gt;
Additional Resources
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-4&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;&lt;a href=&#34;https://lucidmanager.org/data-science/introduction-to-machine-learning/&#34;&gt;Chapter 13&lt;/a&gt; discusses multiple linear regression.&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-5&#34; class=&#34;outline-3&#34;&gt;
&lt;h3 id=&#34;headline-5&#34;&gt;
Other Chapters
&lt;/h3&gt;
&lt;div id=&#34;outline-text-headline-5&#34; class=&#34;outline-text-3&#34;&gt;
&lt;p&gt;&lt;a href=&#34;https://lucidmanager.org/data-science/analysing-the-customer-experience/&#34;&gt;Previos Chapter&lt;/a&gt;: &lt;em&gt;Analysing the Customer Experience&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;
&lt;a href=&#34;https://lucidmanager.org/data-science/cluster-analysis-to-segment-customers/&#34;&gt;Next Chapter&lt;/a&gt;: &lt;em&gt;Clustering customers to define segments&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;
Feel free to &lt;a href=&#34;https://lucidmanager.org/contact/&#34;&gt;contact me&lt;/a&gt; if you have any comments, suggestions or questions about this book.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

      </description>
    </item>
    
    
    
    <item>
      <title>Basics of the R Language</title>
      <link>https://lucidmanager.org/data-science/basics-of-the-r-language/</link>
      <pubDate>Wed, 10 May 2023 00:00:00 +0000</pubDate>
      <author>Peter Prevos</author>
      <guid>https://lucidmanager.org/data-science/basics-of-the-r-language/</guid>
      <description>
	
&lt;p&gt;
&lt;a href=&#34;https://dx.doi.org/10.1201/9781003326977-2&#34;&gt;The second chapter&lt;/a&gt; of &lt;em&gt;Data Science for Water Utilities&lt;/em&gt; introduces the basic principles of the R language and RStudio to analyse and visualise data. Writing code is not only about using the correct syntax and the appropriate functions; this chapter also covers some issues regarding styling your code, making it easy to understand. This chapter concludes with a case study to measure water flow in an open channel using the Kindsvater-Carter formula. The learning objectives for this chapter are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Install R and RStudio and identify the different parts of the RStudio screen.&lt;/li&gt;
&lt;li&gt;Understand the principles of writing code to analyse data.&lt;/li&gt;
&lt;li&gt;Apply R code to solve a simple water problem.


   &lt;div class=&#34;box&#34;&gt;
     &lt;div class=&#34;media&#34;&gt;
       &lt;figure class=&#34;media-left&#34;&gt;
         &lt;p class=&#34;image is-128x128&#34;&gt;
           &lt;img src=&#34;https://lucidmanager.org/images/books/2023_ds4wu.jpg&#34; alt = &#34;Data Science for Water Utilities&#34; title = &#34;Data Science for Water Utilities&#34;&gt;
         &lt;/p&gt;
       &lt;/figure&gt;
       &lt;div class=&#34;media-content&#34;&gt;
         &lt;div class=&#34;content&#34;&gt;
           &lt;p class=&#34;is-size-5 has-text-weight-bold&#34;&gt;Data Science for Water Utilities&lt;/p&gt;
           &lt;p class=&#34;mb-2&#34;&gt;Data Science for Water Utilities published by CRC Press is an applied, practical guide that shows water professionals how to use data science to solve urban water management problems using the R language for statistical computing.&lt;/p&gt;
           &lt;div class=&#34;buttons&#34;&gt;
             &lt;a class=&#34;button is-info&#34; href=&#34;https://routledge.com/9781032354545&#34; target=&#34;_blank&#34;&gt;
               &lt;span class=&#34;icon&#34;&gt;
                 &lt;i class=&#34;fas fa-shopping-cart&#34;&gt;&lt;/i&gt;
               &lt;/span&gt;
               &lt;span&gt;Routledge&lt;/span&gt;
             &lt;/a&gt;
             &lt;a class=&#34;button is-info&#34; href=&#34;https://www.amazon.com/Data-Science-Water-Utilities-Chapman/dp/1032354550&#34; target=&#34;_blank&#34;&gt;
               &lt;span class=&#34;icon&#34;&gt;
                 &lt;i class=&#34;fas fa-shopping-cart&#34;&gt;&lt;/i&gt;
               &lt;/span&gt;
               &lt;span&gt;Amazon&lt;/span&gt;
             &lt;/a&gt;
           &lt;/div&gt;
         &lt;/div&gt;
       &lt;/div&gt;
     &lt;/div&gt;
   &lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The data and code used in this chapter are available on GitHub:&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://github.com/pprevos/r4h2o/&#34; target=&#34;_blank&#34;
   title=&#34;Download r4h2o from GitHub&#34;
   alt=&#34;Download r4h2o from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;r4h2o&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-1&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-1&#34;&gt;
Installing R and Rstudio
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-1&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The best way to use R is through an &lt;em&gt;Integrated Development Environment&lt;/em&gt; (IDE). This type of software helps you to write and manage code. An IDE typically comprises a source code editor, automation tools, and functionality to simplify crafting and running code. Of course, you can use R without the IDE, but it will be less user-friendly.&lt;/p&gt;
&lt;p&gt;
Several IDEs are available to help you write R code. RStudio by Posit is the most popular option.&lt;/p&gt;
&lt;p&gt;
This software is also an open-source project, with free and paid versions for companies that want to use advanced features and support services. RStudio can also work with other languages, such as SQL and Python. Follow these steps to install the required software:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Go to the R Project website: &lt;a href=&#34;https://cran.r-project.org/&#34;&gt;cran.r-project.org&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Download the base version for your operating system and install the software&lt;/li&gt;
&lt;li&gt;Go to the download page on the RStudio website: &lt;a href=&#34;https://posit.co/download/rstudio-desktop/&#34;&gt;posit.co&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Download the installer for the free desktop version and install the software&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Alternatively, you can sign up for a free and fully featured account to access RStudio’s cloud version (&lt;a href=&#34;https://posit.cloud/&#34;&gt;posit.cloud&lt;/a&gt;). This service gives you full access to R and RStudio in your browser without installing software. The free version provides enough hours of computing time to work through this book. You’ll have to pay for a subscription or install the desktop version if you need more time.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/r4h2o/rstudio.jpg&#34; alt=&#34;The RStudio IDE interface&#34; title=&#34;The RStudio IDE interface&#34;/&gt;
&lt;figcaption&gt;
The RStudio IDE interface.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;div id=&#34;outline-container-headline-2&#34; class=&#34;outline-3&#34;&gt;
&lt;h3 id=&#34;headline-2&#34;&gt;
Other R IDEs
&lt;/h3&gt;
&lt;div id=&#34;outline-text-headline-2&#34; class=&#34;outline-text-3&#34;&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;https://microsoft.github.io/RTVS-docs/installation.html&#34;&gt;R Tools for Visual Studio&lt;/a&gt;: Microsoft’s development environment.&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://rattle.togaware.com/&#34;&gt;Rattle&lt;/a&gt; is a popular graphical user interface for data mining in the R programming language.&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://ess.r-project.org/&#34;&gt;Emacs Speaks Statistics&lt;/a&gt;: For advanced computer users.&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-3&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-3&#34;&gt;
Principles of Writing Code
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-3&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;Writing code has a steep learning curve and can be both frustrating and rewarding at the same time. Here are some principles that will help you write good code that is easy to understand:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Use descriptive variable names. Don’t, for example, use &lt;code class=&#34;verbatim&#34;&gt;d&lt;/code&gt;, but &lt;code class=&#34;verbatim&#34;&gt;pipe-diameter&lt;/code&gt; or another descriptive name.&lt;/li&gt;
&lt;li&gt;Add plenty of whitespace in your code.&lt;/li&gt;
&lt;li&gt;Use comments starting with a hashtag: &lt;code class=&#34;verbatim&#34;&gt;#&lt;/code&gt; (comments are not evaluated)&lt;/li&gt;
&lt;li&gt;Follow a coding style guide, for example, the &lt;a href=&#34;https://style.tidyverse.org/&#34;&gt;Tidyverse Style Guide&lt;/a&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/r4h2o/r-learning-curve.jpg&#34; alt=&#34;The R Learning Curve can be steep and full of surprises&#34; title=&#34;The R Learning Curve can be steep and full of surprises&#34; width=&#34;400&#34;/&gt;
&lt;figcaption&gt;
The R Learning Curve can be steep and full of surprises.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-4&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-4&#34;&gt;
R is Meme Proof
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-4&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The basic arithmetic in the R language follows the BODMAS rules (Brackets, Order, Division, Multiplication, Addition, and Subtraction). R is thus meme-proof and can solve the silly mathematical ‘challenges’ spread on social media.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/r4h2o/memes.jpg&#34; alt=&#34;R is meme-proof and can solve arithmetic challenges&#34; title=&#34;R is meme-proof and can solve arithmetic challenges&#34; width=&#34;200&#34;/&gt;
&lt;figcaption&gt;
R is meme-proof and can solve arithmetic challenges.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-5&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-5&#34;&gt;
Channel Flow Case Study
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-5&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;Determining the flow in an open channel is usually achieved by measuring the water depth through a section with a known shape. Forcing the water through a given shape over a sharp weir creates a boundary condition that allows us to calculate the flow. A mathematical relationship determines the volume of water that passes through the channel. This case study uses a rectangular weir. A simplified version of the Kindsvater-Carter formula:&lt;/p&gt;
&lt;p&gt;
$$Q = \frac{2}{3} C_d \sqrt{2g} \\ bh^{\frac{3}{2}}$$&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;$Q$: Flow rate ($m^3/s$)&lt;/li&gt;
&lt;li&gt;$C_d$ : Discharge coefficient&lt;/li&gt;
&lt;li&gt;$g$: Gravitational acceleration (9.81 $m/s^2$ )&lt;/li&gt;
&lt;li&gt;$b$: Measured width of the notch [$m$]&lt;/li&gt;
&lt;li&gt;$h$: Upstream head above crest level [$m$]&lt;/li&gt;
&lt;/ul&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/r4h2o/rectangular-weir.png&#34; alt=&#34;Rectangular weir&#34; title=&#34;Rectangular weir&#34; width=&#34;400&#34;/&gt;
&lt;figcaption&gt;
Rectangular weir.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
Questions:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;What is the flow in $m^3/s$ when $h=100$&lt;/li&gt;
&lt;li&gt;What is the average flow when the level above the weir is 150, 136 and 75mm? &lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-6&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-6&#34;&gt;
Cheat Sheet
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-6&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;To help you remember the various functions discussed in the first five chapters of the book, a &lt;a href=&#34;https://lucidmanager.org/pdf/r4h2o-cheat-sheet.pdf&#34;&gt;cheat sheet&lt;/a&gt; is available.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/r4h2o/r4h2o-cheat-sheet.jpg&#34; alt=&#34;R4H2O Cheat Sheet&#34; title=&#34;R4H2O Cheat Sheet&#34; width=&#34;400&#34;/&gt;
&lt;figcaption&gt;
Data Science for Water Utilities Cheat Sheet.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-7&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-7&#34;&gt;
Basics of the R Language Screencast 
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-7&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;Chapter two of &lt;em&gt;Data Science for Water Utilities&lt;/em&gt; explains the principles of writing code to analyse data in more detail. This screencast runs through the code in this chapter.&lt;/p&gt;
&lt;figure&gt;
&lt;div style=&#34;position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden;&#34;&gt;
      &lt;iframe allow=&#34;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share; fullscreen&#34; loading=&#34;eager&#34; referrerpolicy=&#34;strict-origin-when-cross-origin&#34; src=&#34;https://www.youtube.com/embed/iYO7rbAeatU?autoplay=0&amp;amp;controls=1&amp;amp;end=0&amp;amp;loop=0&amp;amp;mute=0&amp;amp;start=0&#34; style=&#34;position: absolute; top: 0; left: 0; width: 100%; height: 100%; border:0;&#34; title=&#34;YouTube video&#34;&gt;&lt;/iframe&gt;
    &lt;/div&gt;

&lt;figcaption&gt;
Basics of the R Language.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
 The data and code used in this chapter are available on GitHub:&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://github.com/pprevos/r4h2o/&#34; target=&#34;_blank&#34;
   title=&#34;Download r4h2o from GitHub&#34;
   alt=&#34;Download r4h2o from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;r4h2o&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-8&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-8&#34;&gt;
Additional Resources
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-8&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;To help you remember the various functions discussed in the first five chapters of the book, a &lt;a href=&#34;https://lucidmanager.org/pdf/r4h2o-cheat-sheet.pdf&#34;&gt;cheat sheet&lt;/a&gt; is available.&lt;/p&gt;
&lt;p&gt;
&lt;a href=&#34;https://lucidmanager.org/tags/project-euler/&#34;&gt;Project Euler&lt;/a&gt; provides an excellent way to practice solving basic mathematical methods in the R language.&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-9&#34; class=&#34;outline-3&#34;&gt;
&lt;h3 id=&#34;headline-9&#34;&gt;
Other Chapters
&lt;/h3&gt;
&lt;div id=&#34;outline-text-headline-9&#34; class=&#34;outline-text-3&#34;&gt;
&lt;p&gt;&lt;a href=&#34;https://lucidmanager.org/data-science/r-for-water-utilities/&#34;&gt;Previous Chapter&lt;/a&gt;: &lt;em&gt;Introduction to Data Science for Water Utilities&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;
&lt;a href=&#34;https://lucidmanager.org/data-science/loading-and-exploring-water-quality-data/&#34;&gt;Next Chapter&lt;/a&gt;: &lt;em&gt;Loading and Exploring Water Quality Data&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;
Feel free to &lt;a href=&#34;https://lucidmanager.org/contact/&#34;&gt;contact me&lt;/a&gt; if you have any comments, suggestions or questions about this book.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

      </description>
    </item>
    
    
    
    <item>
      <title>Clustering Customers to Define Segments</title>
      <link>https://lucidmanager.org/data-science/cluster-analysis-to-segment-customers/</link>
      <pubDate>Wed, 10 May 2023 00:00:00 +0000</pubDate>
      <author>Peter Prevos</author>
      <guid>https://lucidmanager.org/data-science/cluster-analysis-to-segment-customers/</guid>
      <description>
	
&lt;p&gt;
The ideal form of customer service is personal attention, where the needs of each individual are met. Unfortunately, this level of service is more often than not impossible or too costly to achieve. Service providers, therefore, segment their customers into groups with similar characteristics. Cluster analysis to segment customers is a commonly used technique, which analyses and divides an unlabeled dataset into groups of observations with similar properties. &lt;a href=&#34;https://dx.doi.org/10.1201/9781003326977-10&#34;&gt;This chapter&lt;/a&gt; of &lt;em&gt;Data Science for Water Utilities&lt;/em&gt; shows how to detect patterns and define segments in customer data. The learning objectives for this chapter are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Understand the principles of customer segmentation&lt;/li&gt;
&lt;li&gt;Apply and interpret hierarchical cluster analysis&lt;/li&gt;
&lt;li&gt;Apply and interpret k-means clustering&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;

  &lt;div class=&#34;box&#34;&gt;
    &lt;div class=&#34;media&#34;&gt;
      &lt;figure class=&#34;media-left&#34;&gt;
        &lt;p class=&#34;image is-128x128&#34;&gt;
          &lt;img src=&#34;https://lucidmanager.org/images/books/2023_ds4wu.jpg&#34; alt = &#34;Data Science for Water Utilities&#34; title = &#34;Data Science for Water Utilities&#34;&gt;
        &lt;/p&gt;
      &lt;/figure&gt;
      &lt;div class=&#34;media-content&#34;&gt;
        &lt;div class=&#34;content&#34;&gt;
          &lt;p class=&#34;is-size-5 has-text-weight-bold&#34;&gt;Data Science for Water Utilities&lt;/p&gt;
          &lt;p class=&#34;mb-2&#34;&gt;Data Science for Water Utilities published by CRC Press is an applied, practical guide that shows water professionals how to use data science to solve urban water management problems using the R language for statistical computing.&lt;/p&gt;
          &lt;div class=&#34;buttons&#34;&gt;
            &lt;a class=&#34;button is-info&#34; href=&#34;https://routledge.com/9781032354545&#34; target=&#34;_blank&#34;&gt;
              &lt;span class=&#34;icon&#34;&gt;
                &lt;i class=&#34;fas fa-shopping-cart&#34;&gt;&lt;/i&gt;
              &lt;/span&gt;
              &lt;span&gt;Routledge&lt;/span&gt;
            &lt;/a&gt;
            &lt;a class=&#34;button is-info&#34; href=&#34;https://www.amazon.com/Data-Science-Water-Utilities-Chapman/dp/1032354550&#34; target=&#34;_blank&#34;&gt;
              &lt;span class=&#34;icon&#34;&gt;
                &lt;i class=&#34;fas fa-shopping-cart&#34;&gt;&lt;/i&gt;
              &lt;/span&gt;
              &lt;span&gt;Amazon&lt;/span&gt;
            &lt;/a&gt;
          &lt;/div&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
The data and code used in this chapter are available on GitHub:&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://github.com/pprevos/r4h2o/&#34; target=&#34;_blank&#34;
   title=&#34;Download r4h2o from GitHub&#34;
   alt=&#34;Download r4h2o from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;r4h2o&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-1&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-1&#34;&gt;
Principles of Customer Segmentation
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-1&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The ideal situation for customer-centric services is that each customer receives individual attention. For large organisations, giving each customer individual attention is very costly and treating everybody the same is not very good either. Customer segmentation helps service providers group customers into segments with similar needs.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;em&gt;Demographic&lt;/em&gt;: Age, gender, income, education, ethnicity.&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Behavioural&lt;/em&gt;: Purchasing habits, spending habits, water consumption.&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Psychographic&lt;/em&gt;: Interests, lifestyle, motivations, and water-related priorities.&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Geographic&lt;/em&gt;: Town, postal code, water system.&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-2&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-2&#34;&gt;
Hierarchical Cluster Analysis
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-2&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;This example contains data from ten hypothetical customers (A–J). The first data dimension in the test data is the average annual water consumption, and the second is the size of the land on which the house resides. The clusters should be easily visible by viewing the image.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/r4h2o/cluster-analysis-example.png&#34; alt=&#34;Cluster analysis example&#34; title=&#34;Cluster analysis example&#34; width=&#34;500&#34;/&gt;
&lt;figcaption&gt;
Cluster analysis example.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
Hierarchical cluster analysis is a deterministic method to find the relevant clusters. This method reviews all possible combinations of data points and can thus be problematic when analysing large amounts of data. This tree diagram shows how all the points in the chart relate to each other.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/r4h2o/cluster-analysis-hierarchical.png&#34; alt=&#34;Hierarchical clustering example&#34; title=&#34;Hierarchical clustering example&#34; width=&#34;500&#34;/&gt;
&lt;figcaption&gt;
Hierarchical clustering example.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-3&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-3&#34;&gt;
k-means clustering
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-3&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The &lt;em&gt;k&lt;/em&gt;-means method uses a stochastic approach, which means that the outcome is not always the same when some clusters are in doubt. But, this method can digest much larger data sets than the hierarchical method. Another difference with the first method is that in &lt;em&gt;k&lt;/em&gt;-means, you need to specify the number of clusters before the analysis starts.&lt;/p&gt;
&lt;p&gt;
The elbow method visualises the Within-Clusters Sum of Squares for the number of clusters. The location where the graph has the smallest angle is most likely the ideal number of clusters.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/r4h2o/cluster-analysis-elbow.png&#34; alt=&#34;k-Means cluster analysis elbow method&#34; title=&#34;k-Means cluster analysis elbow method&#34; width=&#34;500&#34;/&gt;
&lt;figcaption&gt;
k-Means cluster analysis elbow method.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-4&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-4&#34;&gt;
Interpreting cluster analysis
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-4&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;Clustering analysis methods are a form of unsupervised machine learning. The computer detects patterns in the data but cannot relate them to meaning. The results of a cluster analysis require human interpretation to link it to the context of the data. &lt;/p&gt;
&lt;p&gt;
In this simple example, we could name the two clusters of households with and those without a garden.&lt;/p&gt;
&lt;p&gt;
In reality, cluster analysis occurs with many more variables, as in these simplified examples. The book provides a small case study that also uses categorical data.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-5&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-5&#34;&gt;
Cluster Analysis to Segment Customers Screencast
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-5&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;Chapter ten of &lt;em&gt;Data Science for Water Utilities&lt;/em&gt; explains the principles of cluster analysis for customer segmentation in more detail. This screencast demonstrates how to undertake cluster analysis to segment customers using the code explained in the book.&lt;/p&gt;
&lt;figure&gt;
&lt;div style=&#34;position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden;&#34;&gt;
      &lt;iframe allow=&#34;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share; fullscreen&#34; loading=&#34;eager&#34; referrerpolicy=&#34;strict-origin-when-cross-origin&#34; src=&#34;https://www.youtube.com/embed/4PwcB2NZX9M?autoplay=0&amp;amp;controls=1&amp;amp;end=0&amp;amp;loop=0&amp;amp;mute=0&amp;amp;start=0&#34; style=&#34;position: absolute; top: 0; left: 0; width: 100%; height: 100%; border:0;&#34; title=&#34;YouTube video&#34;&gt;&lt;/iframe&gt;
    &lt;/div&gt;

&lt;figcaption&gt;
Clustering Customers to Define Segments.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
The data and code used in this chapter are available on GitHub:&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://github.com/pprevos/r4h2o/&#34; target=&#34;_blank&#34;
   title=&#34;Download r4h2o from GitHub&#34;
   alt=&#34;Download r4h2o from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;r4h2o&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-6&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-6&#34;&gt;
Additional Resources
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-6&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;My book &lt;em&gt;Customer Experience Management for Water Utilities&lt;/em&gt; by IWA publishing delves deeper into using marketing theory for urban water supplies.&lt;/p&gt;
&lt;p&gt;

  &lt;div class=&#34;box&#34;&gt;
    &lt;div class=&#34;media&#34;&gt;
      &lt;figure class=&#34;media-left&#34;&gt;
        &lt;p class=&#34;image is-128x128&#34;&gt;
          &lt;img src=&#34;https://lucidmanager.org/images/books/2017b_wum.jpg&#34; alt = &#34;Customer Experience Management for Water Utilities: Marketing Urban Water Supply&#34; title = &#34;Customer Experience Management for Water Utilities: Marketing Urban Water Supply&#34;&gt;
        &lt;/p&gt;
      &lt;/figure&gt;
      &lt;div class=&#34;media-content&#34;&gt;
        &lt;div class=&#34;content&#34;&gt;
          &lt;p class=&#34;is-size-5 has-text-weight-bold&#34;&gt;Customer Experience Management for Water Utilities: Marketing Urban Water Supply&lt;/p&gt;
          &lt;p class=&#34;mb-2&#34;&gt;Practical framework for water utilities to become more focused on their customers following Service-Dominant Logic.&lt;/p&gt;
          &lt;div class=&#34;buttons&#34;&gt;
            &lt;a class=&#34;button is-info&#34; href=&#34;https://iwaponline.com/ebooks/book/327/Customer-Experience-Management-for-Water&#34; target=&#34;_blank&#34;&gt;
              &lt;span class=&#34;icon&#34;&gt;
                &lt;i class=&#34;fas fa-shopping-cart&#34;&gt;&lt;/i&gt;
              &lt;/span&gt;
              &lt;span&gt;IWA Publishing&lt;/span&gt;
            &lt;/a&gt;
          &lt;/div&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-7&#34; class=&#34;outline-3&#34;&gt;
&lt;h3 id=&#34;headline-7&#34;&gt;
Other Chapters
&lt;/h3&gt;
&lt;div id=&#34;outline-text-headline-7&#34; class=&#34;outline-text-3&#34;&gt;
&lt;p&gt;&lt;a href=&#34;https://lucidmanager.org/data-science/basic-linear-regression/&#34;&gt;Previous Chapter&lt;/a&gt;: &lt;em&gt;Basic Linear Regression&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;
&lt;a href=&#34;https://lucidmanager.org/data-science/working-with-dates-and-times-in-r/&#34;&gt;Next Chapter&lt;/a&gt;: &lt;em&gt;Working with Dates and Times&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;
Feel free to &lt;a href=&#34;https://lucidmanager.org/contact/&#34;&gt;contact me&lt;/a&gt; if you have any comments, suggestions or questions about this book.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

      </description>
    </item>
    
    
    
    <item>
      <title>Descriptive Statistics in Water Quality</title>
      <link>https://lucidmanager.org/data-science/descriptive-statistics-in-water-quality/</link>
      <pubDate>Wed, 10 May 2023 00:00:00 +0000</pubDate>
      <author>Peter Prevos</author>
      <guid>https://lucidmanager.org/data-science/descriptive-statistics-in-water-quality/</guid>
      <description>
	
&lt;p&gt;
The first step in telling data stories is summarising the available data. Descriptive statistics are tools that use a single number to describe a distribution. Analysts calculate averages, medians, percentiles and other statistical summaries to represent a data set&amp;#39;s tendency, spread, positions and shape. Statisticians have defined five types of descriptive statistics: frequency, central tendency, position, dispersion and shape. &lt;a href=&#34;https://dx.doi.org/10.1201/9781003326977-4&#34;&gt;This chapter&lt;/a&gt; of &lt;em&gt;Data Science for Water Utilities&lt;/em&gt; shows how to calculate descriptive statistics using synthetic water quality data from an imaginary city. The learning objectives for this chapter are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Summarise water quality data using various descriptive statistics&lt;/li&gt;
&lt;li&gt;Evaluate compliance of water quality data with relevant regulations.&lt;/li&gt;
&lt;li&gt;Perform grouped data analysis&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;

  &lt;div class=&#34;box&#34;&gt;
    &lt;div class=&#34;media&#34;&gt;
      &lt;figure class=&#34;media-left&#34;&gt;
        &lt;p class=&#34;image is-128x128&#34;&gt;
          &lt;img src=&#34;https://lucidmanager.org/images/books/2023_ds4wu.jpg&#34; alt = &#34;Data Science for Water Utilities&#34; title = &#34;Data Science for Water Utilities&#34;&gt;
        &lt;/p&gt;
      &lt;/figure&gt;
      &lt;div class=&#34;media-content&#34;&gt;
        &lt;div class=&#34;content&#34;&gt;
          &lt;p class=&#34;is-size-5 has-text-weight-bold&#34;&gt;Data Science for Water Utilities&lt;/p&gt;
          &lt;p class=&#34;mb-2&#34;&gt;Data Science for Water Utilities published by CRC Press is an applied, practical guide that shows water professionals how to use data science to solve urban water management problems using the R language for statistical computing.&lt;/p&gt;
          &lt;div class=&#34;buttons&#34;&gt;
            &lt;a class=&#34;button is-info&#34; href=&#34;https://routledge.com/9781032354545&#34; target=&#34;_blank&#34;&gt;
              &lt;span class=&#34;icon&#34;&gt;
                &lt;i class=&#34;fas fa-shopping-cart&#34;&gt;&lt;/i&gt;
              &lt;/span&gt;
              &lt;span&gt;Routledge&lt;/span&gt;
            &lt;/a&gt;
            &lt;a class=&#34;button is-info&#34; href=&#34;https://www.amazon.com/Data-Science-Water-Utilities-Chapman/dp/1032354550&#34; target=&#34;_blank&#34;&gt;
              &lt;span class=&#34;icon&#34;&gt;
                &lt;i class=&#34;fas fa-shopping-cart&#34;&gt;&lt;/i&gt;
              &lt;/span&gt;
              &lt;span&gt;Amazon&lt;/span&gt;
            &lt;/a&gt;
          &lt;/div&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
The data and code used in this chapter are available on GitHub:&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://github.com/pprevos/r4h2o/&#34; target=&#34;_blank&#34;
   title=&#34;Download r4h2o from GitHub&#34;
   alt=&#34;Download r4h2o from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;r4h2o&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-1&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-1&#34;&gt;
Descriptive Statistics
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-1&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;We have already seen &lt;em&gt;measures of frequency&lt;/em&gt; in Chapter 3, which count the number of times each observation occurs in a data sample. The remaining four descriptive statistics are:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;em&gt;Central tendency&lt;/em&gt; summarises a distribution with a single number&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Position&lt;/em&gt; describes how an observation relates to the others&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Dispersion&lt;/em&gt; describes how far the data deviates from the central tendency&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Shape&lt;/em&gt; summarises the shape of a distribution&lt;/li&gt;
&lt;/ol&gt;
&lt;div id=&#34;outline-container-headline-2&#34; class=&#34;outline-3&#34;&gt;
&lt;h3 id=&#34;headline-2&#34;&gt;
Measure of Central Tendency
&lt;/h3&gt;
&lt;div id=&#34;outline-text-headline-2&#34; class=&#34;outline-text-3&#34;&gt;
&lt;p&gt;A central tendency summarises a distribution with a single number, such as the mean, median or mode.&lt;/p&gt;
&lt;p&gt;
R has the &lt;code class=&#34;verbatim&#34;&gt;mean()&lt;/code&gt; and &lt;code class=&#34;verbatim&#34;&gt;median()&lt;/code&gt; functions, but no basic function to &lt;a href=&#34;https://www.statology.org/mode-in-r/&#34;&gt;calculate the mode&lt;/a&gt; is available but can easily be added. Water quality data tends to be heavily skewed, so care needs to be taken when using the mean to describe a sample of water quality samples.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/r4h2o/measures-of-central-tendency.png&#34; alt=&#34;Descriptive statistics: Measures of Central Tendency&#34; title=&#34;Descriptive statistics: Measures of Central Tendency&#34; width=&#34;600&#34;/&gt;
&lt;figcaption&gt;
Measures of central tendency.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-3&#34; class=&#34;outline-3&#34;&gt;
&lt;h3 id=&#34;headline-3&#34;&gt;
Measures of Position
&lt;/h3&gt;
&lt;div id=&#34;outline-text-headline-3&#34; class=&#34;outline-text-3&#34;&gt;
&lt;p&gt;A measure of position is a number’s relative position within the sample. Examples of measures of position are quartiles, deciles, and percentiles, generically known as a quantile. A percentile is a quantile multiplied by 100. Percentiles are a standard method to describe water quality data. For example, if we state that the 95th percentile of turbidity was 4 NTU, 95% of results were lower than or equal to 4 NTU, allowing the occasional spike. An earlier article on the website describes how R undertakes &lt;a href=&#34;https://lucidmanager.org/data-science/percentile-calculations/&#34;&gt;percentile calculations&lt;/a&gt; in more detail.&lt;/p&gt;
&amp;nbsp;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-4&#34; class=&#34;outline-3&#34;&gt;
&lt;h3 id=&#34;headline-4&#34;&gt;
Measures of Dispersion
&lt;/h3&gt;
&lt;div id=&#34;outline-text-headline-4&#34; class=&#34;outline-text-3&#34;&gt;
&lt;p&gt;Dispersion is the extent to which the measurements are spread. Several measures are available, such as the range and Inter-Quartile-Range, variance and standard deviation. The base R package has built-in functions for each of these measures. Please note that R uses &lt;a href=&#34;https://en.wikipedia.org/wiki/Bessel%27s_correction&#34;&gt;Bessel’s Correction&lt;/a&gt; to calculate variances to correct the bias in the estimated population variance.&lt;/p&gt;
&amp;nbsp;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-5&#34; class=&#34;outline-3&#34;&gt;
&lt;h3 id=&#34;headline-5&#34;&gt;
Measures of Shape
&lt;/h3&gt;
&lt;div id=&#34;outline-text-headline-5&#34; class=&#34;outline-text-3&#34;&gt;
&lt;p&gt;Lastly, we also need to know something about the shape of the distribution. The skewness of a distribution measure the eccentricity of the distribution curve. The kurtosis measures the ‘fatness’ of the tails of the distribution. A high skewness and kurtosis are, therefore, a sign of outliers in the data.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/r4h2o/measures-of-shape.png&#34; alt=&#34;Descriptive statistics: measures of shape (skewness and kurtosis)&#34; title=&#34;Descriptive statistics: measures of shape (skewness and kurtosis)&#34; width=&#34;600&#34;/&gt;
&lt;figcaption&gt;
Measures of shape (skewness and kurtosis)
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
The &lt;a href=&#34;https://cran.r-project.org/web/packages/moments/index.html&#34;&gt;moments&lt;/a&gt; and &lt;a href=&#34;https://cran.r-project.org/web/packages/e1071/index.html&#34;&gt;e1071&lt;/a&gt; packages provide functions to calculate skewness and kurtosis. These packages use slightly different approaches, so the results can differ. Refer to the book or the package documentation for details.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-6&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-6&#34;&gt;
Grouping Data
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-6&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;Most data sets contain observations from various groups of data. In this case, we can analyse data by analyte, suburb or sample point. The &lt;code class=&#34;verbatim&#34;&gt;group_by()&lt;/code&gt; function in the dplyr package creates a grouped data frame which means you can calculate descriptive statistics by group. The example below shows a subset of the water quality data, and the two tables on the right are grouped by measure (analyte). The &lt;code class=&#34;verbatim&#34;&gt;summarise&lt;/code&gt; function is the workhorse that lets us analyse the data frame for each group.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/r4h2o/grouping.png&#34; alt=&#34;Grouped data frames (tibbles) in Tidyverse&#34; title=&#34;Grouped data frames (tibbles) in Tidyverse&#34; width=&#34;600&#34;/&gt;
&lt;figcaption&gt;
Grouped data frames (tibbles) in Tidyverse.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-7&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-7&#34;&gt;
Descriptive Statistics in R Screencast
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-7&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;Chapter four of &lt;em&gt;Data Science for Water Utilities&lt;/em&gt; explains descriptive statistics and how to calculate them in R in more detail. This screencast below reviews the code for this chapter.&lt;/p&gt;
&lt;figure&gt;
&lt;div style=&#34;position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden;&#34;&gt;
      &lt;iframe allow=&#34;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share; fullscreen&#34; loading=&#34;eager&#34; referrerpolicy=&#34;strict-origin-when-cross-origin&#34; src=&#34;https://www.youtube.com/embed/-HGwANDwxjc?autoplay=0&amp;amp;controls=1&amp;amp;end=0&amp;amp;loop=0&amp;amp;mute=0&amp;amp;start=0&#34; style=&#34;position: absolute; top: 0; left: 0; width: 100%; height: 100%; border:0;&#34; title=&#34;YouTube video&#34;&gt;&lt;/iframe&gt;
    &lt;/div&gt;

&lt;figcaption&gt;
Descriptive Statistics in Water Quality screencast.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
The data and code used in this chapter are available on GitHub:&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://github.com/pprevos/r4h2o/&#34; target=&#34;_blank&#34;
   title=&#34;Download r4h2o from GitHub&#34;
   alt=&#34;Download r4h2o from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;r4h2o&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-8&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-8&#34;&gt;
Additional Resources
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-8&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;To help you remember the various functions discussed in the first five chapters of the book, a &lt;a href=&#34;https://lucidmanager.org/pdf/r4h2o-cheat-sheet.pdf&#34;&gt;cheat sheet&lt;/a&gt; is available.&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-9&#34; class=&#34;outline-3&#34;&gt;
&lt;h3 id=&#34;headline-9&#34;&gt;
Other Chapters
&lt;/h3&gt;
&lt;div id=&#34;outline-text-headline-9&#34; class=&#34;outline-text-3&#34;&gt;
&lt;p&gt;&lt;a href=&#34;https://lucidmanager.org/data-science/loading-and-exploring-water-quality-data/&#34;&gt;Previous Chapter&lt;/a&gt;: &lt;em&gt;Loading and Exploring Data&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;
&lt;a href=&#34;https://lucidmanager.org/data-science/visualising-data-with-ggplot2/&#34;&gt;Next Chapter&lt;/a&gt;: &lt;em&gt;Visualing Water Quality Data with ggplot2&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;
Feel free to &lt;a href=&#34;https://lucidmanager.org/contact/&#34;&gt;contact me&lt;/a&gt; if you have any comments, suggestions or questions about this book.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

      </description>
    </item>
    
    
    
    <item>
      <title>Detecting Outliers and Anomalies in Time Series Data</title>
      <link>https://lucidmanager.org/data-science/detecting-outliers-and-anomalies/</link>
      <pubDate>Wed, 10 May 2023 00:00:00 +0000</pubDate>
      <author>Peter Prevos</author>
      <guid>https://lucidmanager.org/data-science/detecting-outliers-and-anomalies/</guid>
      <description>
	
&lt;p&gt;
Reporting all available data has little value because it overwhelms the organisation with information that is not actionable. A more productive approach to processing the data deluge is to show those processes where something interesting has occurred. A range of techniques is available to detect anomalies in the data. Reporting outliers and anomalies focuses the organisation&amp;#39;s attention by raising questions and motivating action. &lt;a href=&#34;https://dx.doi.org/10.1201/9781003326977-12&#34;&gt;This chapter&lt;/a&gt; of &lt;em&gt;Data Science for Water Utilities&lt;/em&gt; discusses finding the most exciting points in your data to create actionable reports. This chapter also shows how to streamline code with functions and develop a leak detection tool to use with digital metering data. The learning objectives for this session are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Apply statistical methods to detect outliers&lt;/li&gt;
&lt;li&gt;Find anomalies in a time series&lt;/li&gt;
&lt;li&gt;Develop R functions to streamline your code&lt;/li&gt;
&lt;li&gt;Write a function to detect leaks from digital metering data&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;

  &lt;div class=&#34;box&#34;&gt;
    &lt;div class=&#34;media&#34;&gt;
      &lt;figure class=&#34;media-left&#34;&gt;
        &lt;p class=&#34;image is-128x128&#34;&gt;
          &lt;img src=&#34;https://lucidmanager.org/images/books/2023_ds4wu.jpg&#34; alt = &#34;Data Science for Water Utilities&#34; title = &#34;Data Science for Water Utilities&#34;&gt;
        &lt;/p&gt;
      &lt;/figure&gt;
      &lt;div class=&#34;media-content&#34;&gt;
        &lt;div class=&#34;content&#34;&gt;
          &lt;p class=&#34;is-size-5 has-text-weight-bold&#34;&gt;Data Science for Water Utilities&lt;/p&gt;
          &lt;p class=&#34;mb-2&#34;&gt;Data Science for Water Utilities published by CRC Press is an applied, practical guide that shows water professionals how to use data science to solve urban water management problems using the R language for statistical computing.&lt;/p&gt;
          &lt;div class=&#34;buttons&#34;&gt;
            &lt;a class=&#34;button is-info&#34; href=&#34;https://routledge.com/9781032354545&#34; target=&#34;_blank&#34;&gt;
              &lt;span class=&#34;icon&#34;&gt;
                &lt;i class=&#34;fas fa-shopping-cart&#34;&gt;&lt;/i&gt;
              &lt;/span&gt;
              &lt;span&gt;Routledge&lt;/span&gt;
            &lt;/a&gt;
            &lt;a class=&#34;button is-info&#34; href=&#34;https://www.amazon.com/Data-Science-Water-Utilities-Chapman/dp/1032354550&#34; target=&#34;_blank&#34;&gt;
              &lt;span class=&#34;icon&#34;&gt;
                &lt;i class=&#34;fas fa-shopping-cart&#34;&gt;&lt;/i&gt;
              &lt;/span&gt;
              &lt;span&gt;Amazon&lt;/span&gt;
            &lt;/a&gt;
          &lt;/div&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
The data and code used in this chapter are available on GitHub:&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://github.com/pprevos/r4h2o/&#34; target=&#34;_blank&#34;
   title=&#34;Download r4h2o from GitHub&#34;
   alt=&#34;Download r4h2o from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;r4h2o&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-1&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-1&#34;&gt;
Anomaly Detection Methods
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-1&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;Several methods are available to detect anomalies:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Visual inspection of graphs (such as boxplots, see &lt;a href=&#34;https://lucidmanager.org/data-science/visualising-data-with-ggplot2/&#34;&gt;chapter 6&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;Static thresholds, such as physical constraints or a regulatory limit &lt;/li&gt;
&lt;li&gt;Statistical criteria (such as &lt;a href=&#34;https://en.wikipedia.org/wiki/Standard_score&#34;&gt;Z-scores&lt;/a&gt; and &lt;a href=&#34;https://en.wikipedia.org/wiki/Median_absolute_deviation&#34;&gt;Median Absolute Deviation&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;Domain knowledge&lt;/li&gt;
&lt;/ul&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/r4h2o/boxplot-with-outliers.png&#34; alt=&#34;Visually detecting outliers with a boxplot of chlorine data&#34; title=&#34;Visually detecting outliers with a boxplot of chlorine data&#34; width=&#34;500&#34;/&gt;
&lt;figcaption&gt;
Visually detecting outliers with a boxplot of chlorine data.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-2&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-2&#34;&gt;
Leak Detection
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-2&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;Domain knowledge also helps to detect anomalies. For example, reservoir levels cannot extend much above the Full Supply Level, and a pH probe cannot measure values of zero and so on. Domain knowledge helps to define typical situations.&lt;/p&gt;
&lt;p&gt;
Detecting leaks downstream of a meter is easy when we can make some assumptions about the consumption pattern. For any residential connection, we can safely assume there is no flow for at least a few hours daily. This assumption can easily be transformed into code.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/r4h2o/diurnal-curve.png&#34; alt=&#34;Visually detecting outliers with a boxplot of chlorine data&#34; title=&#34;Visually detecting outliers with a boxplot of chlorine data&#34; width=&#34;500&#34;/&gt;
&lt;figcaption&gt;
Visually detecting outliers with a boxplot of chlorine data.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
The data for this case study was &lt;a href=&#34;https://lucidmanager.org/data-science/simulating-water-consumption/&#34;&gt;simulated using R scripts&lt;/a&gt;. A previous blog post about &lt;a href=&#34;https://lucidmanager.org/data-science/analyse-digital-water-meter-data/&#34;&gt;analysing digital meter data&lt;/a&gt; contains more details.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-3&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-3&#34;&gt;
Detecting Outliers and Anomalies Screencast
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-3&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;Chapter twelve of &lt;em&gt;Data Science for Water Utilities&lt;/em&gt; explains the theory and application of anomaly detection in more detail. This screencast below reviews the code for this chapter.&lt;/p&gt;
&lt;figure&gt;
&lt;div style=&#34;position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden;&#34;&gt;
      &lt;iframe allow=&#34;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share; fullscreen&#34; loading=&#34;eager&#34; referrerpolicy=&#34;strict-origin-when-cross-origin&#34; src=&#34;https://www.youtube.com/embed/U4w9-OdYc3A?autoplay=0&amp;amp;controls=1&amp;amp;end=0&amp;amp;loop=0&amp;amp;mute=0&amp;amp;start=0&#34; style=&#34;position: absolute; top: 0; left: 0; width: 100%; height: 100%; border:0;&#34; title=&#34;YouTube video&#34;&gt;&lt;/iframe&gt;
    &lt;/div&gt;

&lt;figcaption&gt;
Detecting Outliers and Anomalies Screencast.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
The data and code used in this chapter are available on GitHub:&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://github.com/pprevos/r4h2o/&#34; target=&#34;_blank&#34;
   title=&#34;Download r4h2o from GitHub&#34;
   alt=&#34;Download r4h2o from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;r4h2o&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-4&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-4&#34;&gt;
Additional Resources
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-4&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The video below provides an excellent introduction to the principles of anomaly detection.&lt;/p&gt;
&lt;figure&gt;
&lt;div style=&#34;position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden;&#34;&gt;
      &lt;iframe allow=&#34;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share; fullscreen&#34; loading=&#34;eager&#34; referrerpolicy=&#34;strict-origin-when-cross-origin&#34; src=&#34;https://www.youtube.com/embed/mhSFyXbCaPM?autoplay=0&amp;amp;controls=1&amp;amp;end=0&amp;amp;loop=0&amp;amp;mute=0&amp;amp;start=0&#34; style=&#34;position: absolute; top: 0; left: 0; width: 100%; height: 100%; border:0;&#34; title=&#34;YouTube video&#34;&gt;&lt;/iframe&gt;
    &lt;/div&gt;

&lt;figcaption&gt;
Anomaly Detection 101.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;div id=&#34;outline-container-headline-5&#34; class=&#34;outline-3&#34;&gt;
&lt;h3 id=&#34;headline-5&#34;&gt;
Other Chapters
&lt;/h3&gt;
&lt;div id=&#34;outline-text-headline-5&#34; class=&#34;outline-text-3&#34;&gt;
&lt;p&gt;&lt;a href=&#34;https://lucidmanager.org/data-science/working-with-dates-and-times-in-r/&#34;&gt;Previous Chapter&lt;/a&gt;: &lt;em&gt;Working with Dates and Times&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;
&lt;a href=&#34;https://lucidmanager.org/data-science/introduction-to-machine-learning/&#34;&gt;Next Chapter&lt;/a&gt;: &lt;em&gt;Introduction to Machine Learning&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;
Feel free to &lt;a href=&#34;https://lucidmanager.org/contact/&#34;&gt;contact me&lt;/a&gt; if you have any comments, suggestions or questions about this book.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

      </description>
    </item>
    
    
    
    <item>
      <title>Introduction to Machine Learning</title>
      <link>https://lucidmanager.org/data-science/introduction-to-machine-learning/</link>
      <pubDate>Wed, 10 May 2023 00:00:00 +0000</pubDate>
      <author>Peter Prevos</author>
      <guid>https://lucidmanager.org/data-science/introduction-to-machine-learning/</guid>
      <description>
	
&lt;p&gt;
Machine learning is an approach to statistical analysis whereby a computer detects and then uses the results to predict outcomes from new data. The combination of large amounts of available data becoming cheaply available and open source machine learning algorithms is causing a revolution in many industries, including water management. For example, water utilities can apply machine learning to predict which water or sewer main is likely to fail soon. &lt;a href=&#34;https://dx.doi.org/10.1201/9781003326977-13&#34;&gt;This chapter&lt;/a&gt; of &lt;em&gt;Data Science for Water Utilities&lt;/em&gt; introduces the principles of machine learning and the basic modelling process. The learning objectives for this chapter are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Understand the principles of machine learning&lt;/li&gt;
&lt;li&gt;Apply cross-validation to linear regression&lt;/li&gt;
&lt;li&gt;Implement a decision tree prediction&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;

  &lt;div class=&#34;box&#34;&gt;
    &lt;div class=&#34;media&#34;&gt;
      &lt;figure class=&#34;media-left&#34;&gt;
        &lt;p class=&#34;image is-128x128&#34;&gt;
          &lt;img src=&#34;https://lucidmanager.org/images/books/2023_ds4wu.jpg&#34; alt = &#34;Data Science for Water Utilities&#34; title = &#34;Data Science for Water Utilities&#34;&gt;
        &lt;/p&gt;
      &lt;/figure&gt;
      &lt;div class=&#34;media-content&#34;&gt;
        &lt;div class=&#34;content&#34;&gt;
          &lt;p class=&#34;is-size-5 has-text-weight-bold&#34;&gt;Data Science for Water Utilities&lt;/p&gt;
          &lt;p class=&#34;mb-2&#34;&gt;Data Science for Water Utilities published by CRC Press is an applied, practical guide that shows water professionals how to use data science to solve urban water management problems using the R language for statistical computing.&lt;/p&gt;
          &lt;div class=&#34;buttons&#34;&gt;
            &lt;a class=&#34;button is-info&#34; href=&#34;https://routledge.com/9781032354545&#34; target=&#34;_blank&#34;&gt;
              &lt;span class=&#34;icon&#34;&gt;
                &lt;i class=&#34;fas fa-shopping-cart&#34;&gt;&lt;/i&gt;
              &lt;/span&gt;
              &lt;span&gt;Routledge&lt;/span&gt;
            &lt;/a&gt;
            &lt;a class=&#34;button is-info&#34; href=&#34;https://www.amazon.com/Data-Science-Water-Utilities-Chapman/dp/1032354550&#34; target=&#34;_blank&#34;&gt;
              &lt;span class=&#34;icon&#34;&gt;
                &lt;i class=&#34;fas fa-shopping-cart&#34;&gt;&lt;/i&gt;
              &lt;/span&gt;
              &lt;span&gt;Amazon&lt;/span&gt;
            &lt;/a&gt;
          &lt;/div&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
The data and code used in this chapter are available on GitHub:&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://github.com/pprevos/r4h2o/&#34; target=&#34;_blank&#34;
   title=&#34;Download r4h2o from GitHub&#34;
   alt=&#34;Download r4h2o from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;r4h2o&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-1&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-1&#34;&gt;
Principles of Machine Learning
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-1&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;Machine learning is an area of study in computer science and statistics that involves developing algorithms that can automatically learn patterns from data and make predictions or decisions, without being explicitly programmed to do so.&lt;/p&gt;
&lt;p&gt;
We have already seen &lt;a href=&#34;https://lucidmanager.org/data-science/basic-linear-regression/&#34;&gt;linear regression&lt;/a&gt;, which is a form of supervised learning. In this type of machine learning, The model is trained on a dataset that contains both input and output variables. &lt;a href=&#34;https://lucidmanager.org/data-science/cluster-analysis-to-segment-customers/&#34;&gt;Cluster analysis&lt;/a&gt; belongs to the unsupervised leanring category. In these types of problems, the computer seeks dor structure in the data without independent variable.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/r4h2o/machine-learning-tree.png&#34; alt=&#34;Family tree of machine learning&#34; title=&#34;Family tree of machine learning&#34; width=&#34;400&#34;/&gt;
&lt;figcaption&gt;
Family tree of machine learning.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
This chapter introduces multiple linear regression and decision trees.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-2&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-2&#34;&gt;
Cross Validation and Fitting
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-2&#34; class=&#34;outline-text-2&#34;&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/r4h2o/machine-learning-fitting.png&#34; alt=&#34;Fitting a machine-learning model&#34; title=&#34;Fitting a machine-learning model&#34; width=&#34;400&#34;/&gt;
&lt;figcaption&gt;
Fitting a machine-learning model.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-3&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-3&#34;&gt;
Multiple Linear Regression
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-3&#34; class=&#34;outline-text-2&#34;&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/r4h2o/concrete-linear-regression.png&#34; alt=&#34;Concrete mixture decision tree&#34; title=&#34;Concrete mixture decision tree&#34; width=&#34;600&#34;/&gt;
&lt;figcaption&gt;
Concrete mixture decision tree.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;div id=&#34;outline-container-headline-4&#34; class=&#34;outline-3&#34;&gt;
&lt;h3 id=&#34;headline-4&#34;&gt;
Comparing models
&lt;/h3&gt;
&lt;div id=&#34;outline-text-headline-4&#34; class=&#34;outline-text-3&#34;&gt;
&lt;p&gt;RMSE&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-5&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-5&#34;&gt;
Decision Tree
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-5&#34; class=&#34;outline-text-2&#34;&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/r4h2o/concrete-decision-tree.png&#34; alt=&#34;Concrete mixture decision tree&#34; title=&#34;Concrete mixture decision tree&#34; width=&#34;400&#34;/&gt;
&lt;figcaption&gt;
Concrete mixture decision tree.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;div id=&#34;outline-container-headline-6&#34; class=&#34;outline-3&#34;&gt;
&lt;h3 id=&#34;headline-6&#34;&gt;
Comparing models
&lt;/h3&gt;
&lt;div id=&#34;outline-text-headline-6&#34; class=&#34;outline-text-3&#34;&gt;
&lt;p&gt;Confusion matrix&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-7&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-7&#34;&gt;
Introduction to Machine Learning Screencast
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-7&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;Chapter thirteen of &lt;em&gt;Data Science for Water Utilities&lt;/em&gt; explains the theory and application of machine learning in more detail. This screencast below reviews the code for this chapter.&lt;/p&gt;
&lt;figure&gt;
&lt;div style=&#34;position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden;&#34;&gt;
      &lt;iframe allow=&#34;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share; fullscreen&#34; loading=&#34;eager&#34; referrerpolicy=&#34;strict-origin-when-cross-origin&#34; src=&#34;https://www.youtube.com/embed/ZGJ77YexLyA?autoplay=0&amp;amp;controls=1&amp;amp;end=0&amp;amp;loop=0&amp;amp;mute=0&amp;amp;start=0&#34; style=&#34;position: absolute; top: 0; left: 0; width: 100%; height: 100%; border:0;&#34; title=&#34;YouTube video&#34;&gt;&lt;/iframe&gt;
    &lt;/div&gt;

&lt;figcaption&gt;
Introduction to Machine Learning Screencast.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
The data and code used in this chapter are available on GitHub:&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://github.com/pprevos/r4h2o/&#34; target=&#34;_blank&#34;
   title=&#34;Download r4h2o from GitHub&#34;
   alt=&#34;Download r4h2o from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;r4h2o&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-8&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-8&#34;&gt;
Additional Resources
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-8&#34; class=&#34;outline-text-2&#34;&gt;
&lt;div id=&#34;outline-container-headline-9&#34; class=&#34;outline-3&#34;&gt;
&lt;h3 id=&#34;headline-9&#34;&gt;
Other Chapters
&lt;/h3&gt;
&lt;div id=&#34;outline-text-headline-9&#34; class=&#34;outline-text-3&#34;&gt;
&lt;p&gt;&lt;a href=&#34;https://lucidmanager.org/data-science/detecting-outliers-and-anomalies/&#34;&gt;Previous Chapter&lt;/a&gt;: &lt;em&gt;Detecting Outliers and Anomalies&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;
Feel free to &lt;a href=&#34;https://lucidmanager.org/contact/&#34;&gt;contact me&lt;/a&gt; if you have any comments, suggestions or questions about this book.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

      </description>
    </item>
    
    
    
    <item>
      <title>Introduction to R for Utilities</title>
      <link>https://lucidmanager.org/data-science/r-for-water-utilities/</link>
      <pubDate>Wed, 10 May 2023 00:00:00 +0000</pubDate>
      <author>Peter Prevos</author>
      <guid>https://lucidmanager.org/data-science/r-for-water-utilities/</guid>
      <description>
	
&lt;p&gt;
Data science is the activity of creating value from data. While the mechanics of developing data products by writing code is an essential skill, data science as an activity needs to be strategic to deliver business value. This chapter develops a framework for a strategic approach to data science by creating useful, sound and aesthetic data products. Usefulness relates to a data product&amp;#39;s ability to achieve business outcomes. A sound data product is developed using valid and reliable models, is reproducible, and, when using data about people, is ethical. Lastly, data products need to be aesthetic, not to beautify but to ensure that the outcomes of analyses are easily understood so that benefits can be realised. The book &lt;em&gt;Data Science for Water Utilities&lt;/em&gt; introduces using R for water utilities through realistic case studies. &lt;a href=&#34;https://dx.doi.org/10.1201/9781003326977-1&#34;&gt;The first chapter&lt;/a&gt; introduces the principles of data science and how they relate to water management.&lt;/p&gt;
&lt;p&gt;

  &lt;div class=&#34;box&#34;&gt;
    &lt;div class=&#34;media&#34;&gt;
      &lt;figure class=&#34;media-left&#34;&gt;
        &lt;p class=&#34;image is-128x128&#34;&gt;
          &lt;img src=&#34;https://lucidmanager.org/images/books/2023_ds4wu.jpg&#34; alt = &#34;Data Science for Water Utilities&#34; title = &#34;Data Science for Water Utilities&#34;&gt;
        &lt;/p&gt;
      &lt;/figure&gt;
      &lt;div class=&#34;media-content&#34;&gt;
        &lt;div class=&#34;content&#34;&gt;
          &lt;p class=&#34;is-size-5 has-text-weight-bold&#34;&gt;Data Science for Water Utilities&lt;/p&gt;
          &lt;p class=&#34;mb-2&#34;&gt;Data Science for Water Utilities published by CRC Press is an applied, practical guide that shows water professionals how to use data science to solve urban water management problems using the R language for statistical computing.&lt;/p&gt;
          &lt;div class=&#34;buttons&#34;&gt;
            &lt;a class=&#34;button is-info&#34; href=&#34;https://routledge.com/9781032354545&#34; target=&#34;_blank&#34;&gt;
              &lt;span class=&#34;icon&#34;&gt;
                &lt;i class=&#34;fas fa-shopping-cart&#34;&gt;&lt;/i&gt;
              &lt;/span&gt;
              &lt;span&gt;Routledge&lt;/span&gt;
            &lt;/a&gt;
            &lt;a class=&#34;button is-info&#34; href=&#34;https://www.amazon.com/Data-Science-Water-Utilities-Chapman/dp/1032354550&#34; target=&#34;_blank&#34;&gt;
              &lt;span class=&#34;icon&#34;&gt;
                &lt;i class=&#34;fas fa-shopping-cart&#34;&gt;&lt;/i&gt;
              &lt;/span&gt;
              &lt;span&gt;Amazon&lt;/span&gt;
            &lt;/a&gt;
          &lt;/div&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-1&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-1&#34;&gt;
What is Data Science?
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-1&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;Data science involves analyzing data in a systematic way to create value for businesses. This field combines mathematics, computer science, and domain knowledge. Some professionals in the industry believe that individuals with expertise in all three areas are rare and often referred to as &amp;#34;data science unicorns.&amp;#34;&lt;/p&gt;
&lt;p&gt;
As part of my Data Science Unicorn Breeding Program, I have written a book aimed at water professionals who already possess mathematical skills and domain knowledge. I want to create the elusive data science unicorns by teaching them computer science.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/data-science/conway-venn-diagram.png&#34; alt=&#34;Conway Venn Diagram&#34; title=&#34;Conway Venn Diagram&#34; width=&#34;400&#34;/&gt;
&lt;figcaption&gt;
Data science competencies (Based on Conway, 2009).
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
See my article about &lt;a href=&#34;https://lucidmanager.org/data-science/strategic-data-science/&#34;&gt;strategic data science&lt;/a&gt;, which develops a systematic approach to creating value from data.&lt;/p&gt;
&lt;p&gt;

  &lt;div class=&#34;box&#34;&gt;
    &lt;div class=&#34;media&#34;&gt;
      &lt;figure class=&#34;media-left&#34;&gt;
        &lt;p class=&#34;image is-128x128&#34;&gt;
          &lt;img src=&#34;https://lucidmanager.org/images/books/2019_sds.jpg&#34; alt = &#34;Principles of Strategic Data Science&#34; title = &#34;Principles of Strategic Data Science&#34;&gt;
        &lt;/p&gt;
      &lt;/figure&gt;
      &lt;div class=&#34;media-content&#34;&gt;
        &lt;div class=&#34;content&#34;&gt;
          &lt;p class=&#34;is-size-5 has-text-weight-bold&#34;&gt;Principles of Strategic Data Science&lt;/p&gt;
          &lt;p class=&#34;mb-2&#34;&gt;Principles of Strategic Data Science helps you join the dots between mathematics, programming, and business analysis. With a unique approach that bridges the gap between mathematics and computer science, this book takes you through the entire data science pipeline.&lt;/p&gt;
          &lt;div class=&#34;buttons&#34;&gt;
            &lt;a class=&#34;button is-info&#34; href=&#34;https://www.packtpub.com/en-us/product/principles-of-strategic-data-science-9781838985509&#34; target=&#34;_blank&#34;&gt;
              &lt;span class=&#34;icon&#34;&gt;
                &lt;i class=&#34;fas fa-shopping-cart&#34;&gt;&lt;/i&gt;
              &lt;/span&gt;
              &lt;span&gt;Packt Publishing&lt;/span&gt;
            &lt;/a&gt;
          &lt;/div&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-2&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-2&#34;&gt;
Learning how to code
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-2&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;This book uses the &lt;a href=&#34;https://www.r-project.org/&#34;&gt;R language for Statistical Computing&lt;/a&gt;. This language is specifically designed for statistical analysis. Data science often uses other languages, such as Python, SQL, and Julia. The fact is that all data scientists need to be multi-lingual. &lt;/p&gt;
&lt;p&gt;
These seven steps will help you in your journey of becoming fluent in any programming language:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;a href=&#34;https://lucidmanager.org/data-science/basics-of-the-r-language/&#34;&gt;Understand the basics&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Code by hand&lt;/li&gt;
&lt;li&gt;Create simple programs&lt;/li&gt;
&lt;li&gt;Practice&lt;/li&gt;
&lt;li&gt;Ask for help&lt;/li&gt;
&lt;li&gt;Build projects&lt;/li&gt;
&lt;li&gt;Help others&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;This book introduces the basics of coding with R. Coding by hand is essential to thoroughly learning the language. Although generative AI, such as GPT or GitHub CoPilot, can write code, it will not teach you how to write code. Generative AI can assist you with finding solutions, but make sure you reverse-engineer the output so you understand the code.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-3&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-3&#34;&gt;
R for Water Utilities
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-3&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The following twelve chapters in this book introduce the R language by developing case studies:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Water quality data: Chapters 3–6 and 12&lt;/li&gt;
&lt;li&gt;Customer survey: Chapters 7–10&lt;/li&gt;
&lt;li&gt;Digital metering: Chapters 11–12&lt;/li&gt;
&lt;li&gt;Concrete curing: Chapter 13&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-4&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-4&#34;&gt;
Additional Resources
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-4&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The water industry produces a lot of resources on how to implement smart water networks.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The &lt;a href=&#34;https://swan-forum.com/&#34;&gt;Smart Water Networks Forum&lt;/a&gt; (SWAN) is the leading, global voice for the smart water sector. They have detailed resources on how to implement a smart water network.&lt;/li&gt;
&lt;li&gt;Fee &lt;a href=&#34;https://iwa-network.org/publications/digital-water-book-a-strategic-digital-transformation-for-the-water-industry/&#34;&gt;Digital Water Book&lt;/a&gt;: A Strategic Digital Transformation for the Water Industry.&lt;/li&gt;
&lt;/ul&gt;
&lt;div id=&#34;outline-container-headline-5&#34; class=&#34;outline-3&#34;&gt;
&lt;h3 id=&#34;headline-5&#34;&gt;
Other Chapters
&lt;/h3&gt;
&lt;div id=&#34;outline-text-headline-5&#34; class=&#34;outline-text-3&#34;&gt;
&lt;p&gt;&lt;a href=&#34;https://lucidmanager.org/data-science/basics-of-the-r-language/&#34;&gt;Next Chapter&lt;/a&gt;: &lt;em&gt;Basics of the R Language&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;
Feel free to &lt;a href=&#34;https://lucidmanager.org/contact/&#34;&gt;contact me&lt;/a&gt; if you have any comments, suggestions or questions about this book.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

      </description>
    </item>
    
    
    
    <item>
      <title>Loading and Exploring Water Quality Data from Spreadsheets</title>
      <link>https://lucidmanager.org/data-science/loading-and-exploring-water-quality-data/</link>
      <pubDate>Wed, 10 May 2023 00:00:00 +0000</pubDate>
      <author>Peter Prevos</author>
      <guid>https://lucidmanager.org/data-science/loading-and-exploring-water-quality-data/</guid>
      <description>
	
&lt;p&gt;
The first action in any analysis project is to obtain and load data. This data is often available in a database, the internet, or a file on your computer. One of the essential skills is thus to load this data into memory and view its content. This chapter introduces how to read CSV files and spreadsheets into R and explore their content using the Tidyverse suite of libraries. &lt;a href=&#34;https://dx.doi.org/10.1201/9781003326977-3&#34;&gt;This chapter&lt;/a&gt; of &lt;em&gt;Data Science for Water Utilities&lt;/em&gt; also presents a case study about water quality using synthetic data from an imaginary water supply network. This chapter has the following learning objectives:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Download and install R packages&lt;/li&gt;
&lt;li&gt;Load and describe CSV files and spreadsheet&lt;/li&gt;
&lt;li&gt;Explore the content of rectangular data (data frames)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;

  &lt;div class=&#34;box&#34;&gt;
    &lt;div class=&#34;media&#34;&gt;
      &lt;figure class=&#34;media-left&#34;&gt;
        &lt;p class=&#34;image is-128x128&#34;&gt;
          &lt;img src=&#34;https://lucidmanager.org/images/books/2023_ds4wu.jpg&#34; alt = &#34;Data Science for Water Utilities&#34; title = &#34;Data Science for Water Utilities&#34;&gt;
        &lt;/p&gt;
      &lt;/figure&gt;
      &lt;div class=&#34;media-content&#34;&gt;
        &lt;div class=&#34;content&#34;&gt;
          &lt;p class=&#34;is-size-5 has-text-weight-bold&#34;&gt;Data Science for Water Utilities&lt;/p&gt;
          &lt;p class=&#34;mb-2&#34;&gt;Data Science for Water Utilities published by CRC Press is an applied, practical guide that shows water professionals how to use data science to solve urban water management problems using the R language for statistical computing.&lt;/p&gt;
          &lt;div class=&#34;buttons&#34;&gt;
            &lt;a class=&#34;button is-info&#34; href=&#34;https://routledge.com/9781032354545&#34; target=&#34;_blank&#34;&gt;
              &lt;span class=&#34;icon&#34;&gt;
                &lt;i class=&#34;fas fa-shopping-cart&#34;&gt;&lt;/i&gt;
              &lt;/span&gt;
              &lt;span&gt;Routledge&lt;/span&gt;
            &lt;/a&gt;
            &lt;a class=&#34;button is-info&#34; href=&#34;https://www.amazon.com/Data-Science-Water-Utilities-Chapman/dp/1032354550&#34; target=&#34;_blank&#34;&gt;
              &lt;span class=&#34;icon&#34;&gt;
                &lt;i class=&#34;fas fa-shopping-cart&#34;&gt;&lt;/i&gt;
              &lt;/span&gt;
              &lt;span&gt;Amazon&lt;/span&gt;
            &lt;/a&gt;
          &lt;/div&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
The data and code used in this chapter are available on GitHub:&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://github.com/pprevos/r4h2o/&#34; target=&#34;_blank&#34;
   title=&#34;Download r4h2o from GitHub&#34;
   alt=&#34;Download r4h2o from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;r4h2o&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-1&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-1&#34;&gt;
The Tidyverse
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-1&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The R language has a lot of capabilities out of the box. But its strongest feature is that it can be easily extended. Thousands of packages are available for almost every imaginable specialised task. Packages are available in public repositories, such as &lt;a href=&#34;https://cran.r-project.org/web/packages/index.html&#34;&gt;CRAN&lt;/a&gt; (The Comprehensive R Archive Network).&lt;/p&gt;
&lt;p&gt;
The &lt;a href=&#34;https://www.tidyverse.org/&#34;&gt;Tidyverse&lt;/a&gt; is a collection of packages that makes working with R a bit easier. The remainder of this book will rely heavily on the Tidyverse way of analysing data.&lt;/p&gt;
&lt;p&gt;
A package provides additional functions for specialised tasks. You install packages with the &lt;code class=&#34;verbatim&#34;&gt;install.packages(&amp;#34;package-name&amp;#34;)&lt;/code&gt; function. To use the functions in a package, you must evaluate &lt;code class=&#34;verbatim&#34;&gt;library(package-name)&lt;/code&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-2&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-2&#34;&gt;
Loading Data into R
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-2&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;Using specialised packages, R can read almost every common data format, such as REST APIs, SQL databases and GIS shape files.&lt;/p&gt;
&lt;p&gt;
The &lt;a href=&#34;https://readr.tidyverse.org/&#34;&gt;Tidyverse readr&lt;/a&gt; package reads rectangular data from text files, such as the standard CSV format. Of course, R can also do this with base functions. Still, the Tidyverse version is faster and better at recognising data types. The &lt;a href=&#34;https://readxl.tidyverse.org/&#34;&gt;Tidyverse readxl&lt;/a&gt; package reads data from Excel files.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-3&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-3&#34;&gt;
Water Quality Case Study
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-3&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;This case study uses synthetic data from an imaginary water system named Gormsey. This data contains four analytes:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Escherichia coli (E. coli): A harmful bacterium.&lt;/li&gt;
&lt;li&gt;Total chlorine [mg/l]. Chlorine is dosed to prevent bacteria from multiplying.&lt;/li&gt;
&lt;li&gt;THM: Trihalomethanes. A byproduct of disinfection with chlorine that can cause a negative health impact.&lt;/li&gt;
&lt;li&gt;Turbidity [NTU]: A measure of cloudiness.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The data is available in the &lt;a href=&#34;https://github.com/pprevos/r4h2o&#34;&gt;GitHub repository&lt;/a&gt; for this book.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-4&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-4&#34;&gt;
Variable Types and Classes
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-4&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The previous chapter introduced scalar and vector variables. However, most data used in practice is stored in a rectangular format as a table. In the R language, a data frame is a rectangular data set where each column is a variable and each row an observation. The Tidyverse data frame is a Tibble, but these names are used interchangeably.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/r4h2o/variable-types.png&#34; alt=&#34;Scalar, vector and data frame variables in R&#34; title=&#34;Scalar, vector and data frame variables in R&#34; width=&#34;500&#34;/&gt;
&lt;figcaption&gt;
Scalar, vector and data frame variables in R.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-5&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-5&#34;&gt;
Exploring Water Quality Data Screencast
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-5&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;Chapter three of &lt;em&gt;Data Science for Water Utilities&lt;/em&gt; explains loading and exploring CSV and Excel files in more detail. This screencast below reviews the code for this chapter.&lt;/p&gt;
&lt;figure&gt;
&lt;div style=&#34;position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden;&#34;&gt;
      &lt;iframe allow=&#34;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share; fullscreen&#34; loading=&#34;eager&#34; referrerpolicy=&#34;strict-origin-when-cross-origin&#34; src=&#34;https://www.youtube.com/embed/KTQYBvD5DT4?autoplay=0&amp;amp;controls=1&amp;amp;end=0&amp;amp;loop=0&amp;amp;mute=0&amp;amp;start=0&#34; style=&#34;position: absolute; top: 0; left: 0; width: 100%; height: 100%; border:0;&#34; title=&#34;YouTube video&#34;&gt;&lt;/iframe&gt;
    &lt;/div&gt;

&lt;figcaption&gt;
Loading and exploring spreadsheet data in R.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
The data and code used in this chapter are available on GitHub:&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://github.com/pprevos/r4h2o/&#34; target=&#34;_blank&#34;
   title=&#34;Download r4h2o from GitHub&#34;
   alt=&#34;Download r4h2o from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;r4h2o&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-6&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-6&#34;&gt;
Additional Resources
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-6&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;To help you remember the various functions discussed in the first five chapters of the book, a &lt;a href=&#34;https://lucidmanager.org/pdf/r4h2o-cheat-sheet.pdf&#34;&gt;cheat sheet&lt;/a&gt; is available.&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-7&#34; class=&#34;outline-3&#34;&gt;
&lt;h3 id=&#34;headline-7&#34;&gt;
Other Chapters
&lt;/h3&gt;
&lt;div id=&#34;outline-text-headline-7&#34; class=&#34;outline-text-3&#34;&gt;
&lt;p&gt;&lt;a href=&#34;https://lucidmanager.org/data-science/basics-of-the-r-language/&#34;&gt;Previous Chapter&lt;/a&gt;: &lt;em&gt;Basics of the R Language&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;
&lt;a href=&#34;https://lucidmanager.org/data-science/descriptive-statistics-in-water-quality/&#34;&gt;Next Chapter&lt;/a&gt;: &lt;em&gt;Descriptive Statistics in Water Quality&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;
Feel free to &lt;a href=&#34;https://lucidmanager.org/contact/&#34;&gt;contact me&lt;/a&gt; if you have any comments, suggestions or questions about this book.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

      </description>
    </item>
    
    
    
    <item>
      <title>Managing and Cleaning Dirty Data</title>
      <link>https://lucidmanager.org/data-science/managing-dirty-data/</link>
      <pubDate>Wed, 10 May 2023 00:00:00 +0000</pubDate>
      <author>Peter Prevos</author>
      <guid>https://lucidmanager.org/data-science/managing-dirty-data/</guid>
      <description>
	
&lt;p&gt;
Data is almost always produced to manage operations and rarely collected to analyse it in the future. The available data is thus never in an ideal format and needs to be converted to be suitable for analysis. We need, at minimum, a clear data structure, the correct variable types, and readable variables names. Preparing data for analysis, sometimes called data munging or wrangling, is an essential part of the data science workflow. &lt;a href=&#34;https://dx.doi.org/10.1201/9781003326977-7&#34;&gt;This chapter&lt;/a&gt; of &lt;em&gt;Data Science for Water Utilities&lt;/em&gt; introduces some techniques to clean data with R and the Tidyverse to create reproducible code. This chapter introduces a case study about customer perceptions about tap water. The learning objectives for this session are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Use the dplyr package to transform data.&lt;/li&gt;
&lt;li&gt;Apply the principles of tidy data.&lt;/li&gt;
&lt;li&gt;Develop a script to automate data cleaning.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;

  &lt;div class=&#34;box&#34;&gt;
    &lt;div class=&#34;media&#34;&gt;
      &lt;figure class=&#34;media-left&#34;&gt;
        &lt;p class=&#34;image is-128x128&#34;&gt;
          &lt;img src=&#34;https://lucidmanager.org/images/books/2023_ds4wu.jpg&#34; alt = &#34;Data Science for Water Utilities&#34; title = &#34;Data Science for Water Utilities&#34;&gt;
        &lt;/p&gt;
      &lt;/figure&gt;
      &lt;div class=&#34;media-content&#34;&gt;
        &lt;div class=&#34;content&#34;&gt;
          &lt;p class=&#34;is-size-5 has-text-weight-bold&#34;&gt;Data Science for Water Utilities&lt;/p&gt;
          &lt;p class=&#34;mb-2&#34;&gt;Data Science for Water Utilities published by CRC Press is an applied, practical guide that shows water professionals how to use data science to solve urban water management problems using the R language for statistical computing.&lt;/p&gt;
          &lt;div class=&#34;buttons&#34;&gt;
            &lt;a class=&#34;button is-info&#34; href=&#34;https://routledge.com/9781032354545&#34; target=&#34;_blank&#34;&gt;
              &lt;span class=&#34;icon&#34;&gt;
                &lt;i class=&#34;fas fa-shopping-cart&#34;&gt;&lt;/i&gt;
              &lt;/span&gt;
              &lt;span&gt;Routledge&lt;/span&gt;
            &lt;/a&gt;
            &lt;a class=&#34;button is-info&#34; href=&#34;https://www.amazon.com/Data-Science-Water-Utilities-Chapman/dp/1032354550&#34; target=&#34;_blank&#34;&gt;
              &lt;span class=&#34;icon&#34;&gt;
                &lt;i class=&#34;fas fa-shopping-cart&#34;&gt;&lt;/i&gt;
              &lt;/span&gt;
              &lt;span&gt;Amazon&lt;/span&gt;
            &lt;/a&gt;
          &lt;/div&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
The data and code used in this chapter are available on GitHub:&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://github.com/pprevos/r4h2o/&#34; target=&#34;_blank&#34;
   title=&#34;Download r4h2o from GitHub&#34;
   alt=&#34;Download r4h2o from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;r4h2o&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-1&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-1&#34;&gt;
Case Study 2: Understanding the Customer Experience
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-1&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The data for this case study is a survey to measure the customer experience of the customers of a water utility. This data is generated with online survey software and contains information about:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;https://lucidmanager.org/data-science/measuring-consumer-involvement/&#34;&gt;Consumer Involvement&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Customer contact frequency&lt;/li&gt;
&lt;li&gt;Level of financial hardship&lt;/li&gt;
&lt;li&gt;Service Quality&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-2&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-2&#34;&gt;
Data Cleaning
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-2&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;Data cleaning, or data jujitsu as some call it, consists of three steps:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Load and explore the data&lt;/li&gt;
&lt;li&gt;Convert the data structure&lt;/li&gt;
&lt;li&gt;Remove invalid data&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;A basic principle of good data science is never to manually clean data. Using a script to clean your data means that it is reproducible for other data sets with the same structure and issues and you can always go back to the raw data in case something went wrong.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-3&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-3&#34;&gt;
Tidy Data
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-3&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The Tidyverse uses the principles of &lt;a href=&#34;https://www.jstatsoft.org/article/view/v059i10/&#34;&gt;Tidy Data&lt;/a&gt;, which means that:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Each variable forms a column&lt;/li&gt;
&lt;li&gt;Each observation forms a row&lt;/li&gt;
&lt;li&gt;Each type of observational unit forms a table&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The data on the left is untidy because the variable is spread over multiple columns. We can fix this by pivoting the data so  that the locations are no longer column names, but they become data.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/r4h2o/tidy-data.png&#34; alt=&#34;Untidy and tidy version of laboratory data&#34; title=&#34;Untidy and tidy version of laboratory data&#34; width=&#34;600&#34;/&gt;
&lt;figcaption&gt;
Untidy and tidy version of laboratory data.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-4&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-4&#34;&gt;
Managing Dirty Data Screencast
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-4&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;Chapter seven of &lt;em&gt;Data Science for Water Utilities&lt;/em&gt; explains using the dplyr package to clean data in more detail. This screencast below reviews the code for this chapter.&lt;/p&gt;
&lt;figure&gt;
&lt;div style=&#34;position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden;&#34;&gt;
      &lt;iframe allow=&#34;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share; fullscreen&#34; loading=&#34;eager&#34; referrerpolicy=&#34;strict-origin-when-cross-origin&#34; src=&#34;https://www.youtube.com/embed/BV8L94b1Y3s?autoplay=0&amp;amp;controls=1&amp;amp;end=0&amp;amp;loop=0&amp;amp;mute=0&amp;amp;start=0&#34; style=&#34;position: absolute; top: 0; left: 0; width: 100%; height: 100%; border:0;&#34; title=&#34;YouTube video&#34;&gt;&lt;/iframe&gt;
    &lt;/div&gt;

&lt;figcaption&gt;
Managing Dirty Data Screencast.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
The data and code used in this chapter are available on GitHub:&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://github.com/pprevos/r4h2o/&#34; target=&#34;_blank&#34;
   title=&#34;Download r4h2o from GitHub&#34;
   alt=&#34;Download r4h2o from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;r4h2o&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-5&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-5&#34;&gt;
Additional Resources
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-5&#34; class=&#34;outline-text-2&#34;&gt;
&lt;div id=&#34;outline-container-headline-6&#34; class=&#34;outline-3&#34;&gt;
&lt;h3 id=&#34;headline-6&#34;&gt;
Other Chapters
&lt;/h3&gt;
&lt;div id=&#34;outline-text-headline-6&#34; class=&#34;outline-text-3&#34;&gt;
&lt;p&gt;&lt;a href=&#34;https://lucidmanager.org/data-science/sharing-the-results-of-data-analysis/&#34;&gt;Previous Chapter&lt;/a&gt;: &lt;em&gt;Sharing Results of Data Analysis&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;
&lt;a href=&#34;https://lucidmanager.org/data-science/analysing-the-customer-experience/&#34;&gt;Next Chapter&lt;/a&gt;: &lt;em&gt;Analysing the Customer Experience&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;
Feel free to &lt;a href=&#34;https://lucidmanager.org/contact/&#34;&gt;contact me&lt;/a&gt; if you have any comments, suggestions or questions about this book.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

      </description>
    </item>
    
    
    
    <item>
      <title>Sharing the Results of Data Analysis with R Markdown</title>
      <link>https://lucidmanager.org/data-science/sharing-the-results-of-data-analysis/</link>
      <pubDate>Wed, 10 May 2023 00:00:00 +0000</pubDate>
      <author>Peter Prevos</author>
      <guid>https://lucidmanager.org/data-science/sharing-the-results-of-data-analysis/</guid>
      <description>
	
&lt;p&gt;
Data science aims to create value from data by creating useful, sound and aesthetic data products. Analysing data is rewarding, but creating value requires you to communicate the results. Analysing data in RStudio is fun but hard to share with anyone who does not understand the language. Of course, you could copy and paste results into a document, but that is neither efficient nor reproducible. This chapter explains how to communicate the fruits of your labour with colleagues and other interested parties by generating reports that combine text and analysis through literate programming. &lt;a href=&#34;https://dx.doi.org/10.1201/9781003326977-6&#34;&gt;This chapter&lt;/a&gt; of &lt;em&gt;Data Science for Water Utilities&lt;/em&gt;  a workflow for data science products and how to create data products using the R Markdown method. The learning objectives for this chapter are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Implement the workflow for data science projects&lt;/li&gt;
&lt;li&gt;Apply the principles of reproducible and replicable research&lt;/li&gt;
&lt;li&gt;Use basic R Markdown to create a Powerpoint presentation from data&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;

  &lt;div class=&#34;box&#34;&gt;
    &lt;div class=&#34;media&#34;&gt;
      &lt;figure class=&#34;media-left&#34;&gt;
        &lt;p class=&#34;image is-128x128&#34;&gt;
          &lt;img src=&#34;https://lucidmanager.org/images/books/2023_ds4wu.jpg&#34; alt = &#34;Data Science for Water Utilities&#34; title = &#34;Data Science for Water Utilities&#34;&gt;
        &lt;/p&gt;
      &lt;/figure&gt;
      &lt;div class=&#34;media-content&#34;&gt;
        &lt;div class=&#34;content&#34;&gt;
          &lt;p class=&#34;is-size-5 has-text-weight-bold&#34;&gt;Data Science for Water Utilities&lt;/p&gt;
          &lt;p class=&#34;mb-2&#34;&gt;Data Science for Water Utilities published by CRC Press is an applied, practical guide that shows water professionals how to use data science to solve urban water management problems using the R language for statistical computing.&lt;/p&gt;
          &lt;div class=&#34;buttons&#34;&gt;
            &lt;a class=&#34;button is-info&#34; href=&#34;https://routledge.com/9781032354545&#34; target=&#34;_blank&#34;&gt;
              &lt;span class=&#34;icon&#34;&gt;
                &lt;i class=&#34;fas fa-shopping-cart&#34;&gt;&lt;/i&gt;
              &lt;/span&gt;
              &lt;span&gt;Routledge&lt;/span&gt;
            &lt;/a&gt;
            &lt;a class=&#34;button is-info&#34; href=&#34;https://www.amazon.com/Data-Science-Water-Utilities-Chapman/dp/1032354550&#34; target=&#34;_blank&#34;&gt;
              &lt;span class=&#34;icon&#34;&gt;
                &lt;i class=&#34;fas fa-shopping-cart&#34;&gt;&lt;/i&gt;
              &lt;/span&gt;
              &lt;span&gt;Amazon&lt;/span&gt;
            &lt;/a&gt;
          &lt;/div&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
The data and code used in this chapter are available on GitHub:&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://github.com/pprevos/r4h2o/&#34; target=&#34;_blank&#34;
   title=&#34;Download r4h2o from GitHub&#34;
   alt=&#34;Download r4h2o from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;r4h2o&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-1&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-1&#34;&gt;
Data Science Workflow
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-1&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The data science workflow starts with &lt;em&gt;preparation&lt;/em&gt; and data cleaning, which can take most of the time in some projects. All projects should begin with a concise problem statement to prevent open-ended data dredging. Programmatic data cleaning is the topic of the next chapter.&lt;/p&gt;
&lt;p&gt;
Once the data has been tidied, we can try to &lt;em&gt;understand&lt;/em&gt; the data through exploratory analysis using &lt;a href=&#34;https://lucidmanager.org/data-science/descriptive-statistics-in-water-quality/&#34;&gt;descriptive statistics&lt;/a&gt; and &lt;a href=&#34;https://lucidmanager.org/data-science/visualising-data-with-ggplot2/&#34;&gt;visualisation&lt;/a&gt;. That insight allows us to model and reflect, an iterative process known as the &amp;#39;data vortex&amp;#39;.&lt;/p&gt;
&lt;p&gt;
Once we have created the model that best answers the problem statement, we can &lt;em&gt;communicate&lt;/em&gt; these results to the consumers of our data products. One method is through literate programming with tools such as R Markdown or Org Mode.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/r4h2o/workflow.png&#34; alt=&#34;Data science workflow&#34; title=&#34;Data science workflow&#34; width=&#34;600&#34;/&gt;
&lt;figcaption&gt;
Data science workflow.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-2&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-2&#34;&gt;
Literate Programming Reproducible Analysis
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-2&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;Literate programming means integrating the text, the data and the code. This approach ensures that the analysis is reproducible, which means you can repeat the same process with new data in the same way.&lt;/p&gt;
&lt;p&gt;
A data science project consists of at least three sets of code:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Cleaning code to get from raw data to a tidy state&lt;/li&gt;
&lt;li&gt;Analytical code to analyse the data&lt;/li&gt;
&lt;li&gt;Presentation code to create summaries, tables and figures.&lt;/li&gt;
&lt;/ol&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/r4h2o/literate-programming.png&#34; alt=&#34;Reproducible research pipeline&#34; title=&#34;Reproducible research pipeline&#34; width=&#34;600&#34;/&gt;
&lt;figcaption&gt;
Reproducible research pipeline.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
With literate programming, you can combine text and code in one script. The most popular method with R is to use a Markdown file. This is the WYSIWYM (What You See is What You Mean) approach instead of the more popular WYSIWYG (What You See Is What You Get) approach in word processors.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/r4h2o/wysiwym.png&#34; alt=&#34;What You See Is What You Mean (WYSIWYM)&#34; title=&#34;What You See Is What You Mean (WYSIWYM)&#34; width=&#34;600&#34;/&gt;
&lt;figcaption&gt;
What You See Is What You Mean (WYSIWYM)
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
The left part of the screen shows what it looks like when I write a book, and the right part shows the end result when exported as a PDF file.&lt;/p&gt;
&lt;p&gt;
The screencast below introduces the principles of this approach.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-3&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-3&#34;&gt;
Sharing the Results of Data Analysis Screencast
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-3&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;Chapter six of &lt;em&gt;Data Science for Water Utilities&lt;/em&gt; explains using literate programming with R Markdown in more detail. This screencast below reviews the code for this chapter.&lt;/p&gt;
&lt;figure&gt;
&lt;div style=&#34;position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden;&#34;&gt;
      &lt;iframe allow=&#34;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share; fullscreen&#34; loading=&#34;eager&#34; referrerpolicy=&#34;strict-origin-when-cross-origin&#34; src=&#34;https://www.youtube.com/embed/c_No60g-mi0?autoplay=0&amp;amp;controls=1&amp;amp;end=0&amp;amp;loop=0&amp;amp;mute=0&amp;amp;start=0&#34; style=&#34;position: absolute; top: 0; left: 0; width: 100%; height: 100%; border:0;&#34; title=&#34;YouTube video&#34;&gt;&lt;/iframe&gt;
    &lt;/div&gt;

&lt;figcaption&gt;
Sharing the Results of Data Analysis with RMarkdown.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
The data and code used in this chapter are available on GitHub:&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://github.com/pprevos/r4h2o/&#34; target=&#34;_blank&#34;
   title=&#34;Download r4h2o from GitHub&#34;
   alt=&#34;Download r4h2o from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;r4h2o&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-4&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-4&#34;&gt;
Additional Resources
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-4&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The &lt;code class=&#34;verbatim&#34;&gt;consumer-involvement.Rmd&lt;/code&gt; in the &lt;code class=&#34;verbatim&#34;&gt;case-studies&lt;/code&gt; folder of the GitHub repository shows how to write a journal article, including references using a BibTeX file. The concept of Consumer Involvement is explained in &lt;a href=&#34;https://lucidmanager.org/data-science/analysing-the-customer-experience/&#34;&gt;Chapter 8&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;
The &lt;a href=&#34;https://bookdown.org/yihui/rmarkdown/&#34;&gt;R Markdown: The Definitive Guide&lt;/a&gt; by Yihui Xie and others explains the how to use literate programming in R in great detail.&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-5&#34; class=&#34;outline-3&#34;&gt;
&lt;h3 id=&#34;headline-5&#34;&gt;
Other Chapters
&lt;/h3&gt;
&lt;div id=&#34;outline-text-headline-5&#34; class=&#34;outline-text-3&#34;&gt;
&lt;p&gt;&lt;a href=&#34;https://lucidmanager.org/data-science/visualising-data-with-ggplot2/&#34;&gt;Previous Chapter&lt;/a&gt;: &lt;em&gt;Visualising Data with ggplot2&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;
&lt;a href=&#34;https://lucidmanager.org/data-science/managing-dirty-data/&#34;&gt;Next Chapter&lt;/a&gt;: &lt;em&gt;Managing Dirty Data&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;
Feel free to &lt;a href=&#34;https://lucidmanager.org/contact/&#34;&gt;contact me&lt;/a&gt; if you have any comments, suggestions or questions about this book.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

      </description>
    </item>
    
    
    
    <item>
      <title>Visualising Data with ggplot2</title>
      <link>https://lucidmanager.org/data-science/visualising-data-with-ggplot2/</link>
      <pubDate>Wed, 10 May 2023 00:00:00 +0000</pubDate>
      <author>Peter Prevos</author>
      <guid>https://lucidmanager.org/data-science/visualising-data-with-ggplot2/</guid>
      <description>
	
&lt;p&gt;
Visualisation is essential for data scientists who want to explore and understand data and communicate their results. Scientists have studied how the mind perceives graphics in great depth and devised a comprehensive body of knowledge that helps us create sound, useful and aesthetic visualisations. The first part of &lt;a href=&#34;https://dx.doi.org/10.1201/9781003326977-5&#34;&gt;this chapter&lt;/a&gt; of &lt;em&gt;Data Science for Water Utilities&lt;/em&gt; introduces some principles of best practices in data visualisation. The second part presents the ggplot2 library and the principles of the Grammar of Graphics to create basic visualisations. The learning objectives for this chapter are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Evaluate data visualisations using basic principles.&lt;/li&gt;
&lt;li&gt;Apply the principles of the Grammar of Graphics.&lt;/li&gt;
&lt;li&gt;Visualise water quality data with the ggplot2 package.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;

  &lt;div class=&#34;box&#34;&gt;
    &lt;div class=&#34;media&#34;&gt;
      &lt;figure class=&#34;media-left&#34;&gt;
        &lt;p class=&#34;image is-128x128&#34;&gt;
          &lt;img src=&#34;https://lucidmanager.org/images/books/2023_ds4wu.jpg&#34; alt = &#34;Data Science for Water Utilities&#34; title = &#34;Data Science for Water Utilities&#34;&gt;
        &lt;/p&gt;
      &lt;/figure&gt;
      &lt;div class=&#34;media-content&#34;&gt;
        &lt;div class=&#34;content&#34;&gt;
          &lt;p class=&#34;is-size-5 has-text-weight-bold&#34;&gt;Data Science for Water Utilities&lt;/p&gt;
          &lt;p class=&#34;mb-2&#34;&gt;Data Science for Water Utilities published by CRC Press is an applied, practical guide that shows water professionals how to use data science to solve urban water management problems using the R language for statistical computing.&lt;/p&gt;
          &lt;div class=&#34;buttons&#34;&gt;
            &lt;a class=&#34;button is-info&#34; href=&#34;https://routledge.com/9781032354545&#34; target=&#34;_blank&#34;&gt;
              &lt;span class=&#34;icon&#34;&gt;
                &lt;i class=&#34;fas fa-shopping-cart&#34;&gt;&lt;/i&gt;
              &lt;/span&gt;
              &lt;span&gt;Routledge&lt;/span&gt;
            &lt;/a&gt;
            &lt;a class=&#34;button is-info&#34; href=&#34;https://www.amazon.com/Data-Science-Water-Utilities-Chapman/dp/1032354550&#34; target=&#34;_blank&#34;&gt;
              &lt;span class=&#34;icon&#34;&gt;
                &lt;i class=&#34;fas fa-shopping-cart&#34;&gt;&lt;/i&gt;
              &lt;/span&gt;
              &lt;span&gt;Amazon&lt;/span&gt;
            &lt;/a&gt;
          &lt;/div&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
The data and code used in this chapter are available on GitHub:&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://github.com/pprevos/r4h2o/&#34; target=&#34;_blank&#34;
   title=&#34;Download r4h2o from GitHub&#34;
   alt=&#34;Download r4h2o from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;r4h2o&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-1&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-1&#34;&gt;
Principles of Visualisation
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-1&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;Data visualisation needs to tell a story, which means that when somebody looks at the image, they need to draw a conclusion. A visualisation should not be a case of &amp;#34;create your own adventure&amp;#34;. Achieving this objective requires simplicity and graphic austerity.&lt;/p&gt;
&lt;p&gt;
The data-to-pixel ratio is a rule of thumb that helps to focus your visualisation on the story. This ratio expresses the number of pixels in the image (excluding the neutral background) versus those representing data.&lt;/p&gt;
&lt;p&gt;
The example below illustrates the idea. The image on the left might be interesting, but the background is distracting, and the colours don&amp;#39;t express data. The version on the right is a lot clearer on the story. We can immediately see which imaginary suburb has the best taste scores for tap water.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/r4h2o/data-pixel-ratio.png&#34; alt=&#34;Data to Pixel Ratio low and high&#34; title=&#34;Data to Pixel Ratio low and high&#34; width=&#34;500&#34;/&gt;
&lt;figcaption&gt;
Data to Pixel Ratio.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-2&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-2&#34;&gt;
Grammar of Graphics and ggplot2
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-2&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The &lt;a href=&#34;https://ggplot2.tidyverse.org/&#34;&gt;ggplot2 package&lt;/a&gt; is part of the Tidyverse. It implements the &lt;em&gt;Grammar of Graphics&lt;/em&gt;, a systematic approach to visualising data using layers. The various functions in ggplot2 are chained with a &lt;code class=&#34;verbatim&#34;&gt;+&lt;/code&gt; sign, forming the individual layers, as shown below. You can combine as many layers as you like.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/r4h2o/grammar-of-graphics.png&#34; alt=&#34;Grammar of Graphics and ggplot2 layers&#34; title=&#34;Grammar of Graphics and ggplot2 layers&#34; width=&#34;500&#34;/&gt;
&lt;figcaption&gt;
Grammar of Graphics and ggplot2 layers.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-3&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-3&#34;&gt;
Using Colour in Visualisations
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-3&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;Most graphs overuse colour to visualise data. Colours should only be used to comply with a style guide or to visualise data.&lt;/p&gt;
&lt;p&gt;
The &lt;a href=&#34;https://colorbrewer2.org&#34;&gt;Color Brewer website&lt;/a&gt; provides a great interface to explore various colour palates.&lt;/p&gt;
&lt;p&gt;
Since writing this book, the latest R version has received an upgraded colour palette. &lt;a href=&#34;https://arxiv.org/abs/2303.04918&#34;&gt;This article&lt;/a&gt; explains the new built-in colour palettes.&lt;/p&gt;
&lt;p&gt;
Emil Hvitfeld has gathered a large collection of &lt;a href=&#34;https://github.com/EmilHvitfeldt/r-color-palettes&#34;&gt;colour palettes in R&lt;/a&gt;. &lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-4&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-4&#34;&gt;
Visualising Data with ggplot2 Screencast
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-4&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;Chapter five of &lt;em&gt;Data Science for Water Utilities&lt;/em&gt; explains visualising data with ggplot2 in more detail. This screencast below reviews the code for this chapter.&lt;/p&gt;
&lt;p&gt;
To help you remember the various functions discussed in the first five chapters of the book, a &lt;a href=&#34;https://lucidmanager.org/pdf/r4h2o-cheat-sheet.pdf&#34;&gt;cheat sheet&lt;/a&gt; is available.&lt;/p&gt;
&lt;figure&gt;
&lt;div style=&#34;position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden;&#34;&gt;
      &lt;iframe allow=&#34;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share; fullscreen&#34; loading=&#34;eager&#34; referrerpolicy=&#34;strict-origin-when-cross-origin&#34; src=&#34;https://www.youtube.com/embed/rzsh2uLtBTU?autoplay=0&amp;amp;controls=1&amp;amp;end=0&amp;amp;loop=0&amp;amp;mute=0&amp;amp;start=0&#34; style=&#34;position: absolute; top: 0; left: 0; width: 100%; height: 100%; border:0;&#34; title=&#34;YouTube video&#34;&gt;&lt;/iframe&gt;
    &lt;/div&gt;

&lt;figcaption&gt;
Visualising Water Quality Data with ggplot2.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
The data and code used in this chapter are available on GitHub:&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://github.com/pprevos/r4h2o/&#34; target=&#34;_blank&#34;
   title=&#34;Download r4h2o from GitHub&#34;
   alt=&#34;Download r4h2o from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;r4h2o&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-5&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-5&#34;&gt;
Additional Resources
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-5&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;To help you remember the various functions discussed in the first five chapters of the book, a &lt;a href=&#34;https://lucidmanager.org/pdf/r4h2o-cheat-sheet.pdf&#34;&gt;cheat sheet&lt;/a&gt; is available.&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-6&#34; class=&#34;outline-3&#34;&gt;
&lt;h3 id=&#34;headline-6&#34;&gt;
Addendum
&lt;/h3&gt;
&lt;div id=&#34;outline-text-headline-6&#34; class=&#34;outline-text-3&#34;&gt;
&lt;p&gt;The &lt;code&gt;width&lt;/code&gt; parameter in ggplot2 geometries that use lines has been deprecated and is replaced with &lt;code&gt;linewidth&lt;/code&gt;.&lt;/p&gt;
&amp;nbsp;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-7&#34; class=&#34;outline-3&#34;&gt;
&lt;h3 id=&#34;headline-7&#34;&gt;
Other Sources
&lt;/h3&gt;
&lt;div id=&#34;outline-text-headline-7&#34; class=&#34;outline-text-3&#34;&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;https://www.slideserve.com/lenore-holder/designing-great-visualizations&#34;&gt;PPT - Designing Great Visualizations PowerPoint Presentation, free download - ID:6574423&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://guides.lib.berkeley.edu/data-visualization/about&#34;&gt;About - Data Visualization - Library Guides at UC Berkeley&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://r-graph-gallery.com/&#34;&gt;The R Graph Gallery – Help and inspiration for R charts&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://flowingdata.com/category/tutorials/&#34;&gt;Tutorials | FlowingData&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&amp;nbsp;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-8&#34; class=&#34;outline-3&#34;&gt;
&lt;h3 id=&#34;headline-8&#34;&gt;
Other Chapters
&lt;/h3&gt;
&lt;div id=&#34;outline-text-headline-8&#34; class=&#34;outline-text-3&#34;&gt;
&lt;p&gt;&lt;a href=&#34;https://lucidmanager.org/data-science/descriptive-statistics-in-water-quality/&#34;&gt;Previous Chapter&lt;/a&gt;: &lt;em&gt;Descriptive Statistics&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;
&lt;a href=&#34;https://lucidmanager.org/data-science/sharing-the-results-of-data-analysis/&#34;&gt;Next Chapter&lt;/a&gt;: &lt;em&gt;Sharing the Results&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;
Feel free to &lt;a href=&#34;https://lucidmanager.org/contact/&#34;&gt;contact me&lt;/a&gt; if you have any comments, suggestions or questions about this book.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

      </description>
    </item>
    
    
    
    <item>
      <title>Working with Dates and Times in R</title>
      <link>https://lucidmanager.org/data-science/working-with-dates-and-times-in-r/</link>
      <pubDate>Wed, 10 May 2023 00:00:00 +0000</pubDate>
      <author>Peter Prevos</author>
      <guid>https://lucidmanager.org/data-science/working-with-dates-and-times-in-r/</guid>
      <description>
	
&lt;p&gt;
Most data collected in operational processes are time series. These are data sets where each measurement is associated with the time of the measurement or observation. This chapter introduces a study which looks at synthetic data collected from water meters from individual houses. Meter reads collected at a high frequency provide valuable insights into water consumption, such as leak detection. &lt;a href=&#34;https://dx.doi.org/10.1201/9781003326977-11&#34;&gt;This chapter&lt;/a&gt; of &lt;em&gt;Data Science for Water Utilities&lt;/em&gt; explains how the R language manages dates and times and uses this knowledge to explore time series data. The learning objectives for this chapter are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Understand the principles of how the R language processes date and time variables.&lt;/li&gt;
&lt;li&gt;Apply the functionality of the Lubridate package to manipulate dates and times.&lt;/li&gt;
&lt;li&gt;Use date and time variables in calculations and visualisations.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;

  &lt;div class=&#34;box&#34;&gt;
    &lt;div class=&#34;media&#34;&gt;
      &lt;figure class=&#34;media-left&#34;&gt;
        &lt;p class=&#34;image is-128x128&#34;&gt;
          &lt;img src=&#34;https://lucidmanager.org/images/books/2023_ds4wu.jpg&#34; alt = &#34;Data Science for Water Utilities&#34; title = &#34;Data Science for Water Utilities&#34;&gt;
        &lt;/p&gt;
      &lt;/figure&gt;
      &lt;div class=&#34;media-content&#34;&gt;
        &lt;div class=&#34;content&#34;&gt;
          &lt;p class=&#34;is-size-5 has-text-weight-bold&#34;&gt;Data Science for Water Utilities&lt;/p&gt;
          &lt;p class=&#34;mb-2&#34;&gt;Data Science for Water Utilities published by CRC Press is an applied, practical guide that shows water professionals how to use data science to solve urban water management problems using the R language for statistical computing.&lt;/p&gt;
          &lt;div class=&#34;buttons&#34;&gt;
            &lt;a class=&#34;button is-info&#34; href=&#34;https://routledge.com/9781032354545&#34; target=&#34;_blank&#34;&gt;
              &lt;span class=&#34;icon&#34;&gt;
                &lt;i class=&#34;fas fa-shopping-cart&#34;&gt;&lt;/i&gt;
              &lt;/span&gt;
              &lt;span&gt;Routledge&lt;/span&gt;
            &lt;/a&gt;
            &lt;a class=&#34;button is-info&#34; href=&#34;https://www.amazon.com/Data-Science-Water-Utilities-Chapman/dp/1032354550&#34; target=&#34;_blank&#34;&gt;
              &lt;span class=&#34;icon&#34;&gt;
                &lt;i class=&#34;fas fa-shopping-cart&#34;&gt;&lt;/i&gt;
              &lt;/span&gt;
              &lt;span&gt;Amazon&lt;/span&gt;
            &lt;/a&gt;
          &lt;/div&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
The data and code used in this chapter are available on GitHub:&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://github.com/pprevos/r4h2o/&#34; target=&#34;_blank&#34;
   title=&#34;Download r4h2o from GitHub&#34;
   alt=&#34;Download r4h2o from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;r4h2o&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-1&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-1&#34;&gt;
Date and Time Variables
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-1&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;All date and time variables in R use integer numbers starting at the Unix Epoch (1 January 1970). Spreadsheets tend to use 31 December 1899 as their datum.&lt;/p&gt;
&lt;p&gt;
The basic time string uses the ISO &lt;a href=&#34;https://en.wikipedia.org/wiki/ISO_8601&#34;&gt;8601 convention&lt;/a&gt;, i.e. &amp;#34;2023-06-15 18:23:45 AEST&amp;#34;.&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-2&#34; class=&#34;outline-3&#34;&gt;
&lt;h3 id=&#34;headline-2&#34;&gt;
Lubridate Package
&lt;/h3&gt;
&lt;div id=&#34;outline-text-headline-2&#34; class=&#34;outline-text-3&#34;&gt;
&lt;p&gt;Working with dates and times in base R can be challenging. The &lt;a href=&#34;https://lubridate.tidyverse.org/&#34;&gt;lubridate package&lt;/a&gt; in the Tidyverse lubricates dates and times with some convenient functions.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-3&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-3&#34;&gt;
Digital Metering Leak Detection
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-3&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The case study for this chapter uses synthetic digital metering data. The data for this case study was &lt;a href=&#34;https://lucidmanager.org/data-science/simulating-water-consumption/&#34;&gt;simulated using R scripts&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;
The article on &lt;a href=&#34;https://lucidmanager.org/data-science/analyse-digital-water-meter-data/&#34;&gt;analysing digital metering data&lt;/a&gt; explains some of the principles and creates a diurnal curve.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/r4h2o/diurnal-curve.png&#34; alt=&#34;Example of a diurnal curve for domestic water consumption&#34; title=&#34;Example of a diurnal curve for domestic water consumption&#34; width=&#34;600&#34;/&gt;
&lt;figcaption&gt;
Example of a diurnal curve for domestic water consumption.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-4&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-4&#34;&gt;
Working with Dates and Times in R Screencast
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-4&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;Chapter eleven of &lt;em&gt;Data Science for Water Utilities&lt;/em&gt; explains working with dates and times and leak detection in more detail. This screencast below reviews the code for this chapter.&lt;/p&gt;
&lt;figure&gt;
&lt;div style=&#34;position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden;&#34;&gt;
      &lt;iframe allow=&#34;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share; fullscreen&#34; loading=&#34;eager&#34; referrerpolicy=&#34;strict-origin-when-cross-origin&#34; src=&#34;https://www.youtube.com/embed/bjcBbZXbO8A?autoplay=0&amp;amp;controls=1&amp;amp;end=0&amp;amp;loop=0&amp;amp;mute=0&amp;amp;start=0&#34; style=&#34;position: absolute; top: 0; left: 0; width: 100%; height: 100%; border:0;&#34; title=&#34;YouTube video&#34;&gt;&lt;/iframe&gt;
    &lt;/div&gt;

&lt;figcaption&gt;
Working with Dates and Times in R.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
The data and code used in this chapter are available on GitHub:&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://github.com/pprevos/r4h2o/&#34; target=&#34;_blank&#34;
   title=&#34;Download r4h2o from GitHub&#34;
   alt=&#34;Download r4h2o from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;r4h2o&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-5&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-5&#34;&gt;
Additional Resources
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-5&#34; class=&#34;outline-text-2&#34;&gt;
&lt;div id=&#34;outline-container-headline-6&#34; class=&#34;outline-3&#34;&gt;
&lt;h3 id=&#34;headline-6&#34;&gt;
Addendum
&lt;/h3&gt;
&lt;div id=&#34;outline-text-headline-6&#34; class=&#34;outline-text-3&#34;&gt;
&lt;p&gt;The code on page 150 uses the &lt;code class=&#34;verbatim&#34;&gt;summarise()&lt;/code&gt; function to calculate daily flows. This function has been deprecated for this particular case. While &lt;code&gt;summarise()&lt;/code&gt; requires that each argument returns a single value, and &lt;code&gt;mutate()&lt;/code&gt; requires that each argument returns the same number of rows as the input, &lt;a href=&#34;https://dplyr.tidyverse.org/reference/reframe.html&#34;&gt;reframe()&lt;/a&gt; is a more general workhorse with no requirements on the number of rows returned per group.&lt;/p&gt;
&amp;nbsp;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-7&#34; class=&#34;outline-3&#34;&gt;
&lt;h3 id=&#34;headline-7&#34;&gt;
Other Chapters
&lt;/h3&gt;
&lt;div id=&#34;outline-text-headline-7&#34; class=&#34;outline-text-3&#34;&gt;
&lt;p&gt;&lt;a href=&#34;https://lucidmanager.org/data-science/cluster-analysis-to-segment-customers/&#34;&gt;Previous Chapter&lt;/a&gt;: &lt;em&gt;Clustering Customers to Define Segments&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;
&lt;a href=&#34;https://lucidmanager.org/data-science/detecting-outliers-and-anomalies/&#34;&gt;Next Chapter&lt;/a&gt;: &lt;em&gt;Detecting Anomalies and Outliers in Water Data&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;
Feel free to &lt;a href=&#34;https://lucidmanager.org/contact/&#34;&gt;contact me&lt;/a&gt; if you have any comments, suggestions or questions about this book.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

      </description>
    </item>
    
    
    
    <item>
      <title>Monte Carlo Cost Estimates: Engineers Throwing Dice</title>
      <link>https://lucidmanager.org/data-science/monte-carlo-cost-estimates/</link>
      <pubDate>Mon, 23 Aug 2021 00:00:00 +0000</pubDate>
      <author>Peter Prevos</author>
      <guid>https://lucidmanager.org/data-science/monte-carlo-cost-estimates/</guid>
      <description>
	
&lt;p&gt;
Estimating the cost of a complex project is not a trivial task. Traditional cost estimates are full of assumptions about the future state of the market and the final deliverable. Monte Carlo cost estimates are a tool for better understanding your project&amp;#39;s risks and enabling better cost control. Monte Carlo simulations are a technique to control your “known unknowns”.&lt;/p&gt;
&lt;p&gt;
Albert Einstein famously said that “God does not play dice”. While this might or might not be the case, engineers play dice and embrace the stochastic nature of reality to predict the future. Monte Carlo Simulations are a method for estimating the cost of your project across thousands of parallel universes to assess financial risk. &lt;/p&gt;
&lt;p&gt;
While I have a pet hate against using &lt;a href=&#34;https://lucidmanager.org/management/alternative-risk-matrix-template/&#34;&gt;matrices to manage risk&lt;/a&gt;, Monte Carlo simulations are an analytical method for dealing with uncertainty and risk. This article explains the principles of Monte Carlo cost estimates and how to implement them in the R language for statistical computing.&lt;/p&gt;
&lt;p&gt;
You can download the code from GitHub in the &lt;code class=&#34;verbatim&#34;&gt;case-studies&lt;/code&gt; folder.&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://github.com/pprevos/r4h2o/&#34; target=&#34;_blank&#34;
   title=&#34;Download r4h2o from GitHub&#34;
   alt=&#34;Download r4h2o from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;r4h2o&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-1&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-1&#34;&gt;
The principles of cost estimates
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-1&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The basic principle of cost estimation is deceptively simple. To estimate the cost for each item in a project, multiply the quantity of work $Q_i$ times the rate $R_i$ you will pay for each unit of work. Sum the cost of each item, and you have the total project cost $P$ for a project with $j$ items:&lt;/p&gt;
&lt;p&gt;
$$P = \sum_{i=1}^j Q_i R_i$$&lt;/p&gt;
&lt;p&gt;
The reality is, unfortunately, more inconsistent than this equation can express. Determining the correct quantity and rate for each item is a fine art requiring knowledge of engineering and economics.&lt;/p&gt;
&lt;p&gt;
Cost estimates reflect the state of knowledge at the time of developing them. Many aspects of the project have yet to be discovered and might only surface once we start digging. As the late &lt;a href=&#34;https://medium.com/the-philosophers-stone/rumsfelds-logic-of-known-knowns-known-unknowns-and-unknown-unknowns-f506db31ac74&#34;&gt;Donald Rumsfeld philosophically said&lt;/a&gt;:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;… there are known knowns; there are things we know we know. We also know there are known unknowns; that is, we know there are things we do not know. But there are also unknown unknowns — the ones we don’t know, we don’t know.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;figure&gt;
&lt;div style=&#34;position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden;&#34;&gt;
      &lt;iframe allow=&#34;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share; fullscreen&#34; loading=&#34;eager&#34; referrerpolicy=&#34;strict-origin-when-cross-origin&#34; src=&#34;https://www.youtube.com/embed/REWeBzGuzCc?autoplay=0&amp;amp;controls=1&amp;amp;end=0&amp;amp;loop=0&amp;amp;mute=0&amp;amp;start=0&#34; style=&#34;position: absolute; top: 0; left: 0; width: 100%; height: 100%; border:0;&#34; title=&#34;YouTube video&#34;&gt;&lt;/iframe&gt;
    &lt;/div&gt;

&lt;figcaption&gt;
Donald Rumsfeld.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;div id=&#34;outline-container-headline-2&#34; class=&#34;outline-3&#34;&gt;
&lt;h3 id=&#34;headline-2&#34;&gt;
Deterministic
&lt;/h3&gt;
&lt;div id=&#34;outline-text-headline-2&#34; class=&#34;outline-text-3&#34;&gt;
&lt;p&gt;The basic method is helpful, but it assumes we know the amount of work or the unit rate accurately. Both parameters are subject to uncertainty and risk because the project has yet to be realised. Only at the end of the project do we know precisely how much it costs.&lt;/p&gt;
&lt;p&gt;
In deterministic cost estimation, this uncertainty is accounted for by adding a contingency to the estimate. This contingency is simply a percentage of the total. The less information about the project (usually at the early stages of development), the higher the rate.&lt;/p&gt;
&lt;p&gt;
In the planning phases, 20% or even higher might be suitable, while uncertainty can be much lower after tender award. This uncertainty covers any risk that quantities or rates are higher, as well as unforeseen activities that were not costed. The table below shows a typical deterministic cost estimate.&lt;/p&gt;
&lt;figure&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Item&lt;/th&gt;
&lt;th class=&#34;align-right&#34;&gt;Quantity&lt;/th&gt;
&lt;th&gt;Unit&lt;/th&gt;
&lt;th class=&#34;align-right&#34;&gt;Rate&lt;/th&gt;
&lt;th class=&#34;align-right&#34;&gt;Cost&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Materials&lt;/td&gt;
&lt;td class=&#34;align-right&#34;&gt;12,000&lt;/td&gt;
&lt;td&gt;$m^1$&lt;/td&gt;
&lt;td class=&#34;align-right&#34;&gt;75&lt;/td&gt;
&lt;td class=&#34;align-right&#34;&gt;$900,000&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Excavation&lt;/td&gt;
&lt;td class=&#34;align-right&#34;&gt;2,200&lt;/td&gt;
&lt;td&gt;$m^3$&lt;/td&gt;
&lt;td class=&#34;align-right&#34;&gt;15&lt;/td&gt;
&lt;td class=&#34;align-right&#34;&gt;$33,000&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Pipe laying&lt;/td&gt;
&lt;td class=&#34;align-right&#34;&gt;12,000&lt;/td&gt;
&lt;td&gt;$m^1$&lt;/td&gt;
&lt;td class=&#34;align-right&#34;&gt;13&lt;/td&gt;
&lt;td class=&#34;align-right&#34;&gt;$156,000&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;em&gt;SUBTOTAL&lt;/em&gt;&lt;/td&gt;
&lt;td class=&#34;align-right&#34;&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td class=&#34;align-right&#34;&gt;&lt;/td&gt;
&lt;td class=&#34;align-right&#34;&gt;&lt;em&gt;$1,089,000&lt;/em&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Contingency&lt;/td&gt;
&lt;td class=&#34;align-right&#34;&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td class=&#34;align-right&#34;&gt;20%&lt;/td&gt;
&lt;td class=&#34;align-right&#34;&gt;$217,000&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;TOTAL&lt;/strong&gt;&lt;/td&gt;
&lt;td class=&#34;align-right&#34;&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td class=&#34;align-right&#34;&gt;&lt;/td&gt;
&lt;td class=&#34;align-right&#34;&gt;&lt;strong&gt;$1,306,000&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;figcaption&gt;
Example of a simple pipeline project estimate.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
A deterministic method is a blunt tool. Uncertainties are not estimated; they are based on rules of thumb. In this example, we cannot know which part of the project contributes most to the uncertainty or the likelihood of staying within budget. Is the project manager simply inflating the budget to reduce the risk of spending more? What is the probability that the project will cost more than the estimated amount? We cannot answer these questions with the deterministic method.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-3&#34; class=&#34;outline-3&#34;&gt;
&lt;h3 id=&#34;headline-3&#34;&gt;
Probabilistic
&lt;/h3&gt;
&lt;div id=&#34;outline-text-headline-3&#34; class=&#34;outline-text-3&#34;&gt;
&lt;p&gt;A better understanding of the financial risk in a project leads to better decisions. Probabilistic cost estimation methods assess the uncertainty of each item rather than applying a percentage to the total. Breaking the uncertainty into smaller chunks allows project managers to better understand the financial risk in their projects and reduces the risk of significant estimating errors.&lt;/p&gt;
&lt;p&gt;
The quantity of work and the rate we pay vary with local conditions, external factors, market rates, and other conditions. We thus need to introduce an error rate $\epsilon_i$ for each item to account for the uncertainty in the estimate. Uncertainty relates to the inaccuracy of each cost item due to a lack of information. Thus, our formula for a project with $j$ items and $k$ events cost now becomes:&lt;/p&gt;
&lt;p&gt;
$$P = \sum_{i=1}^j (Q_i R_i)+ \epsilon_i$$&lt;/p&gt;
&lt;p&gt;
The most basic way to implement this concept is to assign a contingency to each item in the estimate and nominate the risk events in something like this:&lt;/p&gt;
&lt;figure&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Item&lt;/th&gt;
&lt;th class=&#34;align-right&#34;&gt;Quantity&lt;/th&gt;
&lt;th&gt;Unit&lt;/th&gt;
&lt;th class=&#34;align-right&#34;&gt;Rate&lt;/th&gt;
&lt;th class=&#34;align-right&#34;&gt;Cost&lt;/th&gt;
&lt;th class=&#34;align-right&#34;&gt;Uncertainty&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Materials&lt;/td&gt;
&lt;td class=&#34;align-right&#34;&gt;12,000&lt;/td&gt;
&lt;td&gt;$m^1$&lt;/td&gt;
&lt;td class=&#34;align-right&#34;&gt;75&lt;/td&gt;
&lt;td class=&#34;align-right&#34;&gt;$900,000&lt;/td&gt;
&lt;td class=&#34;align-right&#34;&gt;$9,000&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Excavation&lt;/td&gt;
&lt;td class=&#34;align-right&#34;&gt;2,200&lt;/td&gt;
&lt;td&gt;$m^3$&lt;/td&gt;
&lt;td class=&#34;align-right&#34;&gt;15&lt;/td&gt;
&lt;td class=&#34;align-right&#34;&gt;$33,000&lt;/td&gt;
&lt;td class=&#34;align-right&#34;&gt;$16,500&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Pipe laying&lt;/td&gt;
&lt;td class=&#34;align-right&#34;&gt;12,000&lt;/td&gt;
&lt;td&gt;$m^1$&lt;/td&gt;
&lt;td class=&#34;align-right&#34;&gt;13&lt;/td&gt;
&lt;td class=&#34;align-right&#34;&gt;$156,000&lt;/td&gt;
&lt;td class=&#34;align-right&#34;&gt;$30,000&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;em&gt;SUBTOTAL&lt;/em&gt;&lt;/td&gt;
&lt;td class=&#34;align-right&#34;&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td class=&#34;align-right&#34;&gt;&lt;/td&gt;
&lt;td class=&#34;align-right&#34;&gt;&lt;em&gt;$1,089,000&lt;/em&gt;&lt;/td&gt;
&lt;td class=&#34;align-right&#34;&gt;&lt;em&gt;$55,500&lt;/em&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;TOTAL&lt;/strong&gt;&lt;/td&gt;
&lt;td class=&#34;align-right&#34;&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td class=&#34;align-right&#34;&gt;&lt;/td&gt;
&lt;td class=&#34;align-right&#34;&gt;&lt;strong&gt;$1,144,500&lt;/strong&gt;&lt;/td&gt;
&lt;td class=&#34;align-right&#34;&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;figcaption&gt;
Example of a simple pipeline project estimate.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
Note that we now call it &amp;#39;uncertainty&amp;#39; rather than &amp;#39;contingency&amp;#39;. This terminology is more than a semantic difference, as we have estimated our uncertainty at the item level in the Work-Breakdown-Structure.&lt;/p&gt;
&lt;p&gt;
This approach provides additional intelligence about where the project&amp;#39;s risks lie, which can help the team allocate efforts where they are most needed. The fact that the estimate is lower than before is less interesting than what we can gather from the details. If this were my project, I would gather more information about the events with the highest relative uncertainty to reduce risk.&lt;/p&gt;
&lt;p&gt;
A probabilistic cost estimate produces higher accuracy because the error rate for some items will be more controllable than for others. In the traditional method, the amount of contingency is anyone&amp;#39;s best guess. Estimates often hide contingencies in cost items to reduce the risk of requesting more budget. While this approach is pragmatic, looking at each item is more transparent.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-4&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-4&#34;&gt;
Monte Carlo Cost Estimate
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-4&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The probabilistic approach in the table above is an acceptable way to assign uncertainties and risks. Still, it does not give us insight into how likely this estimate will eventuate.&lt;/p&gt;
&lt;p&gt;
Experience shows that the final project outcome rarely matches the estimate. It is, after all, an estimate, not a prediction. In other words, if you do the same project thousands of times in parallel universes, you will have thousands of different outcomes. This science-fiction concept is the basis for Monte Carlo simulations. With some statistics, we can recreate this hypothetical situation by repeating the same estimate thousands of times and analysing the distribution of possible outcomes.&lt;/p&gt;
&lt;p&gt;
Monte Carlo simulations model reality using large volumes of randomised numbers drawn from known distributions. They can be used for any situation where deterministic methods are too complex, such as &lt;a href=&#34;https://lucidmanager.org/data-science/call-centre-workforce-planning-erlang-c-in-r/&#34;&gt;modelling contact centre call traffic&lt;/a&gt;, or in this case, a cost estimate.&lt;/p&gt;
&lt;p&gt;
To add more information to the estimate, we assign each item a low, likely and high cost. The low cost ($a$) is rarely achieved and is your ‘bargain-basement’ price. The likely cost ($c$) is what you would typically use in your estimate. Finally, the high cost ($b$) is what you will pay when everything you can think of goes wrong. Statistically, each cost item now has a &lt;a href=&#34;https://en.wikipedia.org/wiki/Triangular_distribution&#34;&gt;triangular probability distribution&lt;/a&gt;, visualised below.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://upload.wikimedia.org/wikipedia/commons/4/45/Triangular_distribution_PMF.png&#34; alt=&#34;Triangular probability density distribution&#34; title=&#34;Triangular probability density function&#34; width=&#34;400&#34;/&gt;
&lt;figcaption&gt;
Triangular probability density function.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
Our cost estimate would now look something like this:&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Item&lt;/th&gt;
&lt;th class=&#34;align-right&#34;&gt;Low&lt;/th&gt;
&lt;th class=&#34;align-right&#34;&gt;Medium&lt;/th&gt;
&lt;th class=&#34;align-right&#34;&gt;High&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Materials&lt;/td&gt;
&lt;td class=&#34;align-right&#34;&gt;900000&lt;/td&gt;
&lt;td class=&#34;align-right&#34;&gt;909000&lt;/td&gt;
&lt;td class=&#34;align-right&#34;&gt;950000&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Excavation&lt;/td&gt;
&lt;td class=&#34;align-right&#34;&gt;33000&lt;/td&gt;
&lt;td class=&#34;align-right&#34;&gt;49500&lt;/td&gt;
&lt;td class=&#34;align-right&#34;&gt;50000&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Pipe laying&lt;/td&gt;
&lt;td class=&#34;align-right&#34;&gt;156000&lt;/td&gt;
&lt;td class=&#34;align-right&#34;&gt;186000&lt;/td&gt;
&lt;td class=&#34;align-right&#34;&gt;200000&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;
For each item, we can calculate the average or any other percentile. The average cost of an item is $\frac{a+b+c}{3}$. The expected cost $x$ with likelihood $p$ is defined by the &lt;a href=&#34;https://lucidmanager.org/data-science/percentile-calculations/&#34;&gt;quantiles&lt;/a&gt; for each item, given by:&lt;/p&gt;
&lt;p&gt;
$$x_p = \begin{cases}
a + \sqrt{(b-a)(c-a)p} &amp;amp; 0 \leq p \leq p_c \\
b - \sqrt{(b-a)(b-c)(1-p)} &amp;amp; p_c &amp;lt; p \leq 1
\end{cases}$$&lt;/p&gt;
&lt;p&gt;
Where $p_c$ is the probability of the likely estimate ($c$): $p_c=\frac{c-a}{b-a}$, and $a \leq c$ and $b \geq c$.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://upload.wikimedia.org/wikipedia/commons/thumb/e/e5/Triangular_distribution_CMF.png/800px-Triangular_distribution_CMF.png&#34; alt=&#34;Triangular cumulative distribution function&#34; title=&#34;cumulative distribution function&#34; width=&#34;400&#34;/&gt;
&lt;figcaption&gt;
Triangular cumulative distribution function.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
While the triangular distribution is the most common method for assessing uncertainty, other distributions are possible; the principles remain the same. Be mindful, however, that most probability distributions range from minus to plus infinity, which is not a realistic assumption for cost estimates.&lt;/p&gt;
&lt;p&gt;
If your cost estimate contains only one item, you are done, as you can calculate the project cost by plugging in a percentile $p$ into the formulas. The final project cost will be lower than this number at probability $p$. The chosen percentile depends on your risk appetite. A percentile of .95 provides much more confidence that the actual cost will be lower than the budget.&lt;/p&gt;
&lt;p&gt;
But when the estimate includes multiple items, doing so analytically will be too complex for mere mortals. This situation is where the Monte Carlo technique comes in. The Monte Carlo method simulates reality by calculating thousands of possible outcomes. The result is not a singular number but a distribution of $n$ possible outcomes.&lt;/p&gt;
&lt;p&gt;
The outcome of a Monte Carlo simulation is a cost estimate for thousands of parallel universes. If you were to undertake the project $n$ times, the distribution of the final cost is expected to resemble the simulation outcome.&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-5&#34; class=&#34;outline-3&#34;&gt;
&lt;h3 id=&#34;headline-5&#34;&gt;
Monte Carlo Simulation in R
&lt;/h3&gt;
&lt;div id=&#34;outline-text-headline-5&#34; class=&#34;outline-text-3&#34;&gt;
&lt;p&gt;Several R libraries can calculate triangular distributions. The &lt;code class=&#34;verbatim&#34;&gt;triangle&lt;/code&gt; package provides a set of functions to work with &lt;a href=&#34;https://bertcarnell.github.io/triangle/&#34;&gt;triangular distributions&lt;/a&gt;. The &lt;code class=&#34;verbatim&#34;&gt;rtriangle()&lt;/code&gt; function provides a vector of random quantiles for the chosen distribution, as shown below:&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;# Monte Carlo Simulation in R&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(triangle)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;# Triangular distribution&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;hist&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;rtriangle&lt;/span&gt;(n &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10000&lt;/span&gt;, a &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;12000&lt;/span&gt;, b &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;15000&lt;/span&gt;, c &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;14000&lt;/span&gt;), 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;       breaks &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;100&lt;/span&gt;, 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;       main &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Triangular distribution simulation&amp;#34;&lt;/span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
When plotting the histogram of these results, the triangular shape becomes evident, albeit a bit rough around the edges. Notice that a Monte Carlo simulation is only ever an estimate. The higher the number of simulations (in this diagram, 10,000), the higher the resolution of the result. To test this proposition, change the &lt;code class=&#34;verbatim&#34;&gt;n&lt;/code&gt; parameter in the &lt;code class=&#34;verbatim&#34;&gt;rtriangle()&lt;/code&gt; function.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/hydroinformatics/triangular-distribution.png&#34; alt=&#34;Simulated triangular distribution&#34; title=&#34;Simulated triangular distribution&#34;/&gt;
&lt;figcaption&gt;
Simulated triangular distribution.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
To add the probability distribution of all your cost items, store all simulations in a matrix with &lt;code class=&#34;verbatim&#34;&gt;n&lt;/code&gt; columns (number of simulations) and &lt;code class=&#34;verbatim&#34;&gt;j&lt;/code&gt; (number of cost items) rows. Then, in the next step, you add all the simulated values and create a histogram or calculate percentiles.&lt;/p&gt;
&lt;p&gt;
The example below reads a CSV file with the same content as the table above. It then creates a results matrix, runs the simulations and calculates and visualises the results.&lt;/p&gt;
&lt;p&gt;
The &lt;code&gt;set.seed(1234)&lt;/code&gt; function fixes the pseudo-random number generator to always use the same &amp;#39;random&amp;#39; numbers. This way we always have the same result instead of introducing stochastic noise. If you need true randomness, then use a &lt;a href=&#34;https://cran.r-project.org/web/packages/random/index.html&#34;&gt;specialised random generator package&lt;/a&gt; that obtains numbers form the &lt;a href=&#34;https://random.org/&#34;&gt;random.org&lt;/a&gt; website.&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Read Data&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  estimate &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;read.csv&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;data/cost-estimate.csv&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Simulation settings&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  n &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10000&lt;/span&gt; &lt;span style=&#34;color:#888&#34;&gt;## Simulations&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  j &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;nrow&lt;/span&gt;(estimate)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  mc_sims &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;matrix&lt;/span&gt;(ncol &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; n,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                    nrow &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; j)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Simulation&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;set.seed&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1234&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;for&lt;/span&gt; (i &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;in&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;j){
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    mc_sims[i,] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;rtriangle&lt;/span&gt;(n &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; n,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                             a &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; estimate&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;Low[i],
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                             b &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; estimate&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;High[i],
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                             c &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; estimate&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;Medium[i])}
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Determine estimates and 95th percentile&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  mc_results &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;colSums&lt;/span&gt;(mc_sims)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  p95 &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;quantile&lt;/span&gt;(mc_results, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0.95&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Visualise&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;hist&lt;/span&gt;(mc_results, breaks &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;100&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;abline&lt;/span&gt;(v &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; p95, col &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;red&amp;#34;&lt;/span&gt;, lwd &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
A Monte Carlo simulation outcome is thus never one number but a vector of numbers from which you can analyse. The example below uses the 95&lt;sup&gt;th&lt;/sup&gt; percentile as the budget figure.&lt;/p&gt;
&lt;p&gt;
Adding triangular distributions will create a new distribution specific to this project. The &lt;code class=&#34;verbatim&#34;&gt;mc_results&lt;/code&gt; vector holds the estimated possible total cost.&lt;/p&gt;
&lt;p&gt;
To set a budget figure, you need to nominate a percentile. The 50&lt;sup&gt;th&lt;/sup&gt; percentile has an equal chance of being higher or lower than the actual. You should choose the 95&lt;sup&gt;th&lt;/sup&gt; percentile or whatever percentile matches your risk appetite to have more certainty that a sufficient budget is available.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/hydroinformatics/monte-carlo-estimate.png&#34; alt=&#34;Monte Carlo cost estimate&#34; title=&#34;Monte Carlo cost estimate&#34;/&gt;
&lt;figcaption&gt;
Monte Carlo cost estimate and 95&lt;sup&gt;th&lt;/sup&gt; percentile.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
Monte Carlo cost estimates are not only for projects. You can also undertake these calculations at the program level and assess the level of financial risk you hold within the program. &lt;/p&gt;
&lt;p&gt;
Monte Carlo cost estimates are a powerful tool for better understanding your project. They are, however, like any modelling, subject to the GiGo principle (Garbage-in-Garbage-out). Therefore, determining the low, likely and high costs will require domain knowledge. While these three estimates have their level of uncertainty, they will provide better insights than relying only on a monolithic amount for contingency.&lt;/p&gt;
&lt;p&gt;
Probabilistic cost estimates do not guarantee that your project will remain under budget, but they help you achieve this elusive goal.&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://www.r-bloggers.com/&#34; target=&#34;_blank&#34; title=&#34;Proudly associated with R-Bloggers&#34;&gt;
  &lt;button class=&#34;button is-link is-medium&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-r-project&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span&gt;As seen on R Bloggers&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

      </description>
    </item>
    
    
    
    <item>
      <title>Visualise Org-Roam Networks With igraph and R</title>
      <link>https://lucidmanager.org/data-science/visualise-org-roam/</link>
      <pubDate>Sat, 14 Aug 2021 00:00:00 +0000</pubDate>
      <author>Peter Prevos</author>
      <guid>https://lucidmanager.org/data-science/visualise-org-roam/</guid>
      <description>
	
&lt;p&gt;
The &lt;a href=&#34;https://lucidmanager.org/productivity/taking-notes-with-emacs-org-mode-and-org-roam/&#34;&gt;Emacs package Org-Roam&lt;/a&gt; provides a powerful tool to take notes following the idea of the Zettelkasten method. You can write notes with all the power that Emacs provides while linking your thoughts to each other and with your bibliography. This article shows how to analyse and visualise Org-Roam knowledge networks with the &lt;a href=&#34;https://igraph.org/&#34;&gt;iGraph package&lt;/a&gt; and the R language.&lt;/p&gt;
&lt;p&gt;
The image below shows the current state of my personal knowledge network as visualised by iGraph. The Org-Roam community is working hard to create a &lt;a href=&#34;https://github.com/org-roam/org-roam-ui&#34;&gt;visual user interface&lt;/a&gt; for Org-Roam with network diagrams. While this user interface is a great tool to explore your network, Emacs users tend to prefer writing code instead of clicking buttons. This article adds to this concept by showing how to apply some mathematical analysis and delve deep into your Zettelkasten. The beauty of iGraph is that it is available in R, Python, Mathematica and C/C++.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/emacs/org-roam-network.png&#34; alt=&#34;My personal Org-roam notes network&#34; title=&#34;My personal Org-roam notes network&#34;/&gt;
&lt;figcaption&gt;
My personal Org-roam notes network.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
You can download the code in this article from GitHub.&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://github.com/pprevos/digital-humanities/&#34; target=&#34;_blank&#34;
   title=&#34;Download digital-humanities from GitHub&#34;
   alt=&#34;Download digital-humanities from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;digital-humanities&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-1&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-1&#34;&gt;
Loading the Database
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-1&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;Org-Roam stores information about the notes in your system in an SQLite database. This database holds information about the nodes (registered files and headings) and links. The &lt;a href=&#34;https://db.rstudio.com/r-packages/dbi/&#34;&gt;DBI package&lt;/a&gt; enables connecting to these databases.&lt;/p&gt;
&lt;p&gt;
The first lines open the connection. You will have to change the path of the database location to your preferences for this to work. The two queries extract the unique ID and the title for all nodes. The second query extracts the links between nodes. The Org-Roam database also stores external links, which are excluded from this analysis.&lt;/p&gt;
&lt;p&gt;
The next part cleans the titles by removing quotation marks and joins the tables. I could have done this in the query, but I know Tidyverse better than SQL.&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Visualise Org-Roam databse&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Connect to database and extract nodes and links&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(DBI)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  roam &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;dbConnect&lt;/span&gt;(RSQLite&lt;span style=&#34;color:#333&#34;&gt;::&lt;/span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;SQLite&lt;/span&gt;(), &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;~/.config/emacs/org-roam.db&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  nodes &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;dbGetQuery&lt;/span&gt;(roam, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;SELECT id, title FROM nodes&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  links &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;dbGetQuery&lt;/span&gt;(roam, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;SELECT source, dest FROM links WHERE type = &amp;#39;\&amp;#34;id\&amp;#34;&amp;#39;&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;dbDisconnect&lt;/span&gt;(roam)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Clean node names and create network table&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(tidyverse)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  nodes &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; nodes &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;mutate&lt;/span&gt;(title &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;str_remove_all&lt;/span&gt;(title, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;\&amp;#34;&amp;#34;&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  network &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;left_join&lt;/span&gt;(links, nodes, by &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;source&amp;#34;&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;id&amp;#34;&lt;/span&gt;)) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;left_join&lt;/span&gt;(nodes, by &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;dest&amp;#34;&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;id&amp;#34;&lt;/span&gt;)) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;select&lt;/span&gt;(from &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; title.x, to &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; title.y)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
The result of this code is a data frame with two names of the nodes at each vertex in the network. Any unconnected nodes are ignored due to the left join. The next step is to convert this data to iGraph format and start visualising and analysing.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-2&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-2&#34;&gt;
Create iGraph network
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-2&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The iGraph package uses a specific data format. The &lt;code class=&#34;verbatim&#34;&gt;graph.data.frame()&lt;/code&gt; function converts a data frame to an iGraph network. The &lt;code class=&#34;verbatim&#34;&gt;simplify()&lt;/code&gt; function removes any multiple links and self-references. &lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Create network diagram&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(igraph)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  g &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;graph.data.frame&lt;/span&gt;(network)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  g &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;simplify&lt;/span&gt;(g)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-3&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-3&#34;&gt;
Analyse the Network
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-3&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The iGraph package has an extensive library of functions to analyse networks. In this section, we look at a measure for centrality and clustering. &lt;/p&gt;
&lt;div id=&#34;outline-container-headline-4&#34; class=&#34;outline-3&#34;&gt;
&lt;h3 id=&#34;headline-4&#34;&gt;
Centrality
&lt;/h3&gt;
&lt;div id=&#34;outline-text-headline-4&#34; class=&#34;outline-text-3&#34;&gt;
&lt;p&gt;Not all nodes in the network are of equal importance. &lt;a href=&#34;https://en.wikipedia.org/wiki/Centrality&#34;&gt;Centrality&lt;/a&gt; is a concept in graph theory that describes the importance of a node in a network. The most straightforward method of calculating the centrality of a node is to count the number of vertices (connections) that it has with other nodes. The example below shows a simple undirected graph and the degree of each node.&lt;/p&gt;
&lt;figure&gt;
&lt;a title=&#34;Melchoir (source); pan BMP, CC BY-SA 4.0 &amp;lt;https://creativecommons.org/licenses/by-sa/4.0&amp;gt;, via Wikimedia Commons&#34; href=&#34;https://commons.wikimedia.org/wiki/File:UndirectedDegrees_(Loop).svg&#34;&gt;&lt;img width=&#34;320&#34; alt=&#34;UndirectedDegrees (Loop)&#34; src=&#34;https://upload.wikimedia.org/wikipedia/commons/thumb/d/d6/UndirectedDegrees_%28Loop%29.svg/320px-UndirectedDegrees_%28Loop%29.svg.png&#34;&gt;&lt;/a&gt;
&lt;figcaption&gt;
Example of the degree centrality of an undirected graph.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
In a directed graph, like with Org-Roam, we can calculate the in-degree and out-degree, the number of vertices point &lt;em&gt;to&lt;/em&gt; the node, or the number of vertices pointing &lt;em&gt;from&lt;/em&gt; the node (the number of backlinks and the number of links). The &lt;code class=&#34;verbatim&#34;&gt;degree()&lt;/code&gt; function of the iGraph package calculates degree centrality. The code below generates a table with the top ten most connected nodes.&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Centrality&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  centrality &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;tibble&lt;/span&gt;(Node &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;names&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;degree&lt;/span&gt;(g)),
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                       Degree &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;degree&lt;/span&gt;(g, mode &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;total&amp;#34;&lt;/span&gt;),
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                       Links &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;degree&lt;/span&gt;(g, mode &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;out&amp;#34;&lt;/span&gt;),
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                       Backlinks &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;degree&lt;/span&gt;(g, mode &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;in&amp;#34;&lt;/span&gt;)) 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  centrality &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;top_n&lt;/span&gt;(n &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;arrange&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;desc&lt;/span&gt;(Degree))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-5&#34; class=&#34;outline-3&#34;&gt;
&lt;h3 id=&#34;headline-5&#34;&gt;
Communities
&lt;/h3&gt;
&lt;div id=&#34;outline-text-headline-5&#34; class=&#34;outline-text-3&#34;&gt;
&lt;p&gt;While some nodes might be more connected than others, it would also be good to know which nodes are closest to each other. In other words, can we find clusters of nodes (communities) to find meta-structure in the network? A community is a sub-network with densely connected nodes, but less so with nodes outside the community. Expressed probabilistically, two random nodes are more
likely to be associated when they form part of the same community than when they don&amp;#39;t. Thus, community detection increases the parsimony of the network by identifying those groups of nodes that are most closely related to each other.&lt;/p&gt;
&lt;p&gt;
Using the example network, two communities can be visually distinguished: nodes 1–4 and nodes 5–7. Texts A, B and C belong to the first community, while texts C and D belong to the second community. Text C (nodes 2 and 5) spans the two communities of discourse. This solution is valid because each node has more connections to nodes within its own community than nodes outside it.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/digital-humanities/mix_example.communities.png&#34; alt=&#34;Example of a network with two communities&#34; title=&#34;Example of a network with two communities&#34; width=&#34;320&#34;/&gt;
&lt;figcaption&gt;
Example of a network with two communities.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
The examples&amp;#39; communities are easily detected visually, but community detection becomes more difficult as the network grows. Community detection is a mathematical process to cluster nodes into cohesive sub-networks. &lt;/p&gt;
&lt;p&gt;
Several algorithms for community detection have been developed based on a range of mathematical principles. However, the numerical validation of community structure has not yet been satisfactorily solved, and no agreed single method exists to assess the quality of communities. In my research into &lt;a href=&#34;https://lucidmanager.org/data-science/discourse-network-analysis-literature-review/&#34;&gt;knowledge networks&lt;/a&gt;, the Spinglass algorithm provided the most interpretable results.&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Community-detection&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  c &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;cluster_spinglass&lt;/span&gt;(g)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
Note that clustering algorithms only work with complete graph, which means that all nodes are connected to each other by one or more steps. If you have multiple clusters and one or more single nodes, you can decompose the graph into its connected components and analyse each of these.&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  g.comp &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;decompose&lt;/span&gt;(g)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  largest &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;which.max&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sapply&lt;/span&gt;(g.comp, diameter))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  g &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; g.comp[[largest]]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  c &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;cluster_spinglass&lt;/span&gt;(g)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
This algorithm results in a named vector that assigns a number of each community to each node. Making sense of community structure requires some human interpretation because the algorithm does not analyse the texts, only the connections between them. Please note that clustering algorithms are computationally intensive and can take a while.&lt;/p&gt;
&lt;p&gt;
We can assign a name to each community by using the node&amp;#39;s name with the most backlinks within each community. The code below creates a table of the community memberships and joins it with the centrality table. The last part assigns a name to each community by selecting the node&amp;#39;s name with the most backlinks, counts the number of members and arranges the table by community size.&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  communities &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;tibble&lt;/span&gt;(Node &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; c&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;names,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                        Community &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; c&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;membership) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;left_join&lt;/span&gt;(centrality) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;group_by&lt;/span&gt;(Community) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;summarise&lt;/span&gt;(Name &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; Node&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;[which.max&lt;/span&gt;(Backlinks)],
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;              Nodes &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;n&lt;/span&gt;()) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;arrange&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;desc&lt;/span&gt;(Nodes))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-6&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-6&#34;&gt;
Visualise Org-Roam
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-6&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;Now that we have analysed the graph, it is time to visualise the results. iGraph has extensive &lt;a href=&#34;https://igraph.org/r/doc/plot.common.html&#34;&gt;plotting functionality&lt;/a&gt; that gives full control over every aspect of the visualisation. To create the graph shown at the start of this article, use the following code:&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Visualise graph&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;par&lt;/span&gt;(mar &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;plot&lt;/span&gt;(g,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;       layout &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; layout.fruchterman.reingold,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;       mark.border &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;NA&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;       vertex.color &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; c&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;membership,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;       vertex.label &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;NA&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;       vertex.size &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sqrt&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;degree&lt;/span&gt;(g, mode &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;in&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;),
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;       vertex.frame.color &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;NA&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;       edge.arrow.size &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;       edge.color &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;lightgrey&amp;#34;&lt;/span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
The layout of graphs in this example follows the Fruchterman-Reingold method. This method tries to position the nodes so that the ones with the highest centrality are in the centre. Several other methods are available to compute an optimal layout.&lt;/p&gt;
&lt;p&gt;
The colour of each node relates to its community. The node&amp;#39;s size is the square root of the number of backlinks (indegree) plus one so that larger nodes don&amp;#39;t overpower the picture and small nodes vanish. &lt;/p&gt;
&lt;p&gt;
The remainder of the options improve the readability of the graph. This image is aesthetically pleasing, but it is not very informative. Adding text to the nodes would make it unreadable. &lt;/p&gt;
&lt;div id=&#34;outline-container-headline-7&#34; class=&#34;outline-3&#34;&gt;
&lt;h3 id=&#34;headline-7&#34;&gt;
Subnetworks
&lt;/h3&gt;
&lt;div id=&#34;outline-text-headline-7&#34; class=&#34;outline-text-3&#34;&gt;
&lt;p&gt;When a network is fairly large it might be more informative to visualise only a section. The code below determines the number of the relevant node. The &lt;code class=&#34;verbatim&#34;&gt;V()&lt;/code&gt; function holds the properties of al vertices (the nodes) in the network. The &lt;code class=&#34;verbatim&#34;&gt;make_ego_graph()&lt;/code&gt; subsets a network from a given node (ego) and the order. This function creates a list of networks as you can enter more than one node. &lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Subnetwork&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  node &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;which&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;names&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;V&lt;/span&gt;(g)) &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Topology&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  g1 &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;make_ego_graph&lt;/span&gt;(g, order&lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;, nodes &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; node)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  c1 &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;cluster_walktrap&lt;/span&gt;(g1[[1]])
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;plot&lt;/span&gt;(g1[[1]], layout &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; layout.kamada.kawai,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;       mark.border &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;NA&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;       vertex.size &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sqrt&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;degree&lt;/span&gt;(g1[[1]])) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;       vertex.color &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; c1&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;membership,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;       vertex.frame.color &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;NA&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;       vertex.label.family &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Sanserif&amp;#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;       vertex.label.color &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;black&amp;#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;       edge.arrow.size &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;.3&lt;/span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/emacs/org-roam-subnetwork.png&#34; alt=&#34;/images/emacs/org-roam-subnetwork.png&#34; title=&#34;/images/emacs/org-roam-subnetwork.png&#34; /&gt;&lt;figcaption&gt;
Subnetwork example.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-8&#34; class=&#34;outline-3&#34;&gt;
&lt;h3 id=&#34;headline-8&#34;&gt;
Interactive visualisation
&lt;/h3&gt;
&lt;div id=&#34;outline-text-headline-8&#34; class=&#34;outline-text-3&#34;&gt;
&lt;p&gt;The &lt;a href=&#34;https://christophergandrud.github.io/networkD3/&#34;&gt;networkD3 package&lt;/a&gt; can create interactive graphs, as implemented in this code snippet. The first step converts the iGraph object to a D3 object. Then, the htmlwidgets package lets you save this object as an interactive HTML file.&lt;/p&gt;
&lt;p&gt;
The code below adds the number of backlinks to the node table. Then, it defines the legend using the community names described above.&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Interactive visualisation&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(networkD3)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(htmlwidgets)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Convert to object suitable for networkD3&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  d3 &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;igraph_to_networkD3&lt;/span&gt;(g, group &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; communities&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;Name&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;[membership&lt;/span&gt;(c)])
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  d3&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;nodes&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;backlinks &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;degree&lt;/span&gt;(g, mode &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;in&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Create force directed network plot&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  p &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;forceNetwork&lt;/span&gt;(Links &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; d3&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;links, Nodes &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; d3&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;nodes, 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                    Source &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;source&amp;#34;&lt;/span&gt;, Target &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;target&amp;#34;&lt;/span&gt;, 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                    NodeID &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;name&amp;#34;&lt;/span&gt;, Group &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;group&amp;#34;&lt;/span&gt;, 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                    Nodesize &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;backlinks&amp;#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                    zoom &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;TRUE&lt;/span&gt;, legend &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;TRUE&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;saveWidget&lt;/span&gt;(p, file&lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;paste0&lt;/span&gt;( &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;getwd&lt;/span&gt;(), &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;/org-roam.html&amp;#34;&lt;/span&gt;))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-9&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-9&#34;&gt;
Further Analysis
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-9&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;Graph theory is a broad field with lots of approaches to analyse graphs. This article only scathes the surface of what you can find out about your personal knowledge garden. Graph theory is a vital tool in many applications, including the fight against the Coronavirus.&lt;/p&gt;
&lt;figure&gt;
&lt;div style=&#34;position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden;&#34;&gt;
      &lt;iframe allow=&#34;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share; fullscreen&#34; loading=&#34;eager&#34; referrerpolicy=&#34;strict-origin-when-cross-origin&#34; src=&#34;https://www.youtube.com/embed/lJHcg5rRAJQ?autoplay=0&amp;amp;controls=1&amp;amp;end=0&amp;amp;loop=0&amp;amp;mute=0&amp;amp;start=0&#34; style=&#34;position: absolute; top: 0; left: 0; width: 100%; height: 100%; border:0;&#34; title=&#34;YouTube video&#34;&gt;&lt;/iframe&gt;
    &lt;/div&gt;

&lt;figcaption&gt;
Graph theory lectures by Anthony Bosman.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
&lt;a href = &#34;https://www.r-bloggers.com/&#34; target=&#34;_blank&#34; title=&#34;Proudly associated with R-Bloggers&#34;&gt;
  &lt;button class=&#34;button is-link is-medium&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-r-project&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span&gt;As seen on R Bloggers&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;

      </description>
    </item>
    
    
    
    <item>
      <title>Generate H-Fractals using ggplot</title>
      <link>https://lucidmanager.org/data-science/generate-h-fractals-ggplot/</link>
      <pubDate>Fri, 04 Jun 2021 00:00:00 +0000</pubDate>
      <author>Peter Prevos</author>
      <guid>https://lucidmanager.org/data-science/generate-h-fractals-ggplot/</guid>
      <description>
	
&lt;p&gt;
A fractal is a geometric figure in which the same pattern repeats infinitely at an ever-smaller scale. One of the most simple examples is the H-Fractal. You construct a H-Fractal by starting with the line segments that resemble the capital letter H. You then iteratively place smaller H’s centred at the top and bottom of each free line segment. This article shows how to generate H-Fractals with the R language.&lt;/p&gt;
&lt;p&gt;
The H-Fractal or H-Tree is a self-similar fractal with ever-smaller line segments. This fractal is a Paeno Curve, which means that it eventually will fill a whole area and becomes a 2-dimensional object. The H-Fractal is a canopy fractal, a type of graph where a line segment is split into smaller lines at the end. Fractal canopies are very useful in creating models of plants. &lt;/p&gt;
&lt;p&gt;
The H-Fractal principle has practical applications in electronics. For example, microstrip antennae are printed on circuit boards and resemble the tree structure in this fractal. In addition, the splitting of fractal canopies is similar to the structure of bronchial tubes in the human body. A &lt;a href=&#34;https://patents.google.com/patent/US20070236406&#34;&gt;US Patent from 2006&lt;/a&gt; lists a H-Fractal as the geometry for a microwave antenna.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/fractals/h-fractal-5.png&#34; alt=&#34;H-Fractal with five iterations&#34; title=&#34;H-Fractal with five iterations.&#34;/&gt;
&lt;figcaption&gt;
H-Fractal with five iterations.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;div id=&#34;outline-container-headline-1&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-1&#34;&gt;
Method
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-1&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The algorithm starts with a horizontal line on the interval $[-1,1]$. Each iteration adds new shortened lines to the ends of the existing lines. The first iteration results in a shape that resembles the letter H. After that, the direction of the lines switches at each iteration from horizontal to vertical.&lt;/p&gt;
&lt;p&gt;
If the number of iterations of the fractal is $p$, then each iteration adds $2^p$ lines to the tree, and the total number of lines in the fractal is $2^{p+1} + 1$.  &lt;/p&gt;
&lt;p&gt;
The new lines are shortened by a factor, which is set at $\frac{1}{\sqrt{2}}$. If you set it any higher, the lines will overlap—a lower value results in a more sparse design.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-2&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-2&#34;&gt;
Generate H-Fractals with ggplot
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-2&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The code below starts with setting the initial variables and defines the first line in a data frame. The data frame holds the start and end coordinates and the iteration number.&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(ggplot2)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## H-Fractal Parameters&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  p &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;8&lt;/span&gt; &lt;span style=&#34;color:#888&#34;&gt;## Number of iterations&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  a &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sqrt&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;) &lt;span style=&#34;color:#888&#34;&gt;## Shortening parameter&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Define horizontal starting line&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  h_fractal &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;data.frame&lt;/span&gt;(x1 &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;-1&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                          y1 &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                          x2 &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                          y2 &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                          m &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
The loop defines the iterations and adds a new row to the data frame for each line. First, the direction flips by setting the &lt;code class=&#34;verbatim&#34;&gt;dx&lt;/code&gt; and &lt;code class=&#34;verbatim&#34;&gt;dy&lt;/code&gt; values to either 0 or 1. The second part defines the row numbers for the previously defined lines and the new lines. The code then calculates the coordinates for the new line segments.&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;# Generate coordinates (each line on a row)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;for&lt;/span&gt; (m &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;in&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;p) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#888&#34;&gt;## Flip direction&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    dx &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; (m &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;%%&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    dy &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; m &lt;span style=&#34;color:#333&#34;&gt;%%&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#888&#34;&gt;# Previous and current line numbers&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    i &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; (&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;^&lt;/span&gt;(m &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;))&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;^m &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    j &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; (&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;^m)&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;^&lt;/span&gt;(m &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#888&#34;&gt;## Calculate coordinates&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    h_fractal[j, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;x1&amp;#34;&lt;/span&gt;] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(h_fractal[i, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;x1&amp;#34;&lt;/span&gt;] &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; dx &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; a^m, h_fractal[i, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;x2&amp;#34;&lt;/span&gt;] &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; dx &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; a^m)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    h_fractal[j, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;x2&amp;#34;&lt;/span&gt;] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(h_fractal[i, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;x1&amp;#34;&lt;/span&gt;] &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; dx &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; a^m, h_fractal[i, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;x2&amp;#34;&lt;/span&gt;] &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; dx &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; a^m)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    h_fractal[j, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;y1&amp;#34;&lt;/span&gt;] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(h_fractal[i, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;y1&amp;#34;&lt;/span&gt;] &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; dy &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; a^m, h_fractal[i, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;y2&amp;#34;&lt;/span&gt;] &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; dy &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; a^m)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    h_fractal[j, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;y2&amp;#34;&lt;/span&gt;] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(h_fractal[i, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;y1&amp;#34;&lt;/span&gt;] &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; dy &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; a^m, h_fractal[i, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;y2&amp;#34;&lt;/span&gt;] &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; dy &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; a^m)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#888&#34;&gt;## Set line property&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    h_fractal[j, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;m&amp;#34;&lt;/span&gt;] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; m
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
Lastly, The fractal is visualised with ggplot and the line segment geometry. The colour of the line slowly fades by setting it to the iteration number.&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#888&#34;&gt;## Visualise&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ggplot&lt;/span&gt;(h_fractal) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;aes&lt;/span&gt;(x &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; x1, y &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; y1, xend &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; x2, yend &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; y2,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        col &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; m) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;geom_segment&lt;/span&gt;(size &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;.5&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;scale_size&lt;/span&gt;(guide &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;NULL&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;scale_colour_gradientn&lt;/span&gt;(colours &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;gray.colors&lt;/span&gt;(p), guide &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;NULL&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;theme_void&lt;/span&gt;()&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/fractals/h-fractal.png&#34; alt=&#34;H-Fractal example with twelve iterations&#34; title=&#34;H-Fractal example with twelve iterations&#34;/&gt;
&lt;figcaption&gt;
H-Fractal with twelve iterations.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-3&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-3&#34;&gt;
Variations on the Theme
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-3&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;Besides changing the number of iterations and the shortening factor, you can also play with line thickness, colours, and the lines’ direction. This &lt;a href=&#34;https://prevos.shinyapps.io/rfractals/&#34;&gt;Shiny application&lt;/a&gt; lets you play with all variables in this algorithm to generate&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://www.r-bloggers.com/&#34; target=&#34;_blank&#34; title=&#34;Proudly associated with R-Bloggers&#34;&gt;
  &lt;button class=&#34;button is-link is-medium&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-r-project&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span&gt;As seen on R Bloggers&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;

      </description>
    </item>
    
    
    
    <item>
      <title>Tantalising Games in R: Fun with Statistical Computing</title>
      <link>https://lucidmanager.org/data-science/games-in-r/</link>
      <pubDate>Sat, 26 Dec 2020 00:00:00 +0000</pubDate>
      <author>Peter Prevos</author>
      <guid>https://lucidmanager.org/data-science/games-in-r/</guid>
      <description>
	
&lt;p&gt;
Computers exist to solve pressing problems to improve the human condition. Although our world is full of urgent issues to address, the human need for play is a robust internal motivator. This general statement also applies to the &lt;em&gt;R Project for Statistical Computing&lt;/em&gt;. Analysing data analysis is a serious business without much room for fun. This specialised computer language is not designed for games in R.&lt;/p&gt;
&lt;p&gt;
Hacking culture is one of the main reasons that computer science has evolved so fast. One of the many definitions of hacking is to use technology for a purpose it was not designed for, and thus writing games in the R language is an exciting challenge.&lt;/p&gt;
&lt;p&gt;
Not many R developers have written games. R has a &lt;a href=&#34;https://cran.r-project.org/web/packages/games/&#34;&gt;games package&lt;/a&gt; in CRAN, but this library is about the mathematics of game theory. This article explains the R language’s possibilities and limitations for writing games, plus links to actual games others and myself have developed.&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-1&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-1&#34;&gt;
Action Games
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-1&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;Fully functioning action or arcade games are not easy to create in the R language because it has no interrupt function to process input from a keyboard or joystick while the battle continues. R can process keyboard input and mouse clicks on a graph, but the game waits for input. This makes it impossible to have moving elements on a screen.&lt;/p&gt;
&lt;p&gt;
The other impediment in R to write arcade games is the lack of support for sound. The &lt;a href=&#34;https://cran.r-project.org/web/packages/beepr/index.html&#34;&gt;beepr package&lt;/a&gt; by Rasmus Bååth plays a sound after a process has completed. The &lt;code class=&#34;verbatim&#34;&gt;beep&lt;/code&gt; function in this package can play eleven different sounds. However, R can only do one thing at a time, so paying a sound stops any other gameplay.&lt;/p&gt;
&lt;p&gt;
The &lt;a href=&#34;https://lucidmanager.org/data-science/creepy-computer-games-in-r/&#34;&gt;Gravedigger game&lt;/a&gt; from the 1980s book &lt;em&gt;Creepy Computer Games&lt;/em&gt; is a pseudo arcade game that was initially developed for the venerable ZX81, my first computer. In this game, the antagonist only moves once after the player moved. It is thus as much a strategy game as it is an action game.&lt;/p&gt;
&lt;p&gt;
MattSen has interpreted of the classic ZX81 game 3D Monster Maze in the R language. &lt;a href=&#34;https://github.com/MattSen/Games-in-R&#34;&gt;His version&lt;/a&gt; of this game is a cross between an action and an adventure game. &lt;/p&gt;
&lt;p&gt;
Although R is unsuitable for arcade games, I have managed to simulate the classic game of &lt;a href=&#34;https://lucidmanager.org/data-science/pong/&#34;&gt;Pong&lt;/a&gt;. This code simulates a player that follows the path of the ball. Since I wrote this rudimentary Pong version, two people have developed exciting functionality to make arcade games possible.&lt;/p&gt;
&lt;p&gt;
Carsten Croonenbroek has developed the classic &lt;a href=&#34;https://cran.r-project.org/web/packages/Snake/index.html&#34;&gt;Snake game&lt;/a&gt;. This package uses tcltk to obtain user interaction via callback functions (almost as flexible as a real system-level message pump). Perhaps we can generalise the code in this package to create a joystick function in other games.&lt;/p&gt;
&lt;p&gt;
Stephen Kozak wrote a library that uses a Windows executable to constantly check for keyboard input. The &lt;a href=&#34;https://github.com/Teggom/KeyboardCheckeR&#34;&gt;KeyboardCheckR&lt;/a&gt; provides the &lt;code class=&#34;verbatim&#34;&gt;QueryKeyboard()&lt;/code&gt; function. This package is not on CRAN, and you will need to use devtools to install it.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-2&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-2&#34;&gt;
Strategy Games
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-2&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;Developing software to play strategic games has been an objective of artificial intelligence research from the early days of computing. Deep Blue defeating Gary Kasparov in 1997 was one of the significant milestones in the history of computing.&lt;/p&gt;
&lt;p&gt;
More recently, Google’s AlphaGo beat the European Go champion, Fan Hui. Deep Blue and AlphaGo used quite different approaches. Whereas Deep Blue used traditional brute force, AlphaGo uses deep learning. Strategic games are ideal for the R language because of its specialised functionalities in statistical computing and machine learning.&lt;/p&gt;
&lt;p&gt;
You can play the famous combinatorial number-placement puzzle with the &lt;a href=&#34;https://cran.r-project.org/web/packages/sudoku/index.html&#34;&gt;Sudoku package&lt;/a&gt;. This library generates, plays, and solves Sudoku puzzles. The &lt;code class=&#34;verbatim&#34;&gt;playSudoku()&lt;/code&gt; graphical version depends on &lt;code class=&#34;verbatim&#34;&gt;tkrplot&lt;/code&gt;, if you are not on Windows. The &lt;code class=&#34;verbatim&#34;&gt;sudokuALT&lt;/code&gt; package provides an alternative. This package presents more tools, such as plot and print methods.&lt;/p&gt;
&lt;p&gt;
The &lt;a href=&#34;https://cran.r-project.org/web/packages/rchess/index.html&#34;&gt;rchess package&lt;/a&gt; by Joshua Kunst provides some functionality to play chess, but it is not a fully playable game. This package can plot beautiful chess boards, move pieces and detect check. However, this package is not able to generate moves by the computer and does not include a playable interface.&lt;/p&gt;
&lt;p&gt;
&lt;a href=&#34;https://lucidmanager.org/data-science/tic-tac-toe-war-games/&#34;&gt;Tic Tac Toe&lt;/a&gt;, or Noughts and Crosses is one of the most accessible strategic board games. The minimax algorithm can easily play a perfect game, which means that the computer can never beat itself. The Tic Tac Toe game in R uses the locator function to manage the human player. This function reads the position of the graphics cursor when the user presses the left mouse button.&lt;/p&gt;
&lt;p&gt;
Roland Krasser developed a library to play &lt;a href=&#34;https://rolkra.github.io/R-Console-Gaming/&#34;&gt;Codebreaker&lt;/a&gt;, inspired by the classic Mastermind game and classic 1980s design. He also developed &lt;a href=&#34;https://github.com/rolkra/pixelpuzzle&#34;&gt;Pixelpuzzle&lt;/a&gt;, both games are available on CRAN. MattSen has also developed a version of this game, called &lt;a href=&#34;https://github.com/MattSen/Games-in-R&#34;&gt;Bulls and Crows&lt;/a&gt;.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/rgames/pixelpuzzle-allstars.png&#34; alt=&#34;/images/rgames/pixelpuzzle-allstars.png&#34; title=&#34;/images/rgames/pixelpuzzle-allstars.png&#34; /&gt;&lt;figcaption&gt;
Pixelpuzzle by Roland Krasser.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-3&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-3&#34;&gt;
Adventure Games
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-3&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;A text adventure is a form of interactive fiction where a player can freely explore a virtual world described in prose. These games were trendy in the 1980s. The basic principle is that the computer shows a text that describes the current situation. The player enters a text command, usually a simple verb-noun combination. The computer processes the command and displays the new status. This interchange continues until the game reaches a defined state. Either the player dies, or the player has achieved the game’s objectives.&lt;/p&gt;
&lt;p&gt;
Most games are set in typical Dungeons and Dragons settings with wizards, dragons and other usual characters. The lines below are a short extract from &lt;em&gt;Colossal Cave&lt;/em&gt;, by Will Crowther and Don Woods, the very first text adventure written in the mid-1970s:&lt;/p&gt;
&lt;div class=&#34;src src-text&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-text&#34; data-lang=&#34;text&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;You are standing at the end of a road before a small brick building. 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;Around you is a forest. A small stream flows out of the building and down a gully.
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&amp;gt; go south 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;You are in a valley in the forest beside a stream tumbling along a rocky bed.
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&amp;gt; enter stream 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;Your feet are now wet.&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
The &lt;a href=&#34;https://www.r-bloggers.com/2016/06/frequency-analysis-challenge-a-console-based-game-for-rpython/&#34;&gt;BetaBit package&lt;/a&gt; by Smart Poland provides an educational console game where the user has to crack codes. This game comes close to the look-and-feel of traditional text adventures but is not quite the same.&lt;/p&gt;
&lt;p&gt;
This website has two adventure games. The &lt;a href=&#34;https://lucidmanager.org/data-science/tenliner-cave-adventure/&#34;&gt;Tenliner Cave Adventure&lt;/a&gt; is a programming challenge to cram as much functionality as possible into ten lines of BASIC code. This version uses the minimal amount of R code to recreate this masterpiece of programming.&lt;/p&gt;
&lt;p&gt;
My &lt;a href=&#34;https://lucidmanager.org/data-science/text-adventure/&#34;&gt;Secret of Landusia&lt;/a&gt; game provides a fully-functioning set of functions to write classic adventure games. The code can be easily expanded or modified to create new stories.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-4&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-4&#34;&gt;
Games in R: Having fun with the R Language
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-4&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;R is not an optimal language for writing computer entertainment. Multipurpose languages such as Python or Java are much more suited to meet the expectations of gamers.&lt;/p&gt;
&lt;p&gt;
The many limitations don’t hold me back from writing computer games in the R language because working with limitations create a significant challenge. The &lt;em&gt;Devil is in the Data&lt;/em&gt; irregularly publishes code to have fun with R. Subscribe to the blog to receive updates or follow the Rgames repository on Github.&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://github.com/pprevos/RGames/&#34; target=&#34;_blank&#34;
   title=&#34;Download RGames from GitHub&#34;
   alt=&#34;Download RGames from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;RGames&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/p&gt;
&lt;p&gt;
If you know of any other games written in R, then please &lt;a href=&#34;https://lucidmanager.org/contact/&#34;&gt;contact me&lt;/a&gt;. I would love to create a complete compendium of how to have fun in this quirky computing language.&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://www.r-bloggers.com/&#34; target=&#34;_blank&#34; title=&#34;Proudly associated with R-Bloggers&#34;&gt;
  &lt;button class=&#34;button is-link is-medium&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-r-project&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span&gt;As seen on R Bloggers&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;

      </description>
    </item>
    
    
    
    <item>
      <title>Tenliner Cave Adventure in R: Miniature Text Adventure</title>
      <link>https://lucidmanager.org/data-science/tenliner-cave-adventure/</link>
      <pubDate>Wed, 19 Aug 2020 00:00:00 +0000</pubDate>
      <author>Peter Prevos</author>
      <guid>https://lucidmanager.org/data-science/tenliner-cave-adventure/</guid>
      <description>
	
&lt;p&gt;
Computing has come a long way in the past 75 years. I learned how to write code in the 1980s on my tiny 1 kilobyte ZX81 home computer. With only 1 kilobyte of memory, capabilities were limited, but this machine set me off on an adventure writing code in the BASIC language. An active subculture of computer enthusiasts still develops new code and keeps these old machines alive. I recently stumbled across the fantastic &lt;a href=&#34;https://spectrumcomputing.co.uk/index.php?cat=96&amp;amp;id=30392&#34;&gt;Tenliner Cave Adventure&lt;/a&gt; (2016) by Einar Saukas from Brazil. This tiny ZX81 program provides a fully functional classical text adventure using some cunning techniques. This article contains a port of this game to the R language to explore its inner workings.&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-1&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-1&#34;&gt;
Interactive Fiction
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-1&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;Text adventures, or interactive fiction, were trendy in the 1980s. The basic premise is that the computer shows a text and the player responds with a text command. The computer parses the command, calculates the new state of play and progresses the game with a new bit of prose. Last year I wrote an article about writing interactive fiction using the R language with a translation of &lt;a href=&#34;https://lucidmanager.org/data-science/text-adventure/&#34;&gt;Secret of Landusia&lt;/a&gt;. The &lt;em&gt;Tenliner Cave Adventure&lt;/em&gt;  provides a minimalist approach by squashing the basic principles of a text adventure into 1024 bytes of memory. The backstory of this Saukas&amp;#39; mini-adventure is a typical fantasy setting:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;As a young warrior apprentice, you have been chosen by the village elders to seek out the evil menace that lurks in some nearby caves. Once found, use any means at your disposal to defeat it… Good luck on your quest!&lt;/p&gt;
&lt;/blockquote&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/rgames/zx81.png&#34; alt=&#34;ZX81 Screenshot of the Tenliner Cave Adventure&#34; title=&#34;ZX81 Screenshot of the Tenliner Cave Adventure&#34;/&gt;
&lt;figcaption&gt;
ZX81 Screenshot of the Tenliner Cave Adventure.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-2&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-2&#34;&gt;
Tenliner Cave Adventure in R
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-2&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The Tenliner Cave Adventure is a jewel of efficient programming. The game is simple, but it has all typical elements of a text adventure.  As I no longer own a ZX81, I could not resist translating this code to R to be able to play and reverse-engineer the game. &lt;/p&gt;
&lt;p&gt;
The game recognises the following commands:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;north, south, east, west (move)&lt;/li&gt;
&lt;li&gt;inventory&lt;/li&gt;
&lt;li&gt;look&lt;/li&gt;
&lt;li&gt;look &amp;lt;object&amp;gt;&lt;/li&gt;
&lt;li&gt;get &amp;lt;object&amp;gt;&lt;/li&gt;
&lt;li&gt;open &amp;lt;object&amp;gt;&lt;/li&gt;
&lt;li&gt;kill &amp;lt;object&amp;gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Most advanced games understand abbreviations and synonyms, but in this simple version, you have to provide precise commands. Can you rid the village of the evil menace?&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://github.com/pprevos/RGames/&#34; target=&#34;_blank&#34;
   title=&#34;Download RGames from GitHub&#34;
   alt=&#34;Download RGames from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;RGames&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Tenliner Cace Adventure by Einar Saukas&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Ported from ZX81 BASIC to R by Peter Prevos&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Helper functions to keep syntax close to ZX81&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  val &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;function&lt;/span&gt;(p) &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;as.numeric&lt;/span&gt;(p)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  x &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;function&lt;/span&gt;(p, q &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; p) &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;substr&lt;/span&gt;(X, p, q)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Ported ZX81 code&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  X &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;100cannot doyou walk opened   closed   a sword  a key    nothing  a chest  a dragon a corpse taken    you died you won. &amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;repeat&lt;/span&gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;cat&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;paste&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;You are in a&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;substr&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;cavepit halllake&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;val&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;x&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;)) &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;4&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;3&lt;/span&gt;, &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;val&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;x&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;)) &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;4&lt;/span&gt;), &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;\n&amp;#34;&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      u &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;tolower&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;readline&lt;/span&gt;(prompt &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;What would you like to do? &amp;#34;&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      m &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; (u &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;north&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; (&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;x&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;3&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; (u &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;south&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; (&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;x&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;&amp;gt;&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;2&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        (&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;x&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;2&amp;#34;&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;&amp;amp;&lt;/span&gt; u &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;west&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        (&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;x&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;3&amp;#34;&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;&amp;amp;&lt;/span&gt; u &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;east&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      a &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; (&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;3&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;val&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;x&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;)) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; (&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;x&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;3&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;2&amp;#34;&lt;/span&gt;)) &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; (&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;x&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;&amp;amp;&lt;/span&gt; u &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;look chest&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        (&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;11&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; (&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;x&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;3&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;2&amp;#34;&lt;/span&gt;)) &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; (&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;x&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;3&amp;#34;&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;&amp;amp;&lt;/span&gt; u &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;kill dragon&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        (m &lt;span style=&#34;color:#333&#34;&gt;!=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        (&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;5&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;val&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;x&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;))) &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; (u &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;look&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        (&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;6&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;val&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;x&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;3&lt;/span&gt;))) &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; (u &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;inventory&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        (&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;6&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; (&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;x&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;3&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;0&amp;#34;&lt;/span&gt;)) &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; (&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;x&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;4&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;&amp;amp;&lt;/span&gt; u &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;look corpse&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; (&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;x&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;3&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;400&amp;#34;&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;&amp;amp;&lt;/span&gt; u &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;get key&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; (&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;x&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;3&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;201&amp;#34;&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;&amp;amp;&lt;/span&gt; u &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;open chest&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; (&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;x&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;3&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;211&amp;#34;&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;&amp;amp;&lt;/span&gt; u &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;get sword&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      X &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;paste0&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;val&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;x&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;)) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; m, &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;val&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;x&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;)) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; (a &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;), &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;val&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;x&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;3&lt;/span&gt;)) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; (a &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt;), &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;substring&lt;/span&gt;(X, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;4&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;cat&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;paste&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;substr&lt;/span&gt;(X, a &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;9&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;4&lt;/span&gt;, a &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;9&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;12&lt;/span&gt;), &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;\n&amp;#34;&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (a &lt;span style=&#34;color:#333&#34;&gt;&amp;gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;11&lt;/span&gt;) &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;break&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
This port of the &lt;em&gt;Ten-liner Text Adventure&lt;/em&gt; stay as close as possible to the original code by creating two helper functions, &lt;code class=&#34;verbatim&#34;&gt;val()&lt;/code&gt; and &lt;code class=&#34;verbatim&#34;&gt;x()&lt;/code&gt;. In ZX81 basic, you can subset a string with parenthesis, which is mimicked in this code with the &lt;code class=&#34;verbatim&#34;&gt;x()&lt;/code&gt; function. The &lt;code class=&#34;verbatim&#34;&gt;val()&lt;/code&gt; function in ZX81 BASIC converts a string to a value. The rest of the code is almost literally the same as the ZX81 version.&lt;/p&gt;
&lt;p&gt;
The workings of the game are ingenious and minimise memory usage on the ZX81 to a minimum. All numeric values are stored in the &lt;code class=&#34;verbatim&#34;&gt;X&lt;/code&gt; string to save memory, which also contains all game responses. The first number is the room number the player is in, the second number records whether the chest is closed or open and the third number records the objects held by the player (the inventory). The responses in the &lt;code class=&#34;verbatim&#34;&gt;X&lt;/code&gt; variable are all nine characters long. &lt;/p&gt;
&lt;p&gt;
In ZX81 BASIC, all lines have numbers to control sequencing. This version uses a &lt;code class=&#34;verbatim&#34;&gt;repeat{}&lt;/code&gt; loop that keeps running until the game ends.&lt;/p&gt;
&lt;p&gt;
The game opens with displaying the room the player is in. Next, the player is asked to provide a response.&lt;/p&gt;
&lt;p&gt;
The variable &lt;code class=&#34;verbatim&#34;&gt;m&lt;/code&gt; determines the movement of the player depending on their direction. The result is a number between -2 and 2, which indicates the difference between the current and new room. The code uses a complex boolean expression to work this out.&lt;/p&gt;
&lt;p&gt;
The variable &lt;code class=&#34;verbatim&#34;&gt;a&lt;/code&gt; determines the outcome after the command if it is not a change in direction. This is the most sophisticated line in the game. In the original listing, it took three lines to determine the new game state due to line-length restrictions. In this code, I concatenated it into one line in line with the games minimalism philosophy. This line combines the input of the player with the current game state to determine the new situation.&lt;/p&gt;
&lt;p&gt;
In the next step, the code changes the state of the game by updating the three numbers in &lt;code class=&#34;verbatim&#34;&gt;X&lt;/code&gt;. The game displays a response, depending on the content of &lt;code class=&#34;verbatim&#34;&gt;a&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;
It takes much more space to describe how this game works than the size of the code itself.&lt;/p&gt;
&lt;p&gt;
Have fun!&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://www.r-bloggers.com/&#34; target=&#34;_blank&#34; title=&#34;Proudly associated with R-Bloggers&#34;&gt;
  &lt;button class=&#34;button is-link is-medium&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-r-project&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span&gt;As seen on R Bloggers&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;

      </description>
    </item>
    
    
    
    <item>
      <title>Storytelling with Data: Visualising the Receding Sea Ice Sheets</title>
      <link>https://lucidmanager.org/data-science/storytelling-with-data/</link>
      <pubDate>Fri, 14 Aug 2020 00:00:00 +0000</pubDate>
      <author>Peter Prevos</author>
      <guid>https://lucidmanager.org/data-science/storytelling-with-data/</guid>
      <description>
	
&lt;p&gt;
Good data science has to be aesthetic to ensure that the person who consumes your data product draws the right conclusion. Storytelling with data is a craft where mathematics and aesthetics meet. For data science to provide value, it has to be &lt;a href=&#34;https://lucidmanager.org/data-science/strategic-data-science/&#34;&gt;sound, useful and aesthetic&lt;/a&gt;. The aesthetics of data science refers to the way the results are communicated. This article discusses an informative but messy graph about the extent of global sea ice. The second part describes how to improve it with the ggplot2 library.&lt;/p&gt;
&lt;p&gt;
Many data scientists discuss at length which computing language they should learn. I claim that English is the most essential language an analyst can learn (or whatever other language yous peak) and the language of art. A good visualisation is a piece of data art that is composed to achieve a purpose. Whenever somebody looks at your visualisation, you want them to reach the same conclusion as you and they should be able to do so without having to dissect the information.&lt;/p&gt;
&lt;p&gt;
I subscribe to both the &lt;a href=&#34;https://www.reddit.com/r/dataisbeautiful/comments/5ky8rr/live_chart_of_global_sea_ice_shows_2016_falling/&#34;&gt;Data is Beautiful&lt;/a&gt; and &lt;a href=&#34;https://www.reddit.com/r/dataisugly/&#34;&gt;Data is Ugly&lt;/a&gt; subreddits. While beauty might be in the eye of the beholder, aesthetic data science has to follow some rules. I subscribe to the concept that you need to maximise the data-pixel-ratio of your graphs. In other words, each pixel in your chart should ideally form part of the story. No superfluous elements, only use colour if it is part of the story. Also, the type of chart is quite important, as each geometry tells a different story. Lets put this to practice with some ggplot code.&lt;/p&gt;
&lt;p&gt;
This post has been updated based on feedback from Clark Richards as the original used the wrong data set.&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-1&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-1&#34;&gt;
Storytelling with data case study: Receding sea ice sheets
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-1&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;A little while ago, one of the posts on the &lt;a href=&#34;https://www.reddit.com/r/dataisbeautiful/comments/5ky8rr/live_chart_of_global_sea_ice_shows_2016_falling/&#34;&gt;Data is Beautiful&lt;/a&gt; subreddit showed a disturbing graph about the changes in the extent of global sea ice from 1978 till recently. The chart is created by an anonymous citizen data scientist who calls him or herself &lt;a href=&#34;https://neven1.typepad.com/&#34;&gt;Wipneus&lt;/a&gt;, which is Dutch for a snub nose and also a character in an old Dutch comic.&lt;/p&gt;
&lt;p&gt;
This graph contains all the necessary information to come to the conclusion that the total surface area of global sea ice in the has reduced significantly over the past forty years. This conclusion does, however, require quite a bit of squinting and close examination of the graph. &lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/data-science/nsidc_global_area_byyear_b.jpg&#34; alt=&#34;Global Sea Ice Area 1978--2020&#34; title=&#34;Global Sea Ice Area 1978--2020&#34;/&gt;
&lt;figcaption&gt;
Global Sea Ice Area 1978–2020.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
This graph is a typical multivariate time series where the colour of each line indicates a category. While this approach might work fine for one or two lines, the cacophony of colours makes it hard to distinguish which line belongs to which year. This graph would be impossible to interpret for the eight per cent of men who are colour blind. Also, the story this graph tells is confusing. While the story seems to be that sea ice sheets are melting the past forty years, the chart shows the seasonal variations.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-2&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-2&#34;&gt;
Storytelling with data and ggplot
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-2&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;To redesign this graph, we first need to define what the story is we are telling with this data. The original chart shows that the area of the global sea ice is receding over the past forty years. This means that our dependent variable (y-axis) is the surface area, and the independent variable (x-axis) is the year.  &lt;/p&gt;
&lt;p&gt;
The original graph shows the month as the independent variable. The sea ice sheets grows and shrink with the seasons, as the sinusoidal shape in the chart indicates. While this is an interesting pattern, it is not the story we want to tell with this data. To show the influence over time, the year should be the independent variable and perhaps a colour for each month. To prevent a palette with twelve colours, it is probably better to use a grid for each month. The graph below is my proposed improved version. &lt;/p&gt;
&lt;p&gt;
The ribbon indicates the maximum and minimum extent of ice for each month over the years. For many months, the maximum amount of ice in recent years is about the same as the minimum amount at the start of this time series. The dark blue line is the linear regression of the mean sea ice area, which clearly indicates the downward trend.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/data-science/ice-data-beautiful.png&#34; alt=&#34;Global Sea Ice Area 1978-2020 for each month of the year&#34; title=&#34;Global Sea Ice Area 1978-2020 for each month of the year&#34;/&gt;
&lt;figcaption&gt;
Global Sea Ice Area 1978-2020 for each month of the year.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
Good storytelling with data requires that you first define the conclusion you want the consumer of your data product to draw. The next step is to identify the dependent and independent variables and perhaps some grouping as well. Only after you can clearly define these aspects can you choose the best geometry. There are many chart choosers available on the web that help you with this choice.&lt;/p&gt;
&lt;p&gt;
The code below loads the data from the website and transforms it into a tidy format, calculates the minimum, mean and maximum sea ice area and visualises the results.&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(dplyr)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(forcats)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(readr)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(ggplot2)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;download.file&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;https://sites.google.com/site/arctischepinguin/home/sea-ice-extent-area/data/nsidc_global_nt_final_and_nrt.txt.gz&amp;#34;&lt;/span&gt;, destfile &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;global-sea-ice.gz&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;system&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;gunzip global-sea-ice.gz&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  sea_ice &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;read_csv&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;global-sea-ice&amp;#34;&lt;/span&gt;, skip &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;21&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Original plot&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ggplot&lt;/span&gt;(sea_ice, &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;aes&lt;/span&gt;(yearday, area, col &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;format&lt;/span&gt;(date, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;%Y&amp;#34;&lt;/span&gt;))) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;geom_line&lt;/span&gt;() &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;scale_color_discrete&lt;/span&gt;(name &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;labs&lt;/span&gt;(title &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Global Sea Ice Area&amp;#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;         subtitle &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;from NSIDC NASA Team sea ice concentration data&amp;#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;         y &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;expression&lt;/span&gt;(Sea&lt;span style=&#34;color:#333&#34;&gt;~&lt;/span&gt;Ice&lt;span style=&#34;color:#333&#34;&gt;~&lt;/span&gt;Area&lt;span style=&#34;color:#333&#34;&gt;~&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt;^6&lt;span style=&#34;color:#333&#34;&gt;~&lt;/span&gt;km^2))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Ribbon version&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  sea_ice_min_max &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; sea_ice &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;mutate&lt;/span&gt;(month &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;fct_relevel&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;as.factor&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;format&lt;/span&gt;(date, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;%b&amp;#34;&lt;/span&gt;)), month.abb),
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;           year &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;as.numeric&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;format&lt;/span&gt;(date, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;%Y&amp;#34;&lt;/span&gt;))) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;group_by&lt;/span&gt;(year, month) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;summarise&lt;/span&gt;(min_area &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;min&lt;/span&gt;(area, na.rm &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;TRUE&lt;/span&gt;),
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;              mean_area &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;mean&lt;/span&gt;(area, na.rm &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;TRUE&lt;/span&gt;),    
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;              max_area &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;max&lt;/span&gt;(area, na.rm &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;TRUE&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ggplot&lt;/span&gt;(sea_ice_min_max, &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;aes&lt;/span&gt;(x &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; year, ymin &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; min_area, ymax &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; max_area)) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;geom_ribbon&lt;/span&gt;(fill &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;dodgerblue&amp;#34;&lt;/span&gt;, alpha &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;.5&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;scale_y_continuous&lt;/span&gt;(breaks &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;seq&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;12&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;24&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;)) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;geom_smooth&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;aes&lt;/span&gt;(y &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; mean_area), se &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;FALSE&lt;/span&gt;, method &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;lm&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;facet_wrap&lt;/span&gt;(&lt;span style=&#34;color:#333&#34;&gt;~&lt;/span&gt;month) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;labs&lt;/span&gt;(title &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Global Sea Ice Area&amp;#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;         subtitle &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;from NSIDC NASA Team sea ice concentration data\nMinimum and maximum sea ice area and linear regression over the mean&amp;#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;         y &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;expression&lt;/span&gt;(Sea&lt;span style=&#34;color:#333&#34;&gt;~&lt;/span&gt;Ice&lt;span style=&#34;color:#333&#34;&gt;~&lt;/span&gt;Area&lt;span style=&#34;color:#333&#34;&gt;~&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt;^6&lt;span style=&#34;color:#333&#34;&gt;~&lt;/span&gt;km^2)) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;theme_light&lt;/span&gt;(base_size &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;8&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;theme&lt;/span&gt;(panel.background &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;element_rect&lt;/span&gt;(fill &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;#f5f5dc&amp;#34;&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ggsave&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;../../static/images/data-science/ice-data-beautiful.png&amp;#34;&lt;/span&gt;, width &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;6&lt;/span&gt;, height &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;4&lt;/span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
&lt;a href = &#34;https://www.r-bloggers.com/&#34; target=&#34;_blank&#34; title=&#34;Proudly associated with R-Bloggers&#34;&gt;
  &lt;button class=&#34;button is-link is-medium&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-r-project&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span&gt;As seen on R Bloggers&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;
&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-3&#34; class=&#34;outline-3&#34;&gt;
&lt;h3 id=&#34;headline-3&#34;&gt;
Strategic Data Science
&lt;/h3&gt;
&lt;div id=&#34;outline-text-headline-3&#34; class=&#34;outline-text-3&#34;&gt;
&lt;p&gt;If you like to know more about this topic and how to use data science to create business value, perhaps you like to read my book &lt;em&gt;Principles of Strategic Data Science&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;

  &lt;div class=&#34;box&#34;&gt;
    &lt;div class=&#34;media&#34;&gt;
      &lt;figure class=&#34;media-left&#34;&gt;
        &lt;p class=&#34;image is-128x128&#34;&gt;
          &lt;img src=&#34;https://lucidmanager.org/images/books/2019_sds.jpg&#34; alt = &#34;Principles of Strategic Data Science&#34; title = &#34;Principles of Strategic Data Science&#34;&gt;
        &lt;/p&gt;
      &lt;/figure&gt;
      &lt;div class=&#34;media-content&#34;&gt;
        &lt;div class=&#34;content&#34;&gt;
          &lt;p class=&#34;is-size-5 has-text-weight-bold&#34;&gt;Principles of Strategic Data Science&lt;/p&gt;
          &lt;p class=&#34;mb-2&#34;&gt;Principles of Strategic Data Science helps you join the dots between mathematics, programming, and business analysis. With a unique approach that bridges the gap between mathematics and computer science, this book takes you through the entire data science pipeline.&lt;/p&gt;
          &lt;div class=&#34;buttons&#34;&gt;
            &lt;a class=&#34;button is-info&#34; href=&#34;https://www.packtpub.com/en-us/product/principles-of-strategic-data-science-9781838985509&#34; target=&#34;_blank&#34;&gt;
              &lt;span class=&#34;icon&#34;&gt;
                &lt;i class=&#34;fas fa-shopping-cart&#34;&gt;&lt;/i&gt;
              &lt;/span&gt;
              &lt;span&gt;Packt Publishing&lt;/span&gt;
            &lt;/a&gt;
          &lt;/div&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

      </description>
    </item>
    
    
    
    <item>
      <title>Cheesecake Diagrams: Pie Charts with a Different Flavour</title>
      <link>https://lucidmanager.org/data-science/cheesecake-diagram/</link>
      <pubDate>Wed, 05 Aug 2020 00:00:00 +0000</pubDate>
      <author>Peter Prevos</author>
      <guid>https://lucidmanager.org/data-science/cheesecake-diagram/</guid>
      <description>
	
&lt;p&gt;
Part of my job at a regional water utility involves visualising operational data. We manage water and sewerage services for a large number of small and medium-sized towns in regional Victoria (Australia).  Traditionally, performance reports consist of extensive tables filled with numbers with a line for each city. To make this data easier to consume, I developed the cheesecake diagram to spatially visualise performance data. A cheesecake diagram is just like a pie chart, but different.&lt;/p&gt;
&lt;p&gt;
The code for this article is available on GitHub:&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://github.com/pprevos/r4h2o/&#34; target=&#34;_blank&#34;
   title=&#34;Download r4h2o from GitHub&#34;
   alt=&#34;Download r4h2o from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;r4h2o&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-1&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-1&#34;&gt;
Reporting performance spatially
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-1&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;Displaying geographic performance requires geometric objects at appropriate locations. The  &lt;a href=&#34;https://lucidmanager.org/data-science/geographic-bubble-chart/&#34;&gt;geographic bubble chart&lt;/a&gt; visualises a single quantitative variable through the size of the circles, plus a second qualitative variable through the colour.&lt;/p&gt;
&lt;p&gt;
The performance data is randomised for the purpose of this example. Performance is measured using four aggregated parameters. My paper on &lt;a href=&#34;https://www.researchgate.net/publication/326533569_Visualising_water_quality_A_graphical_index_for_drinking_water_system_performance&#34;&gt;visualising water quality performance&lt;/a&gt;  describes the algorithm in detail.&lt;/p&gt;
&lt;p&gt;
The size of the bubbles in the diagram below communicates the consumed volume of water. Due to the orders of magnitude difference in town size, the area is transformed with the square root. The colour of the bubble communicates a random level of performance.&lt;/p&gt;
&lt;p&gt;
In most traffic-light reporting systems, the colour for negative performance is red and excellent performance is green. This combination is, however, not useful for the 8% of men who struggle to see the difference between red and green. The &lt;a href=&#34;https://cran.r-project.org/web/packages/RColorBrewer/index.html&#34;&gt;RColorBrewer&lt;/a&gt; package provides several diverging colourblind-safe colour palates.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/hydroinformatics/bubble-chart-performance.png&#34; alt=&#34;Visualising a single qualitative value with a bubble chart&#34; title=&#34;Visualising a single qualitative value with a bubble chart&#34;/&gt;
&lt;figcaption&gt;
Visualising a single qualitative value with a bubble chart.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
The first part of the code creates mock performance data. The &lt;a href=&#34;https://lucidmanager.org/data-science/geocoding-with-ggmap/&#34;&gt;geocode function of the ggmap package&lt;/a&gt; provides the latitude and longitude for each location. The consumption data is taken from the 2019 Coliban Water annual report. The data has a row for each location, the total consumption, four performance variables and the average over these four.&lt;/p&gt;
&lt;p&gt;
The background map comes from &lt;a href=&#34;https://stamen.com/&#34;&gt;Stamen Design&lt;/a&gt;, under &lt;a href=&#34;https://creativecommons.org/licenses/by/3.0/&#34;&gt;CC BY 3.0&lt;/a&gt;. Data by &lt;a href=&#34;https://www.openstreetmap.org/&#34;&gt;OpenStreetMap&lt;/a&gt;, under &lt;a href=&#34;https://www.openstreetmap.org/copyright&#34;&gt;ODbL&lt;/a&gt;. The second part of the code projects the point geometry on the map and sets the scales and labels. The toner version of the Stamen maps is ideal for this visualisation because of its sparse background.&lt;/p&gt;
&lt;p&gt;
The next section shows how to split these bubbles into quadrants to separately visualise each variable.&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://github.com/pprevos/r4h2o/&#34; target=&#34;_blank&#34;
   title=&#34;Download r4h2o from GitHub&#34;
   alt=&#34;Download r4h2o from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;r4h2o&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Cheesecake diagram&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(tidyverse)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(ggmap)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(RColorBrewer)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Register Google Maps API&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  api &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;readLines&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;case-studies/google-maps.api&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;register_google&lt;/span&gt;(key &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; api)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Create mock performance data&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Towns with water treatment plants&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  towns &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Bendigo&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Boort&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Bridgewater&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Castlemaine&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Cohuna&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Echuca&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Elmore&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Goornong&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Gunower&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Heathcote&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Korong Vale&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Kyneton&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Laanecoorie&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Leitchville&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Lockington&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Pyramid Hill&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Rochester&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Serpentine&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Trentham&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  t &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;length&lt;/span&gt;(towns)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Volume produced&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## https://www.coliban.com.au/files/2019-10/FINAL_CW_AnnualReport2019_200919pm.pdf p. 24&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  consumption &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;11682&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;138&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;141&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2064&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;610&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;3017&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;106&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;44&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;48&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;243&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;117&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;862&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;106&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;161&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;55&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;84&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;857&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;17&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;94&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;set.seed&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1969&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  performance &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;tibble&lt;/span&gt;(Town &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; towns) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;bind_cols&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;geocode&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;paste&lt;/span&gt;(towns, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Victoria, Australia&amp;#34;&lt;/span&gt;))) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;mutate&lt;/span&gt;(Consumption &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; consumption,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;             Treatment &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sample&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;100&lt;/span&gt;, t),
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;             Network &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sample&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;100&lt;/span&gt;, t),
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;             Regulation &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sample&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;100&lt;/span&gt;, t),
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;             Perception &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sample&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;100&lt;/span&gt;, t),
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;             Performance &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;round&lt;/span&gt;((Treatment &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; Network &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; Regulation &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; Perception) &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;4&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Get map&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  bbox &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;make_bbox&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;range&lt;/span&gt;(performance&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;lon), &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;range&lt;/span&gt;(performance&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;lat), f &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;.1&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  map &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;get_stamenmap&lt;/span&gt;(bbox, maptype &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;toner-hybrid&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Single variable&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ggmap&lt;/span&gt;(map, extent &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;device&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;geom_point&lt;/span&gt;(data &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; performance,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                 &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;aes&lt;/span&gt;(lon, lat, size &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sqrt&lt;/span&gt;(Consumption), col &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; Performance),
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                 alpha &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0.9&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;scale_size_area&lt;/span&gt;(max_size &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;24&lt;/span&gt;, guide &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;none&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;scale_color_gradientn&lt;/span&gt;(colors &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;brewer.pal&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;7&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;RdYlBu&amp;#34;&lt;/span&gt;)) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;labs&lt;/span&gt;(title &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;System Performance&amp;#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;           subtitle &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Simulated data&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;theme_void&lt;/span&gt;(base_size &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;8&lt;/span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-2&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-2&#34;&gt;
Introducing the Cheesecake Diagram 
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-2&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;If we want to report more than one variable per location, the circle needs to he divided into two or more sectors. While this might sound like a pie chart, it is not. A pie chart visualises information through the size of the sectors in the diagram. The colours of the sectors communicate the data categories. Visualisation experts generally discourage using pie charts because they are not easy to interpret, but cheesecake diagrams are different.&lt;/p&gt;
&lt;p&gt;
A cheesecake diagram visualises information through the size of the circle and the colour of the sectors. The size of the sector is the same for each. A cheesecake diagram is a type of pie chart, but with a different flavour. The cheesecake diagram should not be used for more than four slices to ensure it remains readable.&lt;/p&gt;
&lt;p&gt;
The &lt;a href=&#34;https://cran.r-project.org/web//packages/scatterpie/index.html&#34;&gt;Scatterpie&lt;/a&gt; package provides functionality to plot pie charts on a map, but it does not allow you to link the colours to the aesthetics. The code below uses the powerful &lt;a href=&#34;https://ggforce.data-imaginist.com/&#34;&gt;ggforce package&lt;/a&gt; to construct the cheesecakes from four circle sectors. To create the sectors, the &lt;code class=&#34;verbatim&#34;&gt;performance&lt;/code&gt; data frame is pivoted, and each sector is defined by its starting and ending angle, being 90 degrees ($\pi/2$ radians). Because the circle now contains information, it is not sized to the level of consumption to keep the sectors clearly visible.&lt;/p&gt;
&lt;p&gt;
Adding the legend is a bit finicky. The &lt;code class=&#34;verbatim&#34;&gt;annotation_custom()&lt;/code&gt; function allows you to plot two ggplot graphs on top of each other. In this case we need to transform the coordinate systems to match as the map is in latitude /longitude and the legend .&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/hydroinformatics/cheesecake-performance.png&#34; alt=&#34;Cheesecake diagram with randomised variables&#34; title=&#34;Cheesecake diagram with randomised variables&#34;/&gt;
&lt;figcaption&gt;
Cheesecake diagram with randomised variables.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Cheesecake diagram&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(ggforce)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(gridExtra)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Convert data&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  cheesecake &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;pivot_longer&lt;/span&gt;(performance, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;-1&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;-4&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                            names_to &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Aspect&amp;#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                            values_to &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Performance&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;filter&lt;/span&gt;(Aspect &lt;span style=&#34;color:#333&#34;&gt;!=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Performance&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;mutate&lt;/span&gt;(start &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;rep&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;seq&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;pi&lt;/span&gt;, length.out &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;5&lt;/span&gt;)[&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;-5&lt;/span&gt;], t),
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;           end &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;rep&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;seq&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;pi&lt;/span&gt;, length.out &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;5&lt;/span&gt;)[&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;-1&lt;/span&gt;], t))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Legend&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  cheesecake_legend &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;tibble&lt;/span&gt;(x0 &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;.1&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;.1&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;-.1&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;-.1&lt;/span&gt;),
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                              y0 &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;.1&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;-.1&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;-.1&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;.1&lt;/span&gt;),
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                              start &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;seq&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;pi&lt;/span&gt;, length.out &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;5&lt;/span&gt;)[&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;-5&lt;/span&gt;],
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                              end &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;seq&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;pi&lt;/span&gt;, length.out &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;5&lt;/span&gt;)[&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;-1&lt;/span&gt;],
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                              dimension &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;unique&lt;/span&gt;(cheesecake&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;Aspect))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  l &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ggplot&lt;/span&gt;(cheesecake_legend) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;aes&lt;/span&gt;(x0 &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; x0, y0 &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; y0, r0 &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;, r &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;, start &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; start, end &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; end) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;geom_arc_bar&lt;/span&gt;(col &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;NA&lt;/span&gt;, fill &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;lightgrey&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;geom_text&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;aes&lt;/span&gt;(x0 &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;6&lt;/span&gt;, y0 &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;6&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                  label &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;unique&lt;/span&gt;(cheesecake&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;Aspect)), size &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;theme_void&lt;/span&gt;() &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;coord_equal&lt;/span&gt;()
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Visualise&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  m &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ggmap&lt;/span&gt;(map, extent &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;device&amp;#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;             base_layer &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ggplot&lt;/span&gt;(data &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; cheesecake,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                                 &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;aes&lt;/span&gt;(x0 &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; lon,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                                     y0 &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; lat,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                                     r0 &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                                     r &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;.05&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                                     start &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; start,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                                     end &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; end,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                                     fill &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; Performance))) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;geom_arc_bar&lt;/span&gt;(col &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;darkgrey&amp;#34;&lt;/span&gt;, size &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;.1&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;scale_size_area&lt;/span&gt;(max_size &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;12&lt;/span&gt;, guide &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;FALSE&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;scale_fill_gradientn&lt;/span&gt;(colors &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;brewer.pal&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;7&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;RdYlBu&amp;#34;&lt;/span&gt;)) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;labs&lt;/span&gt;(title &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;System Performance&amp;#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;         subtitle &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Simulated data&amp;#34;&lt;/span&gt;)  &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;theme_void&lt;/span&gt;(base_size &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;4&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  m &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;coord_cartesian&lt;/span&gt;() &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;coord_equal&lt;/span&gt;() &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;annotation_custom&lt;/span&gt;(grob &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ggplotGrob&lt;/span&gt;(l),
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                      xmin &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; bbox[3] &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;.3&lt;/span&gt;, xmax &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; bbox[3],
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                      ymin &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; bbox[4] &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;.3&lt;/span&gt;, ymax &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; bbox[4])&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;

  &lt;div class=&#34;box&#34;&gt;
    &lt;div class=&#34;media&#34;&gt;
      &lt;figure class=&#34;media-left&#34;&gt;
        &lt;p class=&#34;image is-128x128&#34;&gt;
          &lt;img src=&#34;https://lucidmanager.org/images/books/2023_ds4wu.jpg&#34; alt = &#34;Data Science for Water Utilities&#34; title = &#34;Data Science for Water Utilities&#34;&gt;
        &lt;/p&gt;
      &lt;/figure&gt;
      &lt;div class=&#34;media-content&#34;&gt;
        &lt;div class=&#34;content&#34;&gt;
          &lt;p class=&#34;is-size-5 has-text-weight-bold&#34;&gt;Data Science for Water Utilities&lt;/p&gt;
          &lt;p class=&#34;mb-2&#34;&gt;Data Science for Water Utilities published by CRC Press is an applied, practical guide that shows water professionals how to use data science to solve urban water management problems using the R language for statistical computing.&lt;/p&gt;
          &lt;div class=&#34;buttons&#34;&gt;
            &lt;a class=&#34;button is-info&#34; href=&#34;https://routledge.com/9781032354545&#34; target=&#34;_blank&#34;&gt;
              &lt;span class=&#34;icon&#34;&gt;
                &lt;i class=&#34;fas fa-shopping-cart&#34;&gt;&lt;/i&gt;
              &lt;/span&gt;
              &lt;span&gt;Routledge&lt;/span&gt;
            &lt;/a&gt;
            &lt;a class=&#34;button is-info&#34; href=&#34;https://www.amazon.com/Data-Science-Water-Utilities-Chapman/dp/1032354550&#34; target=&#34;_blank&#34;&gt;
              &lt;span class=&#34;icon&#34;&gt;
                &lt;i class=&#34;fas fa-shopping-cart&#34;&gt;&lt;/i&gt;
              &lt;/span&gt;
              &lt;span&gt;Amazon&lt;/span&gt;
            &lt;/a&gt;
          &lt;/div&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://www.r-bloggers.com/&#34; target=&#34;_blank&#34; title=&#34;Proudly associated with R-Bloggers&#34;&gt;
  &lt;button class=&#34;button is-link is-medium&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-r-project&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span&gt;As seen on R Bloggers&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;

      </description>
    </item>
    
    
    
    <item>
      <title>Export WordPress to Hugo Markdown or Org Mode with R</title>
      <link>https://lucidmanager.org/data-science/export-wordpress-to-hugo/</link>
      <pubDate>Sat, 18 Jul 2020 00:00:00 +0000</pubDate>
      <author>Peter Prevos</author>
      <guid>https://lucidmanager.org/data-science/export-wordpress-to-hugo/</guid>
      <description>
	
&lt;p&gt;
I started my first website in 1996 with hand-written HTML. That became a bit of a chore, so for about fifteen years, WordPress became my friend. WordPress has been great to me, but it is slowly becoming a pain to keep updating plugins, security issues, slow performance and the annoying block editor. I am also always looking for additional activities I can do with Emacs. Hugo takes a lot of the pain of managing site away as you can focus on the content and &lt;a href=&#34;https://lucidmanager.org/productivity/create-websites-with-org-mode-and-hugo/&#34;&gt;Emacs provides me with powerful editing functionality&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;
I recently returned to a static website using &lt;a href=&#34;https://gohugo.io/&#34;&gt;Hugo&lt;/a&gt;. This article explains how to export a WordPress blog to Hugo and customise it with R code. The only reason I used R is because it is the only programming language I know well enough.&lt;/p&gt;
&lt;p&gt;
You will also need to install the mighty &lt;a href=&#34;https://pandoc.org/&#34;&gt;Pandoc software&lt;/a&gt; to convert the content to Org mode and the &lt;a href=&#34;https://wordpress.org/plugins/wp-all-export/&#34;&gt;WP All Export&lt;/a&gt; WordPress plugin to export your website to a CSV file.&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-1&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-1&#34;&gt;
Convert the content to Markdown or Org Mode
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-1&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The first step is to export the WordPress posts database to a CSV file. Several plugins are available that help you with this task. I have used the &lt;a href=&#34;https://wordpress.org/plugins/wp-all-export/&#34;&gt;WP All Export plugin&lt;/a&gt; to export the data. You need to download the ZIP file and install this plugin manually in your WordPress setup. Follow the steps in the All Export plugin and create a CSV file from your posts with at least these fields:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Title&lt;/li&gt;
&lt;li&gt;Slug&lt;/li&gt;
&lt;li&gt;Date&lt;/li&gt;
&lt;li&gt;Content&lt;/li&gt;
&lt;li&gt;Categories&lt;/li&gt;
&lt;li&gt;Tags&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Alternatively, you can &lt;a href=&#34;https://lucidmanager.org/data-science/analyse-site-structure/&#34;&gt;link directly to the WordPress database&lt;/a&gt; and extract the data with the RMySQL package. &lt;/p&gt;
&lt;p&gt;
The content files for Hugo are either Markdown or Org Mode. I prefer to use Org Mode as it provides me with access to the extensive functionality that Emacs has to offer, including &lt;a href=&#34;https://opensource.com/article/20/5/r-emacs-data-science&#34;&gt;writing and evaluating R code&lt;/a&gt;. &lt;/p&gt;
&lt;p&gt;
The &lt;code class=&#34;verbatim&#34;&gt;Content&lt;/code&gt; field in the exported CSV file contains HTML code of the article. The code below reads the CSV file and saves each content field as an HTML file, using the post&amp;#39;s slug as the filename. The mighty Pandoc software converts this file to Org mode. Any draft posts or pages in the export file will have &lt;code class=&#34;verbatim&#34;&gt;NA&lt;/code&gt; as the file name and are as such skipped.&lt;/p&gt;
&lt;p&gt;
Now that we have some content, we need to add the Org mode front matter so that Hugo can build a site. The last part of the code generates the front matter for each post, prepends it to the exported Org mode file and cleans some entries.&lt;/p&gt;
&lt;p&gt;
Copy the code below and save it as &lt;code class=&#34;verbatim&#34;&gt;wp2org.R&lt;/code&gt;. You need to change the filename in the line that starts with &lt;code class=&#34;verbatim&#34;&gt;file&lt;/code&gt; to the name of your export file. The script also creates two subdirectories to store the HTML and Org files.&lt;/p&gt;
&lt;p&gt;
You run this code with &lt;code class=&#34;verbatim&#34;&gt;Rscript wp2org.R&lt;/code&gt; from the same directory where the CSV file is stored. The result will be a collection of Org mode files.&lt;/p&gt;
&lt;p&gt;
This new site will not be perfect just yet. To show the images, you need to download your &lt;code class=&#34;verbatim&#34;&gt;wp-content&lt;/code&gt; folder and move it to the &lt;code class=&#34;verbatim&#34;&gt;static/images&lt;/code&gt; folder in Hugo.&lt;/p&gt;
&lt;p&gt;
The internal links in your blogs will be hard-coded, which means that you need to configure Hugo to ensure your slugs stay the same.&lt;/p&gt;
&lt;p&gt;
There will be other bits and pieces that might not have adequately converted, so do check your pages. &lt;/p&gt;
&lt;p&gt;
All you have to do now is to add a theme to your website, and your blog is fully converted. The Hugo website has a great &lt;a href=&#34;https://gohugo.io/getting-started/quick-start/&#34;&gt;Quick Start&lt;/a&gt; page that will get you going.&lt;/p&gt;
&lt;p&gt;
You can create new posts and edit your content with your favourite text editor. I use &lt;a href=&#34;https://lucidmanager.org/productivity/create-websites-with-org-mode-and-hugo/&#34;&gt;Org mode in Emacs to develop this website&lt;/a&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-2&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-2&#34;&gt;
Summary
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-2&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;In summary, you need to take the following steps:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Install pandoc software and WP All Export WordPress plugin.&lt;/li&gt;
&lt;li&gt;Download your website as a &lt;code class=&#34;verbatim&#34;&gt;CSV&lt;/code&gt; file with the WordPress plugin.&lt;/li&gt;
&lt;li&gt;Copy the R script in a file called &lt;code class=&#34;verbatim&#34;&gt;wp2org.R&lt;/code&gt; and save it in the same location as the &lt;code class=&#34;verbatim&#34;&gt;CSV&lt;/code&gt; file.&lt;/li&gt;
&lt;li&gt;Open your console and move to  the folder with the script and &lt;code class=&#34;verbatim&#34;&gt;CSV&lt;/code&gt; file&lt;/li&gt;
&lt;li&gt;Run &lt;code class=&#34;verbatim&#34;&gt;Rscript wp2org.R&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Review the Org mode files and clean-up any issues&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-3&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-3&#34;&gt;
Script
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-3&#34; class=&#34;outline-text-2&#34;&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Export WP to Hugo&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Read exported WP content&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(tibble)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(readr)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(dplyr)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(stringr)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Replace the filename with the exported file&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  posts &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;read.csv&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;filename&amp;#34;&lt;/span&gt;, skipNul &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;TRUE&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Create subdirectories&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (&lt;span style=&#34;color:#333&#34;&gt;!&lt;/span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;dir.exists&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;tmp&amp;#34;&lt;/span&gt;)) &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;dir.create&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;tmp&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (&lt;span style=&#34;color:#333&#34;&gt;!&lt;/span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;dir.exists&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;org&amp;#34;&lt;/span&gt;)) &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;dir.create&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;org&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Read posts&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;for&lt;/span&gt; (i &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;in&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;nrow&lt;/span&gt;(posts)) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#888&#34;&gt;## Save content as temporary html file&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      filename &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;paste0&lt;/span&gt;(posts&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;Slug[i], &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;.html&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;writeLines&lt;/span&gt;(posts&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;Content[i], &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;paste0&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;tmp/&amp;#34;&lt;/span&gt;, filename))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#888&#34;&gt;## Convert to Org mode with Pandoc&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      pandoc &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;paste0&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;pandoc -o &amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;paste0&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;org/&amp;#34;&lt;/span&gt;, posts&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;Slug[i],
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                                            &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;.org &amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;paste0&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;tmp/&amp;#34;&lt;/span&gt;, filename)))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;system&lt;/span&gt;(pandoc)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Create front matter for all posts&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  fm &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;tibble&lt;/span&gt;(title &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;paste&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;#+title:&amp;#34;&lt;/span&gt;, posts&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;Title),
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;               date &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;paste0&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;#+date: [&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;as.POSIXct&lt;/span&gt;(posts&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;Date, origin &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;1970-01-01&amp;#34;&lt;/span&gt;), &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;]&amp;#34;&lt;/span&gt;),
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;               lastmod &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;paste0&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;#+lastmod: [&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;Sys.Date&lt;/span&gt;(), &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;]&amp;#34;&lt;/span&gt;),
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;               categories &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;paste&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;#+categories[]:&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;str_replace_all&lt;/span&gt;(posts&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;Categories, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34; &amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;-&amp;#34;&lt;/span&gt;)),
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;               tags &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;paste&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;#+tags[]:&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;str_replace_all&lt;/span&gt;(posts&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;Tags, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34; &amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;-&amp;#34;&lt;/span&gt;)),
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;               draft &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;#+draft: true&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;mutate&lt;/span&gt;(categories &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;str_replace_all&lt;/span&gt;(categories, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;\\|&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34; &amp;#34;&lt;/span&gt;),
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;             tags &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;str_replace_all&lt;/span&gt;(tags, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;\\|&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34; &amp;#34;&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Load Hugo files an prepend front matter&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;for&lt;/span&gt; (f &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;in&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;nrow&lt;/span&gt;(posts)) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      filename &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;paste0&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;org/&amp;#34;&lt;/span&gt;, posts&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;Slug[f], &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;.org&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      post &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;paste&lt;/span&gt;(fm[f, ]), &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;readLines&lt;/span&gt;(filename))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#888&#34;&gt;## Repoint images&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      post &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;str_replace_all&lt;/span&gt;(post, &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;paste0&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;http.*wp-content&amp;#34;&lt;/span&gt;), &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;/images&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#888&#34;&gt;## Cleanup LaTeX&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      post &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;str_replace_all&lt;/span&gt;(post, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;\\$latex &amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;$&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#888&#34;&gt;## Remove remaining Wordpress artefacts&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      post &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;str_remove_all&lt;/span&gt;(post, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#39;:::|\\{.wp.*|.*\\&amp;#34;\\}&amp;#39;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#888&#34;&gt;## Write to disk&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;writeLines&lt;/span&gt;(post, filename)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
&lt;a href = &#34;https://www.r-bloggers.com/&#34; target=&#34;_blank&#34; title=&#34;Proudly associated with R-Bloggers&#34;&gt;
  &lt;button class=&#34;button is-link is-medium&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-r-project&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span&gt;As seen on R Bloggers&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;

      </description>
    </item>
    
    
    
    <item>
      <title>GEDCOM Reader for the R Language: Analysing Family History</title>
      <link>https://lucidmanager.org/data-science/gedcom-reader/</link>
      <pubDate>Sat, 20 Jul 2019 21:15:37 +0000</pubDate>
      <author>Peter Prevos</author>
      <guid>https://lucidmanager.org/data-science/gedcom-reader/</guid>
      <description>
	
&lt;p&gt;
Understanding who you are is strongly related to understanding your family history. Discovering ancestors is now a popular hobby, as many archives are available on the internet. The GEDCOM format provides a standardised way to store information about ancestors. This article shows how to develop a GEDCOM reader using the R language.&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-1&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-1&#34;&gt;
The GEDCOM format
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-1&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The GEDCOM format is not an ideal way to store information, but it has become the de-facto standard for family history. This format includes metadata and two sets of data. The file contains a list of the individuals, and it lists the families to which they belong.&lt;/p&gt;
&lt;p&gt;
The basic principle is that each line has a level, indicated by the first digit. At level zero, we find metadata and the individuals and their family. At level one, we see the various types of data, such as births, deaths and marriages. The deeper levels provide the data for these events.&lt;/p&gt;
&lt;p&gt;
&lt;a href=&#34;http://heiner-eichmann.de/gedcom/gedcom.htm&#34;&gt;Heiner Eichmann&lt;/a&gt; maintains a website that explains the format and provides some examples of files to help you understand the syntax.&lt;/p&gt;
&lt;p&gt;
The GEDCOM format is not only old in the way it stores data, but it is also limited in the types of human relationships. These files only store genetic relationships between people and assume that these relationships are marriages between a wife and a husband. Human relationships are, however, a lot more complicated than the genetic relationships between children and their parents, grandparents and ancestors.&lt;/p&gt;
&lt;p&gt;
These issues aside, all genealogical software can export a file to GEDCOM. The next section shows how to create a basic GEDCOM reader using the &lt;a href=&#34;https://stringr.tidyverse.org/&#34;&gt;stringr&lt;/a&gt;, &lt;a href=&#34;https://tibble.tidyverse.org/&#34;&gt;tibble&lt;/a&gt; and &lt;a href=&#34;https://dplyr.tidyverse.org/&#34;&gt;dplyr&lt;/a&gt; packages from the &lt;a href=&#34;https://www.tidyverse.org/&#34;&gt;Tidyverse&lt;/a&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-2&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-2&#34;&gt;
GEDCOM Reader
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-2&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The &lt;code class=&#34;verbatim&#34;&gt;read.gedcom()&lt;/code&gt; function takes a GEDCOM file as input and delivers a data frame (tibble) with basic information:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;ID&lt;/li&gt;
&lt;li&gt;Full name&lt;/li&gt;
&lt;li&gt;Gender&lt;/li&gt;
&lt;li&gt;Birthdate and place&lt;/li&gt;
&lt;li&gt;Father&lt;/li&gt;
&lt;li&gt;Mother&lt;/li&gt;
&lt;li&gt;Death date and place&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This code only can be easily expanded to include further fields by adding lines in the while-loops and including the fields in the data frame.&lt;/p&gt;
&lt;p&gt;
The first lines read the file and setup the data frame. The &lt;code class=&#34;verbatim&#34;&gt;extract()&lt;/code&gt; function extracts an individual&amp;#39;s ID from a line in the file. The for loop runs through each line of the GEDCOM file. When the start of a new individual is detected, the GEDCOM reader collects the relevant information.&lt;/p&gt;
&lt;p&gt;
Births and christenings are considered equal to simplify the data. In older data, we often only know one or the other. The function looks for the start of a family. It extracts the husband and wife and assigns these as parents to each of the children. The last section cleans the data and returns the result.&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://github.com/pprevos/digital-humanities/&#34; target=&#34;_blank&#34;
   title=&#34;Download digital-humanities from GitHub&#34;
   alt=&#34;Download digital-humanities from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;digital-humanities&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Read GEDCOM file for individuals&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(stringr)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(lubridate)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(dplyr)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  read_gedcom_ind &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;function&lt;/span&gt;(gedcom_loc) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    gedcom &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;str_squish&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;readLines&lt;/span&gt;(gedcom_loc))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    idv &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sum&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;grepl&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;^0.*INDI$&amp;#34;&lt;/span&gt;, gedcom))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    fam &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sum&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;grepl&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;^0.*FAM$&amp;#34;&lt;/span&gt;, gedcom))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;cat&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;paste&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Individuals: &amp;#34;&lt;/span&gt;, idv, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;\n&amp;#34;&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;cat&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;paste&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Families: &amp;#34;&lt;/span&gt;, fam, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;\n&amp;#34;&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    family &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;data.frame&lt;/span&gt;(id &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;NA&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                         Full_Name &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;NA&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                         Gender &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;NA&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                         Birth_Date &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;NA&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                         Birth_Place &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;NA&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                         Father_id &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;NA&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                         Mother_id &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;NA&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                         Death_Date &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;NA&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                         Death_Place &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;NA&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#888&#34;&gt;## Extract data&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    extract &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;function&lt;/span&gt;(line, type) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;str_trim&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;str_sub&lt;/span&gt;(line, &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;str_locate&lt;/span&gt;(line, type)[2] &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    id &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;for&lt;/span&gt; (l &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;in&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;length&lt;/span&gt;(gedcom)) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;str_detect&lt;/span&gt;(gedcom[l], &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;^0&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;&amp;amp;&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;str_detect&lt;/span&gt;(gedcom[l], &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;INDI$&amp;#34;&lt;/span&gt;)) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        id &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; id &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        family[id, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;id&amp;#34;&lt;/span&gt;] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;unlist&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;str_split&lt;/span&gt;(gedcom[l], &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;@&amp;#34;&lt;/span&gt;))[2]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        l &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; l &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;while&lt;/span&gt;(&lt;span style=&#34;color:#333&#34;&gt;!&lt;/span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;str_detect&lt;/span&gt;(gedcom[l], &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;^0&amp;#34;&lt;/span&gt;)) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;grepl&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;NAME&amp;#34;&lt;/span&gt;, gedcom[l]))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            family[id, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Full_Name&amp;#34;&lt;/span&gt;] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;extract&lt;/span&gt;(gedcom[l], &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;NAME&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;grepl&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;SEX&amp;#34;&lt;/span&gt;, gedcom[l]))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            family[id, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Gender&amp;#34;&lt;/span&gt;] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;extract&lt;/span&gt;(gedcom[l], &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;SEX&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          l &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; l &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;grepl&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;BIRT|CHR&amp;#34;&lt;/span&gt;, gedcom[l])) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            l &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; l &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;while&lt;/span&gt; (&lt;span style=&#34;color:#333&#34;&gt;!&lt;/span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;str_detect&lt;/span&gt;(gedcom[l], &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;^1&amp;#34;&lt;/span&gt;)) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;              &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;grepl&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;DATE&amp;#34;&lt;/span&gt;, gedcom[l]))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                family[id, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Birth_Date&amp;#34;&lt;/span&gt;] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;extract&lt;/span&gt;(gedcom[l], &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;DATE&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;              &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;grepl&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;PLAC&amp;#34;&lt;/span&gt;, gedcom[l]))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                family[id, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Birth_Place&amp;#34;&lt;/span&gt;] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;extract&lt;/span&gt;(gedcom[l], &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;PLAC&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;              l &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; l &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;grepl&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;DEAT|BURI&amp;#34;&lt;/span&gt;, gedcom[l])) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            l &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; l &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;while&lt;/span&gt; (&lt;span style=&#34;color:#333&#34;&gt;!&lt;/span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;str_detect&lt;/span&gt;(gedcom[l], &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;^1&amp;#34;&lt;/span&gt;)) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;              &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;grepl&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;DATE&amp;#34;&lt;/span&gt;, gedcom[l]))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                family[id, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Death_Date&amp;#34;&lt;/span&gt;] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;extract&lt;/span&gt;(gedcom[l], &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;DATE&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;              &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;grepl&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;PLAC&amp;#34;&lt;/span&gt;, gedcom[l]))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                family[id, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Death_Place&amp;#34;&lt;/span&gt;] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;extract&lt;/span&gt;(gedcom[l], &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;PLAC&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;              l &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; l &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;str_detect&lt;/span&gt;(gedcom[l], &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;^0&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;&amp;amp;&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;str_detect&lt;/span&gt;(gedcom[l], &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;FAM&amp;#34;&lt;/span&gt;)) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        l &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; l &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;while&lt;/span&gt;(&lt;span style=&#34;color:#333&#34;&gt;!&lt;/span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;str_detect&lt;/span&gt;(gedcom[l], &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;^0&amp;#34;&lt;/span&gt;)) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;grepl&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;HUSB&amp;#34;&lt;/span&gt;, gedcom[l]))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            husband &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;unlist&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;str_split&lt;/span&gt;(gedcom[l], &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;@&amp;#34;&lt;/span&gt;))[2]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;grepl&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;WIFE&amp;#34;&lt;/span&gt;, gedcom[l]))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            wife &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;unlist&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;str_split&lt;/span&gt;(gedcom[l], &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;@&amp;#34;&lt;/span&gt;))[2]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;grepl&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;CHIL&amp;#34;&lt;/span&gt;, gedcom[l])) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            child &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;which&lt;/span&gt;(family&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;id &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;unlist&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;str_split&lt;/span&gt;(gedcom[l], &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;@&amp;#34;&lt;/span&gt;))[2])
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            family[child, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Father_id&amp;#34;&lt;/span&gt;] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; husband
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            family[child, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Mother_id&amp;#34;&lt;/span&gt;] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; wife
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          l &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; l &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    family &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;mutate&lt;/span&gt;(Full_Name &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;gsub&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;/&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;str_trim&lt;/span&gt;(Full_Name)),
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;             Birth_Date &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;as.Date&lt;/span&gt;(family&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;Birth_Date, format &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;%d %b %Y&amp;#34;&lt;/span&gt;),
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;             Death_Date &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;as.Date&lt;/span&gt;(family&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;Death_Date, format &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;%d %b %Y&amp;#34;&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-3&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-3&#34;&gt;
Analysing the data
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-3&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;There are many websites with GEDCOM files of family histories of famous and not so famous people. The
&lt;a href=&#34;https://webtreeprint.com/tp_famous_gedcoms.php&#34;&gt;Famous GEDCOMs&lt;/a&gt; website has a few useful examples to test the GEDCOM reader.&lt;/p&gt;
&lt;p&gt;
Once the data is in a data frame, you can analyse it any way you please. The code below downloads a file with the presidents of the US, with their ancestors and descendants. The &lt;code class=&#34;verbatim&#34;&gt;alive()&lt;/code&gt; function filters people who are alive at a certain date. For people without birth date, it sets a maximum age of 100 years.&lt;/p&gt;
&lt;p&gt;
The histogram shows the distribution of ages at time of death of all the people in the presidents file.&lt;/p&gt;
&lt;p&gt;
These are just some random examples of how to analyse family history data with this GEDCOM reader. The next article will explain how to plot a population pyramid using this data. A future article will discuss how to visualise the structure of family history.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/digital-humanities/us-presidents-birth-years.png&#34; alt=&#34;Birth years of people of U SPresidents&#34; title=&#34;Birth years of people of US Presidents&#34;/&gt;
&lt;figcaption&gt;
Birth years of people of in the Presidents file.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Basic family history statistics&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(ggplot2)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  presidents &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;read_gedcom_ind&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;https://webtreeprint.com/tp_downloader.php?path=famous_gedcoms/pres.ged&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;mutate&lt;/span&gt;(presidents, Year &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;year&lt;/span&gt;(Birth_Date)) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ggplot&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;aes&lt;/span&gt;(Year)) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;geom_histogram&lt;/span&gt;(binwidth &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt;, fill &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;#6A6A9D&amp;#34;&lt;/span&gt;, col &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;white&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;labs&lt;/span&gt;(title &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Birth years in the presidents file&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ggsave&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;us-presidents-birth-year.png&amp;#34;&lt;/span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;a href = &#34;https://www.r-bloggers.com/&#34; target=&#34;_blank&#34; title=&#34;Proudly associated with R-Bloggers&#34;&gt;
  &lt;button class=&#34;button is-link is-medium&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-r-project&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span&gt;As seen on R Bloggers&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/div&gt;
&lt;/div&gt;

      </description>
    </item>
    
    
    
    <item>
      <title>Project Euler 144: Laser Beams and Elliptical Billiards</title>
      <link>https://lucidmanager.org/data-science/project-euler-144/</link>
      <pubDate>Thu, 16 May 2019 00:00:00 +0000</pubDate>
      <author>Peter Prevos</author>
      <guid>https://lucidmanager.org/data-science/project-euler-144/</guid>
      <description>
	
&lt;p&gt;
Playing pool at a square table can be hard enough for most people. But what about testing your skills on an oval table? &lt;a href=&#34;https://projecteuler.net/problem=144&#34;&gt;Project Euler 144&lt;/a&gt; investigates multiple reflections of a laser beam. This problem is mathematically equivalent to bouncing billiard balls in an elliptical pool table, demonstrated by Ales Bellow in the video below. This article provides a solution to the Euler problem and simulates playing pool in an elliptical table. &lt;/p&gt;
&lt;figure&gt;
&lt;div style=&#34;position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden;&#34;&gt;
      &lt;iframe allow=&#34;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share; fullscreen&#34; loading=&#34;eager&#34; referrerpolicy=&#34;strict-origin-when-cross-origin&#34; src=&#34;https://www.youtube.com/embed/4KHCuXN2F3I?autoplay=0&amp;amp;controls=1&amp;amp;end=0&amp;amp;loop=0&amp;amp;mute=0&amp;amp;start=0&#34; style=&#34;position: absolute; top: 0; left: 0; width: 100%; height: 100%; border:0;&#34; title=&#34;YouTube video&#34;&gt;&lt;/iframe&gt;
    &lt;/div&gt;

&lt;figcaption&gt;
Alex Bellos, Elliptical Pool Table - Numberphile.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;div id=&#34;outline-container-headline-1&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-1&#34;&gt;
Project Euler 144 Definition
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-1&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;In laser physics, a “white cell” is a mirror system that acts as a delay line for the laser beam. The beam enters the cell, bounces around on the mirrors, and eventually works its way back out.&lt;/p&gt;
&lt;p&gt;
The specific white cell we will be considering is an ellipse with the equation $4x^2 + y^2= 100$. The section corresponding to $-0.01 \leq \times \leq +0.01$ at the top is missing, allowing the light to enter and exit through the hole.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/project-euler/problem-144a.png&#34; alt=&#34;Project Euler 144: Laser Beams and Elliptical Billiards&#34; title=&#34;Project Euler 144: Laser Beams and Elliptical Billiards&#34;/&gt;
&lt;figcaption&gt;
White cell.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
The light beam in this problem starts at the point (0.0, 10.1) just outside the white cell, and the beam first impacts the mirror at (1.4, -9.6). Each time the laser beam hits the surface of the ellipse, it follows the usual law of reflection “angle of incidence equals the angle of reflection.”&lt;/p&gt;
&lt;p&gt;
Both the incident and reflected beams make the same angle with the normal line at the point of incidence. In the figure on the left, the red line shows the first two points of contact between the laser beam and the wall of the white cell; the blue line indicates the line tangent to the ellipse at the point of incidence of the first bounce.&lt;/p&gt;
&lt;p&gt;
The slope $m$ of the tangent line at any point $(x,y)$ of the given ellipse is $m = -4x/y$. The normal line is perpendicular to this tangent line at the point of incidence.&lt;/p&gt;
&lt;p&gt;
How many times does the beam hit the internal surface of the white cell before exiting?&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-2&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-2&#34;&gt;
Proposed Solution
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-2&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The first step was to rewrite the equation to use functions to generalise the problem. The general Cartesian equation for an ellipse is:&lt;/p&gt;
&lt;p&gt;
$$\frac{x^2}{a^2} + \frac{y^2}{b^2} = 1, a &amp;lt; b$$&lt;/p&gt;
&lt;p&gt;
The length of the axes for this problem is $a =5$ and $b = 10$. While the Project Euler description gives the formula for the tangent slope to the ellipse, I have generalised the code to reuse it for the elliptical billiards table. The slope of the tangent to an ellipse at point $(x,y)$ is:&lt;/p&gt;
&lt;p&gt;
$$m=-\frac{b^2x}{a^2y}$$&lt;/p&gt;
&lt;p&gt;
This first code snippet defines functions to draw an ellipse and calculate the bouncing angle. The last part of the code bounces the laser inside the cell until it exits through the top.&lt;/p&gt;
&lt;p&gt;
The formula to find the intersection between a line and an ellipse has two possible solutions, one of which is the same as the original point.&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://github.com/pprevos/ProjectEuler/&#34; target=&#34;_blank&#34;
   title=&#34;Download ProjectEuler from GitHub&#34;
   alt=&#34;Download ProjectEuler from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;ProjectEuler&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  plot_ellipse &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;function&lt;/span&gt;(a, b, colour &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;NA&lt;/span&gt;, line &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;black&amp;#34;&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;plot.new&lt;/span&gt;()
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;plot.window&lt;/span&gt;(xlim &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt;a, a), ylim &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt;b, b), asp &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;par&lt;/span&gt;(mar &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;rep&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;,&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;4&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      x &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;seq&lt;/span&gt;(&lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt;a, a, length &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;200&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      y &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sqrt&lt;/span&gt;(b^2 &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; (b^2 &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt; a^2) &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; x^2)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;lines&lt;/span&gt;(x, y, col &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; line)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;lines&lt;/span&gt;(x, &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt;y, col &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; line)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;polygon&lt;/span&gt;(x, y, col &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; colour, border &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;NA&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;polygon&lt;/span&gt;(x, &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt;y, col &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; colour, border &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;NA&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  bounce &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;function&lt;/span&gt;(coords) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      x &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; coords&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;x
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      y &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; coords&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;y
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#888&#34;&gt;## Tangent to ellipse&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      t &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt;(b^2 &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt; a^2) &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; (x[2] &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt; y[2])
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#888&#34;&gt;## Deflection on sloping mirror y = mx + c&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      dydx &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;diff&lt;/span&gt;(y) &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;diff&lt;/span&gt;(x)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      m &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;tan&lt;/span&gt;(&lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;pi&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;atan&lt;/span&gt;(dydx) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;atan&lt;/span&gt;(t))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      c &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; y[2] &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; m &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; x[2]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#888&#34;&gt;## Determine intersection point&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#888&#34;&gt;## Source: http://www.ambrsoft.com/TrigoCalc/Circles2/Ellipse/EllipseLine.htm&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      x[1] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; x[2]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      y[1] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; y[2]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      x2 &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; (&lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt;a^2 &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; m &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; c &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;-1&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; (a &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; b &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sqrt&lt;/span&gt;(a^2 &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; m^2 &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; b^2 &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; c^2))) &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            (a^2 &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; m^2 &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; b^2)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      x[2] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ifelse&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;round&lt;/span&gt;(x[1] &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt; x2[1], &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;6&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;, x2[2], x2[1])
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      y[2] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; m &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; x[2] &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; c
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;return&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;data.frame&lt;/span&gt;(x, y))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;# Initial conditions&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  a &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;5&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  b &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  x1 &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  y1 &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10.1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  x2 &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1.4&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  y2 &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;-9.6&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  answer &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;plot_ellipse&lt;/span&gt;(a, b)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;points&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;,&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;), &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt;c, c), pch &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;19&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Bounce laser breams&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  laser &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;data.frame&lt;/span&gt;(x &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(x1, x2), y &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(y1, y2))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;while&lt;/span&gt;((laser&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;x[2] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;-0.01&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;|&lt;/span&gt; laser&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;x[2] &lt;span style=&#34;color:#333&#34;&gt;&amp;gt;&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0.01&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;|&lt;/span&gt; laser&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;y[2] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;) { &lt;span style=&#34;color:#888&#34;&gt;## Escape?&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;lines&lt;/span&gt;(laser&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;x, laser&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;y, col &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;red&amp;#34;&lt;/span&gt;, lwd &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;.5&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      laser &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;bounce&lt;/span&gt;(laser)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      answer &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; answer &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;print&lt;/span&gt;(answer)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/project-euler/problem-144-laser-beams.png&#34; alt=&#34;Multiple reflections of a laser beam.&#34; title=&#34;Multiple reflections of a laser beam.&#34;/&gt;
&lt;figcaption&gt;
Multiple reflections of a laser beam.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-3&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-3&#34;&gt;
Elliptical Pool Table
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-3&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;We can use the solution to Euler problem 144 to play billiards on an elliptical billiards table. To close the article, we return to the elliptical pool table demonstrated by Alex Bellos. This code draws the pool table to the dimensions mentioned in the video. We know that the table has an eccentricity of $e = 0.43$ and a long axis of $a = 130$ cm. The code defines the short axis ($b$) and the distance of the focal points from the centre.&lt;/p&gt;
&lt;p&gt;
The code selects a random starting point and angle of the shot. The code first determines whether the line passes through the pocket. If this is not the case, the algorithm then finds the place where the ball hits and keeps bouncing it until it falls into the pocket, or the ball bounces 100 times.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/project-euler/problem-144-pool-table.png&#34; alt=&#34;/images/project-euler/problem-144-pool-table.png&#34; title=&#34;/images/project-euler/problem-144-pool-table.png&#34; /&gt;&lt;figcaption&gt;
Elliptical pool table simulation.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
Elliptical billiard tables have four possible outcomes. Any ball the pass through a focal point will fall into the pocket, ending the simulation. Any ball that passes outside the focal points will bounce around, and the combined trajectories form an ellipse. When the ball moves between the foci, the result is a hyperbola. Lastly, some unique circumstances result in a regular polygon.&lt;/p&gt;
&lt;p&gt;
If simulations are not enough for you, then head over to the &lt;a href=&#34;http://www.instructables.com/id/Elliptical-Pool-Table/&#34;&gt;Instructables&lt;/a&gt; website to find out how you can construct an elliptical billiards table. There is even a patent for an &lt;a href=&#34;https://patents.google.com/patent/US5226644&#34;&gt;elliptical pocket billiard table&lt;/a&gt;, with the pockets at the edge.&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://github.com/pprevos/ProjectEuler/&#34; target=&#34;_blank&#34;
   title=&#34;Download ProjectEuler from GitHub&#34;
   alt=&#34;Download ProjectEuler from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;ProjectEuler&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  e &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0.43&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  a &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;130&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  b &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; a &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sqrt&lt;/span&gt;((&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; e) &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; (&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; e)) &lt;span style=&#34;color:#888&#34;&gt;# a &amp;gt; b&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  f &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sqrt&lt;/span&gt;(a^2 &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; b^2)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;plot_ellipse&lt;/span&gt;(a, b, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;darkgreen&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;NA&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;points&lt;/span&gt;(&lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt;f, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;, pch &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;19&lt;/span&gt;, cex &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;points&lt;/span&gt;(f, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;, pch &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;19&lt;/span&gt;, col &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;grey&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Simulate random shot&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  angle &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;runif&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;pi&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  x1 &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;runif&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;, &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt;a, a)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  ymax &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sqrt&lt;/span&gt;(b^2 &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; (b^2 &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt; a^2) &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; x1^2)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  y1 &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;runif&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;, &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt;ymax, ymax)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## First shot&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  m &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;tan&lt;/span&gt;(angle)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  c &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; y1 &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; m &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; x1
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  x2 &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; (&lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt;a^2 &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; m &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; c &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;-1&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; (a &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; b &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sqrt&lt;/span&gt;(a^2 &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; m^2 &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; b^2 &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; c^2))) &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt; (a^2 &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; m^2 &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; b^2)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  y2 &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; m &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; x2 &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; c
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  x2 &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; x2&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;[which&lt;/span&gt;(((x2 &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; x1) &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; (&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;cos&lt;/span&gt;(angle) &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;))]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  y2 &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; y2&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;[which&lt;/span&gt;(((y2 &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; y1) &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; (&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sin&lt;/span&gt;(angle) &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;))]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  shot &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; (&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;data.frame&lt;/span&gt;(x &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(x1, x2), y &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(y1, y2)))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Bounce ball&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;for&lt;/span&gt; (i &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;in&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;100&lt;/span&gt;){
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      dydx &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;diff&lt;/span&gt;(shot&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;y) &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;diff&lt;/span&gt;(shot&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;x)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;all.equal&lt;/span&gt;(dydx, (shot&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;y[1] &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt; (shot&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;x[1] &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt;f)) &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;TRUE&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          shot[2, ] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt;f, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;lines&lt;/span&gt;(shot&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;x, shot&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;y, col &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;yellow&amp;#34;&lt;/span&gt;, lwd &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (shot[2,&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;] &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;) &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;break&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      shot &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;bounce&lt;/span&gt;(shot)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;points&lt;/span&gt;(x1, y1, pch &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;19&lt;/span&gt;, col &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;blue&amp;#34;&lt;/span&gt;, cex &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1.8&lt;/span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
&lt;a href = &#34;https://www.r-bloggers.com/&#34; target=&#34;_blank&#34; title=&#34;Proudly associated with R-Bloggers&#34;&gt;
  &lt;button class=&#34;button is-link is-medium&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-r-project&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span&gt;As seen on R Bloggers&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;

      </description>
    </item>
    
    
    
    <item>
      <title>The Secret of Landusia: A Text Adventure in the R Language</title>
      <link>https://lucidmanager.org/data-science/text-adventure/</link>
      <pubDate>Sun, 12 May 2019 00:00:00 +0000</pubDate>
      <author>Peter Prevos</author>
      <guid>https://lucidmanager.org/data-science/text-adventure/</guid>
      <description>
	
&lt;p&gt;
In the 1980s and early 1990s, I spent a lot of time playing and writing text adventure games on my Atari 8-bit computer. Text adventures, or interactive fiction, are games where the player reads a story on the screen. The player can change the outcomes of the story by issuing text instructions to the computer. These lines are an extract from &lt;a href=&#34;http://rickadams.org/adventure/&#34;&gt;Colossal Cave&lt;/a&gt;, by Will Crowther and Don Woods, the very first text adventure written in the mid-1970s:&lt;/p&gt;
&lt;pre class=&#34;example&#34;&gt;
You are standing at the end of a road before a small brick building. Around you is a forest. A small stream flows out of the building and down a gully.
&amp;gt; go south
You are in a valley in the forest beside a stream tumbling along a rocky bed.
&amp;gt; enter stream
Your feet are now wet.
&lt;/pre&gt;
&lt;p&gt;
The basic premise of a text adventure is that the player finds herself in a simulated world. Unlike contemporary simulations, this virtual world is created only with text. This might seem a strange idea to modern games, but simulated worlds through text have existed for thousands of years. A text adventure enhances the traditional book by giving the player the freedom to act within the narrative. The objective of these games is to solve problems to reach a specified goal. &lt;/p&gt;
&lt;p&gt;
This article describes how to write a text adventure in the R language. This article replicates a game and the techniques described by Hal Renko and Sam Edwards in the Dutch book &lt;em&gt;Adventures!&lt;/em&gt; (1986).&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/rgames/renko-edwards.jpg&#34; alt=&#34;Hal Renko and Sam Edwarks. Adventures! (1986).&#34; title=&#34;Hal Renko and Sam Edwarks. Adventures! (1986).&#34;/&gt;
&lt;figcaption&gt;
Cover of the 1986 book &lt;em&gt;Adventures!&lt;/em&gt;
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;div id=&#34;outline-container-headline-1&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-1&#34;&gt;
Spoiler Alert!
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-1&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The sections below describe the detailed workings and the narrative of the game. If you don&amp;#39;t like spoilers, then perhaps you should download the code and play &lt;em&gt;The Secret of Landusia&lt;/em&gt; before reading further.&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://github.com/pprevos/RGames/&#34; target=&#34;_blank&#34;
   title=&#34;Download RGames from GitHub&#34;
   alt=&#34;Download RGames from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;RGames&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-2&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-2&#34;&gt;
The Secret of Landusia Text Adventure
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-2&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;This section explains the detailed workings of the Landusia game. The text adventure consists of four files:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code class=&#34;verbatim&#34;&gt;adventure.R&lt;/code&gt;: The code.&lt;/li&gt;
&lt;li&gt;&lt;code class=&#34;verbatim&#34;&gt;actions.md&lt;/code&gt;: Markdown file with descriptions of rooms and actions.&lt;/li&gt;
&lt;li&gt;&lt;code class=&#34;verbatim&#34;&gt;map.csv&lt;/code&gt;: Matrix of the relationship between rooms.&lt;/li&gt;
&lt;li&gt;&lt;code class=&#34;verbatim&#34;&gt;objects.csv&lt;/code&gt;: Matrix of the status of objects and actors.&lt;/li&gt;
&lt;/ul&gt;
&lt;div id=&#34;outline-container-headline-3&#34; class=&#34;outline-3&#34;&gt;
&lt;h3 id=&#34;headline-3&#34;&gt;
The Game Loop
&lt;/h3&gt;
&lt;div id=&#34;outline-text-headline-3&#34; class=&#34;outline-text-3&#34;&gt;
&lt;p&gt;The computer displays the current state of the game with a short piece of prose. The player then enters a command in natural language, which changes the state of the game. When the state of the player or the game reaches a certain point, the player has either won or lost the game. The game loop processes the commands of the player and determines the new state of the game. The state of the game consists of:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;em&gt;Player&lt;/em&gt;: The player can have many attributes, such as health, objects
they are carrying and their location.&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Map&lt;/em&gt;: Logic of the game localities and a description of each area.&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Objects&lt;/em&gt;: Are either placed in a room or are with the player.&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Actors&lt;/em&gt;: Can act independently.&lt;/li&gt;
&lt;/ul&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/rgames/landusia-flowchart.png&#34; alt=&#34;Text adventure flowchart&#34; title=&#34;Text adventure flowchart&#34;/&gt;
&lt;figcaption&gt;
Text adventure flowchart.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-4&#34; class=&#34;outline-3&#34;&gt;
&lt;h3 id=&#34;headline-4&#34;&gt;
Initialisation
&lt;/h3&gt;
&lt;div id=&#34;outline-text-headline-4&#34; class=&#34;outline-text-3&#34;&gt;
&lt;p&gt;The initialisation defines the vocabulary through a vector of verbs and a vector of directions. The code reads the map, objects and prose into memory and sets the starting variables for the player. The initialisation includes two auxiliary functions that the player cannot call directly.&lt;/p&gt;
&lt;p&gt;
The prose function takes a key phrase and finds the location of the heading for that key in the &lt;code class=&#34;verbatim&#34;&gt;actions&lt;/code&gt; variable. The following lines of code print the text up to the next heading indicator. The prose function describes rooms, actions, actors and so on. Using a markdown file simplifies writing the text so it is not hidden amongst code. The prose function reads keywords after double hashtags. Other headings are used for convenience.&lt;/p&gt;
&lt;p&gt;
The inventory function lists all objects that the player carries. This function is a separate verb in most adventure games, but in this game, it is integrated with other verbs.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-5&#34; class=&#34;outline-3&#34;&gt;
&lt;h3 id=&#34;headline-5&#34;&gt;
The Player
&lt;/h3&gt;
&lt;div id=&#34;outline-text-headline-5&#34; class=&#34;outline-text-3&#34;&gt;
&lt;p&gt;The program simulates the player with a set of parameters, such as their location, level of health and whatever other aspect is relevant. A player can, for example, only carry a certain number of objects, can only endure two blows by a sword, and so on.&lt;/p&gt;
&lt;p&gt;
In the Landusia game, the player has a level of &lt;code class=&#34;verbatim&#34;&gt;health&lt;/code&gt; and maximum carrying &lt;code class=&#34;verbatim&#34;&gt;capacity&lt;/code&gt;. The &lt;code class=&#34;verbatim&#34;&gt;room&lt;/code&gt; variable keeps track of where the player is located.&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-6&#34; class=&#34;outline-4&#34;&gt;
&lt;h4 id=&#34;headline-6&#34;&gt;
The Map
&lt;/h4&gt;
&lt;div id=&#34;outline-text-headline-6&#34; class=&#34;outline-text-4&#34;&gt;
&lt;p&gt;The game consists of 28 &amp;#39;rooms&amp;#39;. The map is stored in a matrix which indicates the relationships between the rooms. Each direction can have one or no exit. The map can also dynamically change during the game. Specific directions can, for example, only become available after the player opens a door. A rockfall could close an entrance, and so on. The player can move around the map by issuing directional commands. These commands are the four cardinal compass directions, and up and down.&lt;/p&gt;
&lt;p&gt;
In Landusia, the first nine rooms are a forest. The blacksmith lives south of the forest. Clovar&amp;#39;s castle lies to the south-east and the temple north-east of the forest. The connection between room 14 and 15 opens after the player uses the rope in room 14.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/rgames/landusia-map.png&#34; alt=&#34;Secret of Landusia map&#34; title=&#34;Secret of Landusia map&#34;/&gt;
&lt;figcaption&gt;
Secret of Landusia map.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-7&#34; class=&#34;outline-4&#34;&gt;
&lt;h4 id=&#34;headline-7&#34;&gt;
Objects
&lt;/h4&gt;
&lt;div id=&#34;outline-text-headline-7&#34; class=&#34;outline-text-4&#34;&gt;
&lt;p&gt;The descriptions of the different rooms of the game can contain objects. These objects are fixed in that their description is hard-coded into the game. A text adventure can also have movable objects. Each object can have its own properties, such as weight, and location. The location of the object can either be a room or the player (room zero). These properties are stored in another matrix.&lt;/p&gt;
&lt;p&gt;
The Landusia game has four objects: bandage, sword, flute and rope. These objects are scattered around the map and have a certain weight. The &lt;code class=&#34;verbatim&#34;&gt;capacity&lt;/code&gt; variable limits what the layer can carry simultaneously.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/rgames/landusia-battlements.jpg&#34; alt=&#34;Landusia castle battlements&#34; title=&#34;Landusia castle battlements&#34;/&gt;
&lt;figcaption&gt;
Landusia castle battlements.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-8&#34; class=&#34;outline-4&#34;&gt;
&lt;h4 id=&#34;headline-8&#34;&gt;
Actors
&lt;/h4&gt;
&lt;div id=&#34;outline-text-headline-8&#34; class=&#34;outline-text-4&#34;&gt;
&lt;p&gt;Actors are like objects, with the difference that they are active in the game. As the game progresses, actors take independent actions. Actors can move around the map and respond to the actions of the player. They can have properties, such as health and strength. Actors can be either friends or enemies that create a dynamic narrative.&lt;/p&gt;
&lt;p&gt;
Landusia has four actors which are stored in the same matrix a the objects. Each actor has a name, location, health and status. The actors function controls their actions.&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-9&#34; class=&#34;outline-5&#34;&gt;
&lt;h5 id=&#34;headline-9&#34;&gt;
Wizard
&lt;/h5&gt;
&lt;div id=&#34;outline-text-headline-9&#34; class=&#34;outline-text-5&#34;&gt;
&lt;p&gt;The wizard appears in the living quarters. His only task is to give the player the flute, once they manage to reach him. After this one action, the wizard vanishes by changing his location to room 100.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-10&#34; class=&#34;outline-5&#34;&gt;
&lt;h5 id=&#34;headline-10&#34;&gt;
Crow
&lt;/h5&gt;
&lt;div id=&#34;outline-text-headline-10&#34; class=&#34;outline-text-5&#34;&gt;
&lt;p&gt;When the player is in the forest, a random variable determine whether they are swooped by a crow. Another random variable determines whether the player was hurt. If the crow manages to hit the player, the code reduces the &lt;code class=&#34;verbatim&#34;&gt;health&lt;/code&gt; variable.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/rgames/landusia-crow.jpg&#34; alt=&#34;Crow attacking the player&#34; title=&#34;Crow attacking the player&#34;/&gt;
&lt;figcaption&gt;
Crow attacking the player.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-11&#34; class=&#34;outline-5&#34;&gt;
&lt;h5 id=&#34;headline-11&#34;&gt;
Dragon
&lt;/h5&gt;
&lt;div id=&#34;outline-text-headline-11&#34; class=&#34;outline-text-5&#34;&gt;
&lt;p&gt;The dragon sequence depends on the room the player is in. In room 17, the dragon has a 50% chance of hitting, while in room 18, the dragon is always sucessful. The player needs to kill the dragon with the sword (see below). The success of these fights depends on a random variable.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-12&#34; class=&#34;outline-5&#34;&gt;
&lt;h5 id=&#34;headline-12&#34;&gt;
Blacksmith
&lt;/h5&gt;
&lt;div id=&#34;outline-text-headline-12&#34; class=&#34;outline-text-5&#34;&gt;
&lt;p&gt;The blacksmith has the most complex handler of them all. He starts lying wounded on the floor. If the player does not help the blacksmith by using the bandage, he will die. If the player uses the bandage, the blacksmith is healed and move to the workshop to forge a sword for the player. The status variable controls the evolution of the blacksmith in the game.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-13&#34; class=&#34;outline-4&#34;&gt;
&lt;h4 id=&#34;headline-13&#34;&gt;
Command interpreter
&lt;/h4&gt;
&lt;div id=&#34;outline-text-headline-13&#34; class=&#34;outline-text-4&#34;&gt;
&lt;p&gt;The command interpreter asks the player for instructions, which the program then interprets. The state of the game (player, map, objects and actors) is changed, depending on the input. If the state of the game reaches a particular defined state, the game will end either successfully or miserably.&lt;/p&gt;
&lt;p&gt;
The basic principle of all command translators in interactive fiction is that they analyse the input using
&lt;a href=&#34;https://www.regular-expressions.info/rlanguage.html&#34;&gt;regex&lt;/a&gt;-type approaches. The interpreter compares the player&amp;#39;s instructions with a vocabulary and strips verbs and nouns.&lt;/p&gt;
&lt;p&gt;
For example, &amp;#34;take lamp&amp;#34; calls the take function and passes it the parameter &lt;em&gt;lamp&lt;/em&gt;. The object &lt;em&gt;lamp&lt;/em&gt; will then be moved from its current location to the player. The game could also check if the player still has enough carrying capacity and so on.&lt;/p&gt;
&lt;p&gt;
If the player enters a command that the interpreter cannot parse, then a negative response follows. The better the text adventure, the higher the likelihood that the game understands the command. Traditional text adventures use elaborate command interpreters to maximise the level of realism of the simulated world. This text adventure uses a minimised vocabulary and each of the verbs has its own function.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/rgames/landusia-dragon.jpg&#34; alt=&#34;The dragon of Landusia&#34; title=&#34;The dragon of Landusia&#34;/&gt;
&lt;figcaption&gt;
The dragon of Landusia.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;div id=&#34;outline-container-headline-14&#34; class=&#34;outline-5&#34;&gt;
&lt;h5 id=&#34;headline-14&#34;&gt;
Look
&lt;/h5&gt;
&lt;div id=&#34;outline-text-headline-14&#34; class=&#34;outline-text-5&#34;&gt;
&lt;p&gt;The look function takes a room number as input and displays the prose related to the room. The next section lists the available passages and objects in the room. The final part of the look function displays any objects that the player is carrying.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-15&#34; class=&#34;outline-5&#34;&gt;
&lt;h5 id=&#34;headline-15&#34;&gt;
Take and Put
&lt;/h5&gt;
&lt;div id=&#34;outline-text-headline-15&#34; class=&#34;outline-text-5&#34;&gt;
&lt;p&gt;These two functions allow a player to take an object or place it on the ground. The weight of the object is added or subtracted to the carrying capacity. When the object is not present in the room or not carried by the player, the code responds appropriately.&lt;/p&gt;
&lt;p&gt;
These functions cannot respond to objects mentioned in the room descriptions or actors, such as trees or the dragon. This can be easily remedied by creating two types of objects, moveable and immovable.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-16&#34; class=&#34;outline-5&#34;&gt;
&lt;h5 id=&#34;headline-16&#34;&gt;
Wait
&lt;/h5&gt;
&lt;div id=&#34;outline-text-headline-16&#34; class=&#34;outline-text-5&#34;&gt;
&lt;p&gt;The wait function does nothing. Waiting might be useful to let other actors do their thing. The wait function has a dummy parameter because there is none, but the main loop assigns one to each verb. This verb is needed to give the blacksmith enough time to forge the sword.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-17&#34; class=&#34;outline-5&#34;&gt;
&lt;h5 id=&#34;headline-17&#34;&gt;
Use
&lt;/h5&gt;
&lt;div id=&#34;outline-text-headline-17&#34; class=&#34;outline-text-5&#34;&gt;
&lt;p&gt;The use function starts by checking whether the player actually has the object in their possession. The remainder of the function contains instructions for each of the four objects. The bandage will either heal yourself or the blacksmith. The sword is not very useful in this context. The flute activates the magical teleportation by changing the &lt;code class=&#34;verbatim&#34;&gt;room&lt;/code&gt; variable. Finally, using the rope changes the map by opening the connection between rooms 14 and 15. This action also changes the prose so that the room descriptions are enhanced.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-18&#34; class=&#34;outline-5&#34;&gt;
&lt;h5 id=&#34;headline-18&#34;&gt;
Kill
&lt;/h5&gt;
&lt;div id=&#34;outline-text-headline-18&#34; class=&#34;outline-text-5&#34;&gt;
&lt;p&gt;This last function is conditional upon having the sword. If the player has no sword, then their fists are the weapon of choice. If an opponent is present in the same room as the player, they have a 60% chance of hitting the opponent. The sword has a higher strength than a fist fight. The actor function controls the return hits.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-19&#34; class=&#34;outline-3&#34;&gt;
&lt;h3 id=&#34;headline-19&#34;&gt;
Expanding the Game
&lt;/h3&gt;
&lt;div id=&#34;outline-text-headline-19&#34; class=&#34;outline-text-3&#34;&gt;
&lt;p&gt;This game is modular in structure so that it should be easy to enhance this game or to write a completely different game. They key to writing a good adventure is simplicity. It is easy to get carried away with writing complex command structure that the player has to guess are available.&lt;/p&gt;
&lt;p&gt;
Feel free to leave a message below if you find bugs or like to enhance the prose or code.&lt;/p&gt;
&lt;p&gt;
A lot has changed in computing since the heyday of text adventures. Although these games provided an illusion of freedom to act in a simulated world, the player is always limited by what the writer intended them to do.&lt;/p&gt;
&lt;p&gt;
Perhaps natural language processing, complex Markov chains and text-generating deep learning methods can generate the ultimate text adventure. Imagine a text adventure where the computer understands the basic structure but freely develops the prose and interactions.&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://www.r-bloggers.com/&#34; target=&#34;_blank&#34; title=&#34;Proudly associated with R-Bloggers&#34;&gt;
  &lt;button class=&#34;button is-link is-medium&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-r-project&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span&gt;As seen on R Bloggers&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

      </description>
    </item>
    
    
    
    <item>
      <title>Analyse Site Structure networks with R and igraph</title>
      <link>https://lucidmanager.org/data-science/analyse-site-structure/</link>
      <pubDate>Tue, 09 Apr 2019 00:00:00 +0000</pubDate>
      <author>Peter Prevos</author>
      <guid>https://lucidmanager.org/data-science/analyse-site-structure/</guid>
      <description>
	
&lt;p&gt;
One of the essential parts of Search Engine Optimisation (SEO) is a logical site structure. Site structure relates to how the pages of a website link to each other. A well-designed structure of internal links improves the user&amp;#39;s experience. It also helps web crawlers to find their way through the website. This article shows how to use the igraph software to analyse site structure.&lt;/p&gt;
&lt;p&gt;
The Lucid Manager website has been around for ten years now, and the content has grown organically over time, which is a reflection of my changing interests and evolving career. Although I use categories and tags to organise the articles, internal linking between posts is a bit haphazard. This post shows how I extracted the relevant data from the Wordpress database and visualised the internal links with the iGraph package. This analysis shows that my website needs a bit of rework to improve its jungle of internal links to provide you and search engines a better experience.&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-1&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-1&#34;&gt;
Site Structure
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-1&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;A website needs to have a &lt;a href=&#34;https://yoast.com/site-structure-the-ultimate-guide/&#34;&gt;logical structure&lt;/a&gt; to prevent it from being a loose collecting of articles. Internal links on a website organise the available information through taxonomy, and these links provide context to the text by referring to related pages. The taxonomy consists of menus, breadcrumbs, categories, tags and other structural elements. The body of the text of each article contains the contextual which provide additional or related information to the reader.&lt;/p&gt;
&lt;p&gt;
Wordpress automatically adds links to categories and tags and the creates an ordered structure. Body text links are created organically and can therefore quickly turn into a chaotic jungle of relationships. Wordpress has plugins that help with site structure, but not of these tools provide a complete overview of the structure in the internal links.&lt;/p&gt;
&lt;p&gt;
The taxonomy provides an automated structure of the website by categorising articles. The internal linking structure is organic and needs some further consideration. This analysis focuses on the organic links within the body of the text by visualising and analysing the internal linking structure.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-2&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-2&#34;&gt;
Extracting Wordpress Data
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-2&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The Lucid Manager used to be created with Wordpress. This system stores the body text of all articles in the &lt;code class=&#34;verbatim&#34;&gt;wp_posts&lt;/code&gt; table in the database. To analyse site structure, we need the following fields:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code class=&#34;verbatim&#34;&gt;post_name&lt;/code&gt; (slug)&lt;/li&gt;
&lt;li&gt;&lt;code class=&#34;verbatim&#34;&gt;post_content&lt;/code&gt; (full text)&lt;/li&gt;
&lt;li&gt;&lt;code class=&#34;verbatim&#34;&gt;post_type&lt;/code&gt; (page, post or other types)&lt;/li&gt;
&lt;li&gt;&lt;code class=&#34;verbatim&#34;&gt;post_status&lt;/code&gt; (use only published posts)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The post type is in most instances either a page or a post, but can also be attachments and so on. The post name (slug) links posts together, and the post content contains the full text of the article in HTML.&lt;/p&gt;
&lt;p&gt;
Several methods are available to extract the required data from the Wordpress database. The easiest way is to use a plugin such as &lt;a href=&#34;https://www.wpallimport.com/export/&#34;&gt;WP All Export&lt;/a&gt;. You can drag-and-drop the required fields or run a SQL query. The second method is to log in to the phpMyAdmin on your cPanel and run the relevant queries.&lt;/p&gt;
&lt;p&gt;
The third method involves using the &lt;a href=&#34;https://cran.r-project.org/web/packages/RMySQL/index.html&#34;&gt;RMySQL&lt;/a&gt; package in R to directly download data from the site. You will need to create a new user with limited access rights and open the database to your IP address.&lt;/p&gt;
&lt;p&gt;
The first step reads the data from the database and selects all published posts. The credentials are read from the &lt;code class=&#34;verbatim&#34;&gt;dbconnect.R&lt;/code&gt; file, so I don&amp;#39;t publish sensitive data on this website. The table names will be different if you use a Wordpress network. Thanks to &amp;#39;phaskat&amp;#39; on the &lt;a href=&#34;https://wordpress.stackexchange.com/questions/333163/i-would-like-some-help-wth-an-sql-query-to-link-posts-with-categories/333189#333189&#34;&gt;Wordpress StackExchange&lt;/a&gt; for helping with the queries. Please note that when using a Wordpress network, you will need to replace the wp prefix in the table name with the relevant string.&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://github.com/pprevos/digital-humanities/&#34; target=&#34;_blank&#34;
   title=&#34;Download digital-humanities from GitHub&#34;
   alt=&#34;Download digital-humanities from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;digital-humanities&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#888&#34;&gt;## Load data (CSV exported from wp_posts table in MySQL database)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(tidyverse)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(tidytext)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(igraph)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(RMySQL)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(RColorBrewer)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#888&#34;&gt;## Download data using RmySQL&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;source&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;dbconnect.R&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#888&#34;&gt;## lucidmanager &amp;lt;- dbConnect(MySQL(),&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#888&#34;&gt;##                          user = &amp;#34;username&amp;#34;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#888&#34;&gt;##                          password = &amp;#34;Password1&amp;#34;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#888&#34;&gt;##                          dbname = &amp;#34;databe name&amp;#34;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#888&#34;&gt;##                          host = &amp;#34;URL&amp;#34;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;posts.query &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;SELECT p.post_name, p.post_content, t.name
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;background-color:#fff0f0&#34;&gt;                FROM wp_posts p
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;background-color:#fff0f0&#34;&gt;                JOIN wp_term_relationships tr ON (tr.object_id = p.ID)
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;background-color:#fff0f0&#34;&gt;                JOIN wp_term_taxonomy tt ON (tt.term_taxonomy_id = tr.term_taxonomy_id )
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;background-color:#fff0f0&#34;&gt;                JOIN wp_terms t ON (t.term_id = tt.term_id)
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;background-color:#fff0f0&#34;&gt;                WHERE p.post_type=&amp;#39;post&amp;#39; AND p.post_status=&amp;#39;publish&amp;#39; 
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;background-color:#fff0f0&#34;&gt;                      AND tt.taxonomy = &amp;#39;category&amp;#39;&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;pages.query &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;SELECT post_name, post_content
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;background-color:#fff0f0&#34;&gt;                FROM wp_posts
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;background-color:#fff0f0&#34;&gt;                WHERE post_type=&amp;#39;page&amp;#39; AND post_status=&amp;#39;publish&amp;#39;&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;posts.data &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;dbSendQuery&lt;/span&gt;(lucidmanager, posts.query)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;posts &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;fetch&lt;/span&gt;(posts.data, n &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;-1&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;pages.data &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;dbSendQuery&lt;/span&gt;(lucidmanager, pages.query)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;pages &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;fetch&lt;/span&gt;(pages.data, n &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;-1&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;dbDisconnect&lt;/span&gt;(lucidmanager)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;pages&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;name &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Page&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;content &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;rbind&lt;/span&gt;(posts, pages)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;as_tibble&lt;/span&gt;(content)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
  The result of this code is a data frame with all posts and pages:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code class=&#34;verbatim&#34;&gt;post_name&lt;/code&gt;: The slug of the post or page.&lt;/li&gt;
&lt;li&gt;&lt;code class=&#34;verbatim&#34;&gt;post_content&lt;/code&gt;: Content of the page (HTML file without headers).&lt;/li&gt;
&lt;li&gt;&lt;code class=&#34;verbatim&#34;&gt;name&lt;/code&gt;: Post category.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I currently no longer use Wordpress but have moved everything to the &lt;a href=&#34;https://lucidmanager.org/data-science/export-wordpress-to-hugo/&#34;&gt;Hugo static website generator&lt;/a&gt;, using &lt;a href=&#34;https://lucidmanager.org/productivity/create-websites-with-org-mode-and-hugo/&#34;&gt;Emacs Org Mode to write content&lt;/a&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-3&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-3&#34;&gt;
Converting the data to a network
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-3&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The second step uses the &lt;a href=&#34;https://www.tidytextmining.com/&#34;&gt;tidytext package&lt;/a&gt; to convert the texts of the articles into tokens. In this case, a token is any set of characters between spaces. In the default setting, the tokenisation function also splits words at dashes, which is not helpful because we want to preserve the hyperlinks as one token.&lt;/p&gt;
&lt;p&gt;
The code uses regular expressions to filter all internal links (detect &lt;code class=&#34;verbatim&#34;&gt;lucidmanager.org&lt;/code&gt;) and retain the slug. The slug is the text after the website name. The &lt;code class=&#34;verbatim&#34;&gt;post_name&lt;/code&gt; field in the database contains the slug for the post itself. We thus identify a link from one internal page to another.&lt;/p&gt;
&lt;p&gt;
These steps result in a table that shows how all slugs link to each other. In network analysis, this is an &lt;a href=&#34;https://en.wikipedia.org/wiki/Adjacency_list&#34;&gt;adjacency list&lt;/a&gt; as each line represents a relationship in the network. The category variable identifies the post category, which is helpful to colour the resulting graph. Note that any post without outgoing links will not appear in this list.&lt;/p&gt;
&lt;p&gt;
This code also works with any data frame that contains the slug and content of the web pages as fields. The category variable is optional. Replace &lt;code class=&#34;verbatim&#34;&gt;lucidmanager.org&lt;/code&gt; with the URL of the website you are analysing.&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  links &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; content &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;unnest_tokens&lt;/span&gt;(word, post_content,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                  token &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; stringr&lt;span style=&#34;color:#333&#34;&gt;::&lt;/span&gt;str_split, pattern &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34; &amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;filter&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;grepl&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;.+lucidmanager.org/&amp;#34;&lt;/span&gt;, word)) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;mutate&lt;/span&gt;(link &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;gsub&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;.+lucidmanager.org/&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;&amp;#34;&lt;/span&gt;, word),
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;           link &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;gsub&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;/.+&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;&amp;#34;&lt;/span&gt;, link)) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;filter&lt;/span&gt;(link &lt;span style=&#34;color:#333&#34;&gt;!=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;tag&amp;#34;&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;&amp;amp;&lt;/span&gt; link &lt;span style=&#34;color:#333&#34;&gt;!=&lt;/span&gt;  &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;wp-content&amp;#34;&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;&amp;amp;&lt;/span&gt; link &lt;span style=&#34;color:#333&#34;&gt;!=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;author&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;select&lt;/span&gt;(category &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; name, post_name, link)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
The resulting adjacency list will look something like this:&lt;/p&gt;
&lt;figure&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;category&lt;/th&gt;
&lt;th&gt;post_name&lt;/th&gt;
&lt;th&gt;link&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;RStats&lt;/td&gt;
&lt;td&gt;analyse-site-structure&lt;/td&gt;
&lt;td&gt;export-wordpress-to-hugo&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;RStats&lt;/td&gt;
&lt;td&gt;export-wordpress-to-hugo&lt;/td&gt;
&lt;td&gt;analyse-site-structure&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;figcaption&gt;
Extract from adjacency list with two pages referring each other.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-4&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-4&#34;&gt;
Analyse Site Structure with iGraph
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-4&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;Network analysis is the perfect tool to analyse site structure because each post on the website is a node and a link between two articles is a graph edge (arrow). &lt;/p&gt;
&lt;p&gt;
The Lucid Manager currently discusses strategic and fun data analysis, but in the past, I wrote articles about water utility marketing and critical perspectives on management theories. The questions I like to answer with this analysis are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Are all pages connected to each other in one or more steps?&lt;/li&gt;
&lt;li&gt;What is the most linked page?&lt;/li&gt;
&lt;li&gt;Which pages link to themselves?&lt;/li&gt;
&lt;li&gt;Are there duplicated links within one post?&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The iGraph software transforms the link table to a network. Any posts and pages without any links (solitary pages) are added separately. Each post is also given a colour related to its category, and pages are a separate colour.&lt;/p&gt;
&lt;p&gt;
This analysis shows that the internal link structure contains several sub networks, which are groups of pages that are only linked to each other. This graph will help in to identify more linking opportunities to dissolve sub networks.&lt;/p&gt;
&lt;p&gt;
The &lt;a href=&#34;https://igraph.org/r/doc/degree.html&#34;&gt;degree function&lt;/a&gt; in iGraph determines the number of adjacent edges of each graph. The degree function can specify incoming and outgoing links. My article about service quality in water utilities is the most linked post on this site.
In network analysis, this is the node with the highest degree of linking.&lt;/p&gt;
&lt;p&gt;
The loops in the diagram indicate self-referencing articles. The &lt;a href=&#34;https://igraph.org/r/doc/which_multiple.html&#34;&gt;which_loop&lt;/a&gt; function identifies which arrows have the same start and end. The &lt;code class=&#34;verbatim&#34;&gt;E&lt;/code&gt; function shows the list of edges, which shows that four pages on my website refer to themselves.&lt;/p&gt;
&lt;p&gt;
Lastly, the &lt;a href=&#34;https://igraph.org/r/doc/which_multiple.html&#34;&gt;which_multiple&lt;/a&gt; function identifies any duplicated edges in the graph. Within this context, these are pages that contain the same link more than once. The Lucid Manager website had seventeen instances of duplicated links when I first analysed its structure.&lt;/p&gt;
&lt;p&gt;
This first phase gives me plenty of homework to improve the internal linking structure of this website.&lt;/p&gt;
&lt;p&gt;
&lt;figure&gt;&lt;a href=&#34;https://lucidmanager.org/images/digital-humanities/lucidmanager_links.png&#34;&gt;&lt;img src=&#34;https://lucidmanager.org/images/digital-humanities/lucidmanager_links.png&#34;
    alt=&#34;Visualising website structure.&#34;&gt;&lt;/a&gt;&lt;figcaption&gt;
      &lt;p&gt;Visualising website structure. (click to enlarge)&lt;/p&gt;
    &lt;/figcaption&gt;
&lt;/figure&gt;
&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  solitary &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(posts&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;post_name, pages&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;post_name)[&lt;span style=&#34;color:#333&#34;&gt;!&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(posts&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;post_name, pages&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;post_name) &lt;span style=&#34;color:#333&#34;&gt;%in%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                                                    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;unique&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(links&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;post_name, links&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;link)))]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  network &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;select&lt;/span&gt;(links, post_name, link) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;graph_from_data_frame&lt;/span&gt;(directed &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;TRUE&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;vertices&lt;/span&gt;(solitary)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  colour &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;tibble&lt;/span&gt;(post_name &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;V&lt;/span&gt;(network)&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;name) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;left_join&lt;/span&gt;(content) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;mutate&lt;/span&gt;(name &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;factor&lt;/span&gt;(name),
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;           colour &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;brewer.pal&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;length&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;unique&lt;/span&gt;(content&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;name)), &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Set2&amp;#34;&lt;/span&gt;)[name]) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;select&lt;/span&gt;(&lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt;post_content)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;V&lt;/span&gt;(network)&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;color &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;as.vector&lt;/span&gt;(colour&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;colour)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;par&lt;/span&gt;(mar &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;rep&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;4&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;plot&lt;/span&gt;(network,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;       layout &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; layout.fruchterman.reingold,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;       vertex.label.cex &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;.7&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;       vertex.size &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;degree&lt;/span&gt;(network),
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;       vertex.label.color &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;black&amp;#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;       vertex.frame.color &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;NA&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;       edge.arrow.size &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;.2&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;       edge.color &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;darkgray&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Diagnose network&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;which.max&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;degree&lt;/span&gt;(network, mode &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;in&amp;#34;&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;E&lt;/span&gt;(network)&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;[which_loop&lt;/span&gt;(network)]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;E&lt;/span&gt;(network)&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;[which_multiple&lt;/span&gt;(network)]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-5&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-5&#34;&gt;
Future improvements
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-5&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;This analysis is a great step to start systematically analysing organic website structure. Several other techniques help to understand the structure of a website further. Community detection and the inclusion of tags and categories in the data can provide additional context. SEO experts might find this concept useful. Perhaps somebody else can develop a Wordpress plugin to provide this visualisation.&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://www.r-bloggers.com/&#34; target=&#34;_blank&#34; title=&#34;Proudly associated with R-Bloggers&#34;&gt;
  &lt;button class=&#34;button is-link is-medium&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-r-project&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span&gt;As seen on R Bloggers&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;

      </description>
    </item>
    
    
    
    <item>
      <title>Decode Lyrics in Pop Music with the Songsim algorithm</title>
      <link>https://lucidmanager.org/data-science/decoding-lyrics-with-songsim/</link>
      <pubDate>Sat, 23 Mar 2019 03:14:17 +0000</pubDate>
      <author>Peter Prevos</author>
      <guid>https://lucidmanager.org/data-science/decoding-lyrics-with-songsim/</guid>
      <description>
	
&lt;p&gt;
Music is an inherently mathematical form of art. Ancient Greek mathematician Pythagoras was the first to describe the logic of the scales that form melody and harmony. Numbers can also represent the rhythm of the music. Even the lyrics have a mathematical structure. Poets structure syllables and repeat words to create pleasing sounding prose. This article shows how to decode lyrics from pop songs and visualise them using the Songsim method to analyse their metre.&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-1&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-1&#34;&gt;
Decode Lyrics using the Songsim algorithm
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-1&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;Data visualiser, pop music appreciator and machine learner &lt;a href=&#34;https://twitter.com/HalfEatenScone&#34;&gt;Colin Morris&lt;/a&gt; has extensively analysed the repetitiveness of song lyrics. Colin demonstrated that lyrics are becoming more repetitive since the early days of pop music. The most repetitive song is &lt;em&gt;Around the World&lt;/em&gt; by Daft Punk, which should not be a surprise since the artist repeats the same phrase 144 times. &lt;em&gt;Bohemian Rhapsody&lt;/em&gt; by Queen has some of the least repetitive lyrics in popular music.&lt;/p&gt;
&lt;p&gt;
The TedX presentation (see below) by Colin Morris shows how he visualises the repetitiveness of song lyrics with what he calls the &lt;a href=&#34;https://colinmorris.github.io/SongSim/&#34;&gt;Songsim&lt;/a&gt; algorithm. As a result, the more points in the graph, the more often the composer repeated a word&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/digital-humanities/daft-punk-queen.png&#34; alt=&#34;Visualisation of the lyrics of Daft Punk&amp;#39;s &amp;#39;Around the World&amp;#39; and Queen&amp;#39;s &amp;#39;Bohemian Rhapsody&amp;#39;&#34; title=&#34;Visualisation of the lyrics of Daft Punk&amp;#39;s &amp;#39;Around the World&amp;#39; and Queen&amp;#39;s &amp;#39;Bohemian Rhapsody&amp;#39;&#34;/&gt;
&lt;figcaption&gt;
Visualisation of the lyrics of Daft Punk&amp;#39;s &amp;#39;Around the World&amp;#39; and Queen&amp;#39;s &amp;#39;Bohemian Rhapsody&amp;#39;.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-2&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-2&#34;&gt;
The visual language of song lyrics
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-2&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;Morris decided to use a self-similarity matrix, which biologists use to visualise DNA sequences, to decode lyrics. In this method, the individual words of the song are the names of the columns and the names of the rows in a matrix. For every point in the song where the row name equals the column name, shows a dot. By definition, the diagonal of every similarity matrix is filled. The timeline of the song thus runs along the diagonal from top left to bottom right.&lt;/p&gt;
&lt;p&gt;
Patterns away from the diagonal represent two different points in time that have the same words. The more of these patterns we see, the more repetitive a song is. Let&amp;#39;s demonstrate this with the &lt;a href=&#34;https://www.youtube.com/watch?v=g3qPT30LejM&#34;&gt;first words ever recorded&lt;/a&gt; by Thomas Edison in 1877.&lt;/p&gt;
&lt;figure&gt;
&lt;div style=&#34;position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden;&#34;&gt;
      &lt;iframe allow=&#34;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share; fullscreen&#34; loading=&#34;eager&#34; referrerpolicy=&#34;strict-origin-when-cross-origin&#34; src=&#34;https://www.youtube.com/embed/g3qPT30LejM?autoplay=0&amp;amp;controls=1&amp;amp;end=0&amp;amp;loop=0&amp;amp;mute=0&amp;amp;start=0&#34; style=&#34;position: absolute; top: 0; left: 0; width: 100%; height: 100%; border:0;&#34; title=&#34;YouTube video&#34;&gt;&lt;/iframe&gt;
    &lt;/div&gt;

&lt;figcaption&gt;
Original Edison 1877 tin foil recording.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;blockquote&gt;
&lt;p&gt;Mary had a little lamb, whose fleece was white as snow. And everywhere that Mary went, the lamb was sure to go.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;
The similarity matrix below visualises the two first sentences of the famous nursery rhyme. It shows where the words &amp;#34;Mary&amp;#34;, &amp;#34;lamb&amp;#34; and &amp;#34;was&amp;#34; are repeated once.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/digital-humanities/edison-little-lamb.png&#34; alt=&#34;Self-similarity matrix for Mary had a Little Lamb by Thomas Edison.&#34; title=&#34;Self-similarity matrix for Mary had a Little Lamb by Thomas Edison.&#34;/&gt;
&lt;figcaption&gt;
Self-similarity matrix for Mary had a Little Lamb by Thomas Edison.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
The snowflake diagrams are a visual language to decode lyrics. The verses are the gutters with only diagonal lines. A verse is not very repetitive besides some stop words. The verse repeats through the song. Many songs have a bridge that contrasts with the rest of the song. As a result, the bridge is in most songs a unique pattern with self-similarity.&lt;/p&gt;
&lt;p&gt;
The diagram below visualises the lyrics of one of the most famous pop songs ever, &lt;em&gt;Waterloo&lt;/em&gt; by Abba. The first 30 words are the opening verse, which shows little repetition, other than stop words such as and the pronoun I. After that we see diagonal lines appearing that represent the repetitive use of the song title. Towards the end of the song, we see the bridge, which is like a little snowflake within the diagram.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/digital-humanities/abba-waterloo.png&#34; alt=&#34;/images/digital-humanities/abba-waterloo.png&#34; title=&#34;Decoding lyrics: Waterloo by Abba.&#34; at=&#34;Decoding lyrics: Waterloo by Abba.&#34;/&gt;
&lt;figcaption&gt;
Decoding lyrics: Waterloo by Abba.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
The next section shows how to implement this approach with ggplot, scraping pop song lyrics from the azlyrics.com website.&lt;/p&gt;
&lt;figure&gt;
&lt;div style=&#34;position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden;&#34;&gt;
      &lt;iframe allow=&#34;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share; fullscreen&#34; loading=&#34;eager&#34; referrerpolicy=&#34;strict-origin-when-cross-origin&#34; src=&#34;https://www.youtube.com/embed/_tjFwcmHy5M?autoplay=0&amp;amp;controls=1&amp;amp;end=0&amp;amp;loop=0&amp;amp;mute=0&amp;amp;start=0&#34; style=&#34;position: absolute; top: 0; left: 0; width: 100%; height: 100%; border:0;&#34; title=&#34;YouTube video&#34;&gt;&lt;/iframe&gt;
    &lt;/div&gt;

&lt;figcaption&gt;
Pop Music is Stuck on Repeat | Colin Morris | TEDxPenn
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-3&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-3&#34;&gt;
Implementing Songsim with ggplot
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-3&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The code below visualises song lyrics or poetry as suggested by Colin Morris. The code uses four libraries. I use the tidyverse series of libraries because it makes life very easy. The tidytext library uses the tidyverse principles to analyse text. The old reshape2 library helps to transform a matrix, and lastly, rvest helps to scrape song lyrics from the azlyrics website.&lt;/p&gt;
&lt;p&gt;
The first function scrapes song lyrics from the azlyrics website using the artist and song as input. The first three lines clean the artist and song variables. This code removes any character that is not a number or a letter, converts to lowercase and lastly removes the definite article in the artist name. These two fields are then concatenated to create the URL, which the function prints. The remainder of the code scrapes the lyrics from the website or trips on an error 404 when it cannot find the song/artist combination.&lt;/p&gt;
&lt;p&gt;
The second function implements the Morris method to visualise the lyrics. The code extracts single words from the text and places them in a data frame (tibble). This data frame is subsequently converted to a boolean matrix that contains the visualisation.&lt;/p&gt;
&lt;p&gt;
The code looks at each word and places the value TRUE where reappears in the song. Each of the vectors is then concatenated to a matrix. Lastly, ggplot visualises the matrix is visualised as a raster.&lt;/p&gt;
&lt;p&gt;
What does your favourite song look like a snowflake diagram?&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://github.com///&#34; target=&#34;_blank&#34;
   title=&#34;Download  from GitHub&#34;
   alt=&#34;Download  from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Decoding lyrics&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(tidyverse)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(tidytext)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(reshape2)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(rvest)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  get_lyrics &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;function&lt;/span&gt;(artist, song) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      artist &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;gsub&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;[^A-Za-z0-9]+&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;tolower&lt;/span&gt;(artist))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      song &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;gsub&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;[^A-Za-z0-9]+&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;tolower&lt;/span&gt;(song))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      artist &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;gsub&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;^the&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;&amp;#34;&lt;/span&gt;, artist)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      url &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;paste&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;http://azlyrics.com/lyrics/&amp;#34;&lt;/span&gt;, 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                  artist, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;/&amp;#34;&lt;/span&gt;, song, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;.html&amp;#34;&lt;/span&gt;, sep &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;print&lt;/span&gt;(url)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      azlyrics &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;read_html&lt;/span&gt;(url)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      lyrics &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;html_nodes&lt;/span&gt;(azlyrics, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;div&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      lyrics &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;html_text&lt;/span&gt;(lyrics[23])
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;gsub&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;\r|\n&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34; &amp;#34;&lt;/span&gt;, lyrics)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  plot_snowflake &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;function&lt;/span&gt;(artist, song){
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      lyrics &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;get_lyrics&lt;/span&gt;(artist, song)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      lyrics &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;data_frame&lt;/span&gt;(line &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; lyrics) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;filter&lt;/span&gt;(line &lt;span style=&#34;color:#333&#34;&gt;!=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      words &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; lyrics  &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;unnest_tokens&lt;/span&gt;(word, line) 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      words_matrix &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;lapply&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;nrow&lt;/span&gt;(words),
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                             &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;function&lt;/span&gt;(w){
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                                 &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;as.character&lt;/span&gt;(words[w, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;]) &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; words
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                             }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                             ) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;do.call&lt;/span&gt;(cbind, .)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;rownames&lt;/span&gt;(words_matrix) &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;nrow&lt;/span&gt;(words)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;colnames&lt;/span&gt;(words_matrix) &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;nrow&lt;/span&gt;(words)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;melt&lt;/span&gt;(words_matrix, varnames &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;x&amp;#34;&lt;/span&gt;,  &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;y&amp;#34;&lt;/span&gt;)) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ggplot&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;aes&lt;/span&gt;(x, &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt;y, fill &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; value)) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;geom_raster&lt;/span&gt;() &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;scale_fill_manual&lt;/span&gt;(values &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;white&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;dodgerblue4&amp;#34;&lt;/span&gt;), guide &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;FALSE&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;theme_void&lt;/span&gt;() &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;     
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ggtitle&lt;/span&gt;(artist, subtitle &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; song)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;plot_snowflake&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Abba&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Waterloo&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ggsave&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Abba-Waterloo.png&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  artist &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Thomas Edison&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  song &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Mary Had a Little Lamb&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  lyrics &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Mary had a little lamb, whose fleece was white as snow. And everywhere that Mary went, the lamb was sure to go.&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(gridExtra)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;png&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;DaftPunk-Queen.png&amp;#34;&lt;/span&gt;, width &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1024&lt;/span&gt;, height &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;768&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  l1 &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;plot_snowflake&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Daft Punk&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Around the world&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  l2 &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;plot_snowflake&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Queen&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Bohemian Rhapsody&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;grid.arrange&lt;/span&gt;(l1, l2, ncol &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;dev.off&lt;/span&gt;()
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;getwd&lt;/span&gt;()
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  artist &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Frank Zappa&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  song &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Titties Beer&amp;#34;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;a href = &#34;https://www.r-bloggers.com/&#34; target=&#34;_blank&#34; title=&#34;Proudly associated with R-Bloggers&#34;&gt;
  &lt;button class=&#34;button is-link is-medium&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-r-project&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span&gt;As seen on R Bloggers&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/div&gt;
&lt;/div&gt;

      </description>
    </item>
    
    
    
    <item>
      <title>Strategic Data Science: Create Value from Data Big and Small</title>
      <link>https://lucidmanager.org/data-science/strategic-data-science/</link>
      <pubDate>Sun, 24 Feb 2019 00:00:00 +0000</pubDate>
      <author>Peter Prevos</author>
      <guid>https://lucidmanager.org/data-science/strategic-data-science/</guid>
      <description>
	
&lt;p&gt;
Data science is without a doubt the most popular business fad of the past decade. The wonderful promises of machine learning blind many managers, so they forget about deploying these new approaches strategically. To create value from data, managers need to use a strategic approach to data science. This article provides a framework for data science, following my book &lt;em&gt;Principles of Strategic Data Science&lt;/em&gt;, published by Packt.&lt;/p&gt;
&lt;p&gt;

  &lt;div class=&#34;box&#34;&gt;
    &lt;div class=&#34;media&#34;&gt;
      &lt;figure class=&#34;media-left&#34;&gt;
        &lt;p class=&#34;image is-128x128&#34;&gt;
          &lt;img src=&#34;https://lucidmanager.org/images/books/2019_sds.jpg&#34; alt = &#34;Principles of Strategic Data Science&#34; title = &#34;Principles of Strategic Data Science&#34;&gt;
        &lt;/p&gt;
      &lt;/figure&gt;
      &lt;div class=&#34;media-content&#34;&gt;
        &lt;div class=&#34;content&#34;&gt;
          &lt;p class=&#34;is-size-5 has-text-weight-bold&#34;&gt;Principles of Strategic Data Science&lt;/p&gt;
          &lt;p class=&#34;mb-2&#34;&gt;Principles of Strategic Data Science helps you join the dots between mathematics, programming, and business analysis. With a unique approach that bridges the gap between mathematics and computer science, this book takes you through the entire data science pipeline.&lt;/p&gt;
          &lt;div class=&#34;buttons&#34;&gt;
            &lt;a class=&#34;button is-info&#34; href=&#34;https://www.packtpub.com/en-us/product/principles-of-strategic-data-science-9781838985509&#34; target=&#34;_blank&#34;&gt;
              &lt;span class=&#34;icon&#34;&gt;
                &lt;i class=&#34;fas fa-shopping-cart&#34;&gt;&lt;/i&gt;
              &lt;/span&gt;
              &lt;span&gt;Packt Publishing&lt;/span&gt;
            &lt;/a&gt;
          &lt;/div&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-1&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-1&#34;&gt;
What is Data Science?
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-1&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The manager to understand the importance of data was &lt;a href=&#34;https://lucidmanager.org/management/frederick-taylor-management-fundamentalism/&#34;&gt;Frederick Taylor&lt;/a&gt;. He was a pioneer in using data to manage a business. He used data because he wanted to lower the influence of opinions and rules-of-thumb in favour of a scientific approach to management. The term data science emerged in the middle of the last century when electronic computation first became a topic of study. In those days, the discipline was literally a science of storing and manipulating data.&lt;/p&gt;
&lt;p&gt;
The current definition has drifted away from this initial academic activity to a business activity. The current data science hype can be traced back to an article in the 2012 edition of &lt;a href=&#34;https://hbr.org/2012/10/data-scientist-the-sexiest-job-of-the-21st-century&#34;&gt;Harvard Business Review&lt;/a&gt;. Davenport and Patil proclaimed data scientist to be the sexiest job of the twenty-first century. Subsequently, in the wake of this article, the number of data science searches in Google increased rapidly.&lt;/p&gt;
&lt;p&gt;
Organisations have for a long time used data to improve the lives of their customers, shareholders or society overall. Management gurus promoted concepts such as the data-driven organisation, evidence-based management, business intelligence and Six Sigma to help businesses realise the benefits of their data. Data science is an evolution of these methods enabled by the data revolution.&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-2&#34; class=&#34;outline-3&#34;&gt;
&lt;h3 id=&#34;headline-2&#34;&gt;
The Data Revolution
&lt;/h3&gt;
&lt;div id=&#34;outline-text-headline-2&#34; class=&#34;outline-text-3&#34;&gt;
&lt;p&gt;Recent developments in information technology have significantly improved what we can do with data, resulting in what we now know as data science. Firstly, most business processes are managed electronically, which has exponentially increased the amount of available data. Developments in communication, such as the Internet of Things and personal mobile devices, have significantly reduced the price of collecting data.&lt;/p&gt;
&lt;p&gt;
Secondly, the computing capabilities on the average office worker&amp;#39;s desk outstrip the capabilities of the supercomputers of the past. Not only is it cheaper to collect vast amounts of electronic data, but processing these enormous volumes has also come within reach of the average office worker.&lt;/p&gt;
&lt;p&gt;
Lastly, developments in applied mathematics and open source licensing have accelerated our capabilities in analysing this data. These new technologies allow us to discover patterns that were previously invisible. Data science is available to anyone wanting to learn because most tools to examine data are freely available on the internet with a helpful community sharing knowledge on how to use them.&lt;/p&gt;
&lt;p&gt;
These three developments enabled an evolution from traditional business analysis to data science. Subsequently, data science has become the strategic and &lt;em&gt;systematic&lt;/em&gt; approach to analysing data to achieve organisational objectives using electronic computing. This definition is agnostic of the promises of machine learning and leverages the three developments mentioned above. Data science as such is the next evolution in business analysis that maximises the value we can extract from data.&lt;/p&gt;
&amp;nbsp;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-3&#34; class=&#34;outline-3&#34;&gt;
&lt;h3 id=&#34;headline-3&#34;&gt;
Data Science Strategy Competencies
&lt;/h3&gt;
&lt;div id=&#34;outline-text-headline-3&#34; class=&#34;outline-text-3&#34;&gt;
&lt;p&gt;The craft of data science combines three different competencies. To illustrate this idea, data scientist Drew Conway visualised these core competencies of data science in a &lt;a href=&#34;http://drewconway.com/zia/2013/3/26/the-data-science-venn-diagram&#34;&gt;Venn diagram&lt;/a&gt;.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/data-science/conway-venn-diagram.png&#34; alt=&#34;Conway Venn Diagram&#34; title=&#34;Conway Venn Diagram&#34; width=&#34;500&#34;/&gt;
&lt;figcaption&gt;
Data science competencies (Based on Conway, 2009).
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
Firstly and most importantly, data science requires &lt;em&gt;domain knowledge&lt;/em&gt;. Any analysis needs to be grounded in the reality it seeks to improve. Subject-matter expertise is a necessary condition to make sense of an investigation. Professional expertise in most areas uses &lt;em&gt;mathematics&lt;/em&gt; to understand and improve outcomes. New mathematical tools expand the traditional approaches to develop a deeper understanding of the domain under consideration. Lastly, &lt;em&gt;computer science&lt;/em&gt; is the competency that binds the available data with mathematics. Writing computer code to extract, transform and analyse data to create information and stimulate knowledge is an essential skill for any data scientist.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-4&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-4&#34;&gt;
Good Data Science
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-4&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;In order to create value with data, we need to know how to create or recognise good data science. The second chapter of the book uses three principles originally introduced two thousand years ago by the Roman architect and engineer Vitruvius. He wrote that buildings need to be &lt;em&gt;useful&lt;/em&gt;, &lt;em&gt;sound&lt;/em&gt; and &lt;em&gt;aesthetic&lt;/em&gt;. These requirements are also ideally suited to define best-practice in data science.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/data-science/vitrivius.png&#34; alt=&#34;The Vitruvian triangle for data science&#34; title=&#34;The Vitruvian triangle for data science&#34; width=&#34;400&#34;/&gt;
&lt;figcaption&gt;
The Vitruvian triangle for data science.
&lt;/figcaption&gt;
&lt;/figure&gt;
&amp;nbsp;
&lt;div id=&#34;outline-container-headline-5&#34; class=&#34;outline-3&#34;&gt;
&lt;h3 id=&#34;headline-5&#34;&gt;
Usefulness
&lt;/h3&gt;
&lt;div id=&#34;outline-text-headline-5&#34; class=&#34;outline-text-3&#34;&gt;
&lt;p&gt;For data science to be useful, it needs to contribute to the objectives of an organisation positively. It is in this sense that data science is an applied science and not an academic pursuit. The famous Data-Information-Knowledge pyramid visualises the process of creating value from data.&lt;/p&gt;
&lt;p&gt;
&lt;em&gt;Useful&lt;/em&gt; data science meaningfully improves our reality through data. Data is a representation of either a social or physical reality. Any data source is ever only a sample of the fullness and complexity of the real world. Information is data imbued with context. The raw data collected from reality needs to be summarised, visualised and analysed so that managers understand the reality of their business. This information increases knowledge about a business process, which is subsequently used to improve the reality from which the data was collected. This feedback loop visualises the essence of analysing data in businesses. Data science is a seductive activity because it is reasonably straightforward to create impressive visualisations with sophisticated algorithms. If data products don&amp;#39;t improve or enlighten the current situation, they are in essence useless.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/data-science/feedback-loop.png&#34; alt=&#34;Reality - Data - Information - Knowledge pyramid&#34; title=&#34;Reality - Data - Information - Knowledge pyramid&#34; width=&#34;500&#34;/&gt;
&lt;figcaption&gt;
Reality - Data - Information - Knowledge pyramid.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-6&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-6&#34;&gt;
Soundness
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-6&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;Data science needs to be &lt;em&gt;sound&lt;/em&gt; to ensure that the outcomes are valid and reliable. The validity and reliability of data are where the science meets the traditional approaches to analysing data. Validity is the extent to which the data represents the reality it describes. The reliability of data relates to the accuracy of the measurement. These
two concepts depend on the type of data under consideration. Measuring physical processes is less complicated than the social aspects of society. Validity and reliability are in essence a sophisticated way of expressing the well-known Garbage-In-Garbage-Out principle.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/data-science/soundness.png&#34; alt=&#34;/images/data-science/soundness.png&#34; title=&#34;Reliability and validity of data and analysis&#34; width=&#34;500&#34;/&gt;
&lt;figcaption&gt;
Reliability and validity of data and analysis.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
The soundness of data science also relates to the reproducibility of the analysis to ensure that other professionals can review the outcomes. Reproducibility prevents that the data and the process by which it was transformed and analysed become a black-box where we have no reason to trust the results. Data science also needs to be sound concerning the governance of the workflow. All data sources need to be curated by relevant subject matter experts to ensure their validity and reliability. Data experts provide that the data is available to those who need it.&lt;/p&gt;
&amp;nbsp;
&lt;div id=&#34;outline-container-headline-7&#34; class=&#34;outline-3&#34;&gt;
&lt;h3 id=&#34;headline-7&#34;&gt;
Aesthetics
&lt;/h3&gt;
&lt;div id=&#34;outline-text-headline-7&#34; class=&#34;outline-text-3&#34;&gt;
&lt;p&gt;Lastly, data science needs to be &lt;em&gt;aesthetic&lt;/em&gt; to ensure that any visualisation or report is easy to understand by the consumer of the analysis. This requirement is not about beautification through infographics. Aesthetic data products minimise the risk or making wrong decisions because the information is presented without room for misinterpretation. Any visualisation needs to focus on telling a story with the data. This story can be a comparison, a prediction, a trend or whatever else is relevant to the problem.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/data-science/data-pixel-ratio.jpg&#34; alt=&#34;Example of low and high data-to-pixel ratio&#34; title=&#34;Example of low and high data-to-pixel ratio&#34; width=&#34;500&#34;/&gt;
&lt;figcaption&gt;
Example of low and high data-to-pixel ratio.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
One of the essential principles of aesthetic data science is the data-to-pixel ratio. This principle means that we need to maximise the ratio between all the pixels on a screen and those pixels that present information. Good data visualisation practices austerity to ensure that the people that consume the information understand the story that needs to be told.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-8&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-8&#34;&gt;
Strategic Data Science
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-8&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The data science continuum is a strategic journey for organisations that seek to maximise value from data. As an organisation moves along the continuum, increased complexity is the payoff for increased value. This continuum looks like hierarchy, but all phases are equally important. The last stages cannot exist without the previous ones. Therefore, implementing machine learning without a solid grasp of data collection will result in useless outcomes.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/data-science/data-science-continuum.png&#34; alt=&#34;Data science strategy continuum&#34; title=&#34;Data science strategy continuum&#34; width=&#34;500&#34;/&gt;
&lt;figcaption&gt;
Data science strategy continuum.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
Collecting data requires considerations on what to collect, how to collect it and at what frequency. To collect meaningful data requires a good understanding of the relationship between reality and data. There is no such thing as raw data as all information relies on assumptions and practical limitations.&lt;/p&gt;
&amp;nbsp;
&lt;div id=&#34;outline-container-headline-9&#34; class=&#34;outline-3&#34;&gt;
&lt;h3 id=&#34;headline-9&#34;&gt;
Type of Value from Data
&lt;/h3&gt;
&lt;div id=&#34;outline-text-headline-9&#34; class=&#34;outline-text-3&#34;&gt;
&lt;ul&gt;
&lt;li&gt;Describing the data is the first step in extracting value.&lt;/li&gt;
&lt;li&gt;Descriptive statistics are the core of most business reporting and are
an essential first step in analysing the data.&lt;/li&gt;
&lt;li&gt;Diagnostics or analysis is the core activity of most professions. Each
subject area uses specialised methods to create new information from
data.&lt;/li&gt;
&lt;li&gt;Predictive analysis seems to be the holy grail for many managers. A
prediction is not a perfect description of the future but it provides
the distribution of possible futures. Managers can use this
information to change the present to construct their desired future.&lt;/li&gt;
&lt;li&gt;Prescriptive analysis uses the knowledge created in the previous
phases to automatically run a business process and even decide on
future courses of action.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Any organisation starting with data science should follow the five phases in this process and not jump ahead to try to bypass the seemingly less valuable stages.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-10&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-10&#34;&gt;
The Data-Driven Organisation
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-10&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;Implementing a data science strategy is more than a matter of establishing a specialised team and solve complex problems. Creating a data-driven organisation that maximises the value of data requires a whole-of-business approach that involves people with the right attitude and skills, appropriate systems and robust processes.&lt;/p&gt;
&lt;p&gt;
A data science team combines the three competencies described in the Conway Venn diagram. People that have skills in all three of these areas are rare, and the industry calls them unicorns. There is no need for recruiters to start hunting unicorns because these three areas of expertise can also exist within a team. Possibly more important than the technical skills are the social skills of a data scientist. Not only need they create useful, sound and aesthetic data science, they also need to convince the consumers of their work of its value.&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-11&#34; class=&#34;outline-3&#34;&gt;
&lt;h3 id=&#34;headline-11&#34;&gt;
Data Literacy
&lt;/h3&gt;
&lt;div id=&#34;outline-text-headline-11&#34; class=&#34;outline-text-3&#34;&gt;
&lt;p&gt;One of the problems of creating value with data is ensuring that the results are implemented in the organisation. A starting point to achieve this is to ensure that the users of data products have a relevant level of data literacy. Developing data literacy among consumers of data science is probably one of the greatest challenges. The required level of data literacy depends on the type of position and the role of the data consumer within the organisation.&lt;/p&gt;
&lt;p&gt;
Data scientists use an extensive range of tools and are often opportunistic in their choice of software. &lt;a href=&#34;https://lucidmanager.org/data-science/spreadsheets-for-data-science/&#34;&gt;Spreadsheets&lt;/a&gt; are not very suitable to create good data science. Data science requires coding skills and the Python and R languages are powerful tools to solve complex problems. After the data specialists have developed the best way to analyse data, they need to communicate these to their customers. Many specific products exist to communicate data to users with interactive dashboards and many other dynamic systems.&lt;/p&gt;
&lt;p&gt;
The final part of this book on data science strategy delves into the ethics of data science. From the fact that something can be done, we certainly cannot conclude that it should be done. Just like any other profession that impacts humans, data scientists need ethical guidelines to ensure that their activities cause no harm. The book provides some basic guidelines that can assist data scientists to assess the ethical merits of their projects.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-12&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-12&#34;&gt;
Summary
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-12&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;In summary, data science is about more than using the latest algorithms to analyse data. Good data science has to be useful in that it positively influences the objectives of the organisation. Furthermore, good data science has to be sound through a methodical approach.&lt;/p&gt;
&lt;p&gt;
My books &lt;em&gt;Principles of Strategic Data Science&lt;/em&gt; and &lt;em&gt;Data Science for Water Utilities&lt;/em&gt; provide detailed insights into these topics.&lt;/p&gt;
&lt;p&gt;

  &lt;div class=&#34;box&#34;&gt;
    &lt;div class=&#34;media&#34;&gt;
      &lt;figure class=&#34;media-left&#34;&gt;
        &lt;p class=&#34;image is-128x128&#34;&gt;
          &lt;img src=&#34;https://lucidmanager.org/images/books/2019_sds.jpg&#34; alt = &#34;Principles of Strategic Data Science&#34; title = &#34;Principles of Strategic Data Science&#34;&gt;
        &lt;/p&gt;
      &lt;/figure&gt;
      &lt;div class=&#34;media-content&#34;&gt;
        &lt;div class=&#34;content&#34;&gt;
          &lt;p class=&#34;is-size-5 has-text-weight-bold&#34;&gt;Principles of Strategic Data Science&lt;/p&gt;
          &lt;p class=&#34;mb-2&#34;&gt;Principles of Strategic Data Science helps you join the dots between mathematics, programming, and business analysis. With a unique approach that bridges the gap between mathematics and computer science, this book takes you through the entire data science pipeline.&lt;/p&gt;
          &lt;div class=&#34;buttons&#34;&gt;
            &lt;a class=&#34;button is-info&#34; href=&#34;https://www.packtpub.com/en-us/product/principles-of-strategic-data-science-9781838985509&#34; target=&#34;_blank&#34;&gt;
              &lt;span class=&#34;icon&#34;&gt;
                &lt;i class=&#34;fas fa-shopping-cart&#34;&gt;&lt;/i&gt;
              &lt;/span&gt;
              &lt;span&gt;Packt Publishing&lt;/span&gt;
            &lt;/a&gt;
          &lt;/div&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;

  &lt;div class=&#34;box&#34;&gt;
    &lt;div class=&#34;media&#34;&gt;
      &lt;figure class=&#34;media-left&#34;&gt;
        &lt;p class=&#34;image is-128x128&#34;&gt;
          &lt;img src=&#34;https://lucidmanager.org/images/books/2023_ds4wu.jpg&#34; alt = &#34;Data Science for Water Utilities&#34; title = &#34;Data Science for Water Utilities&#34;&gt;
        &lt;/p&gt;
      &lt;/figure&gt;
      &lt;div class=&#34;media-content&#34;&gt;
        &lt;div class=&#34;content&#34;&gt;
          &lt;p class=&#34;is-size-5 has-text-weight-bold&#34;&gt;Data Science for Water Utilities&lt;/p&gt;
          &lt;p class=&#34;mb-2&#34;&gt;Data Science for Water Utilities published by CRC Press is an applied, practical guide that shows water professionals how to use data science to solve urban water management problems using the R language for statistical computing.&lt;/p&gt;
          &lt;div class=&#34;buttons&#34;&gt;
            &lt;a class=&#34;button is-info&#34; href=&#34;https://routledge.com/9781032354545&#34; target=&#34;_blank&#34;&gt;
              &lt;span class=&#34;icon&#34;&gt;
                &lt;i class=&#34;fas fa-shopping-cart&#34;&gt;&lt;/i&gt;
              &lt;/span&gt;
              &lt;span&gt;Routledge&lt;/span&gt;
            &lt;/a&gt;
            &lt;a class=&#34;button is-info&#34; href=&#34;https://www.amazon.com/Data-Science-Water-Utilities-Chapman/dp/1032354550&#34; target=&#34;_blank&#34;&gt;
              &lt;span class=&#34;icon&#34;&gt;
                &lt;i class=&#34;fas fa-shopping-cart&#34;&gt;&lt;/i&gt;
              &lt;/span&gt;
              &lt;span&gt;Amazon&lt;/span&gt;
            &lt;/a&gt;
          &lt;/div&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;

      </description>
    </item>
    
    
    
    <item>
      <title>Factor Analysis in R: Measuring Consumer Involvement</title>
      <link>https://lucidmanager.org/data-science/measuring-consumer-involvement/</link>
      <pubDate>Sat, 19 Jan 2019 00:00:00 +0000</pubDate>
      <author>Peter Prevos</author>
      <guid>https://lucidmanager.org/data-science/measuring-consumer-involvement/</guid>
      <description>
	
&lt;p&gt;
The first step for anyone who wants to promote or sell something is to understand the psychology of potential customers. Getting into consumers&amp;#39; minds is often problematic because measuring psychological traits is complex. Consumer involvement is a measure of people&amp;#39;s attitudes towards a product or service. This article introduces the concept of consumer involvement. An example using data from tap water consumers illustrates the theory. This article analyses data collected from these consumers using factor analysis in R, with the &lt;a href=&#34;https://personality-project.org/r/&#34;&gt;psych package&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;
The most common method to measure psychological traits is to ask people several questions. Analysing this data is complicated because it is difficult to determine how the survey responses relate to the software of the mind. While the answers given by survey respondents are the directly measured variables, we like to know the hidden (latent) states in their minds. Factor Analysis is a technique that identifies latent variables within a data set, such as a customer survey.&lt;/p&gt;
&lt;p&gt;
The basic principle of measuring consumer attitudes is that their state of mind causes them to respond in a certain way. Factor analysis reverses this causality by analysing patterns in responses that indicate the consumer&amp;#39;s state of mind. Using a computing analogy, factor analysis is a technique for reverse-engineering source code by analysing the input and output.&lt;/p&gt;
&lt;p&gt;
The data and code for this article are available on GitHub in the &lt;code class=&#34;verbatim&#34;&gt;case-studies&lt;/code&gt; folder:&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://github.com/pprevos/r4h2o/&#34; target=&#34;_blank&#34;
   title=&#34;Download r4h2o from GitHub&#34;
   alt=&#34;Download r4h2o from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;r4h2o&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-1&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-1&#34;&gt;
What is Consumer Involvement?
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-1&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;Involvement is a marketing metric that describes the relevance of a product or service in somebody&amp;#39;s life. &lt;a href=&#34;http://www.sfu.ca/~zaichkow/research.html&#34;&gt;Judy Zaichkowsky&lt;/a&gt; defines consumer involvement formally as “a person&amp;#39;s perceived relevance of the object based on inherent needs, values, and interests”. People who own a car will most likely be highly involved with purchasing and driving the vehicle due to the money involved and the social role it plays in &lt;a href=&#34;https://horizonofreason.com/culture/first-law-of-consumer-behaviour/&#34;&gt;developing their public self&lt;/a&gt;. Consumers will most likely have a much lower level of involvement with the instant coffee they drink than with their clothes.&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-2&#34; class=&#34;outline-3&#34;&gt;
&lt;h3 id=&#34;headline-2&#34;&gt;
Managerial Relevance
&lt;/h3&gt;
&lt;div id=&#34;outline-text-headline-2&#34; class=&#34;outline-text-3&#34;&gt;
&lt;p&gt;The level of consumer involvement depends on a complex array of factors. These factors relate to psychology, situational factors, and the service provider&amp;#39;s marketing mix. The lowest level of involvement is considered a state of inertia which occurs when people habitually purchase a product without comparing alternatives.&lt;/p&gt;
&lt;p&gt;
From a managerial point of view, involvement is crucial because it is causally related to willingness to pay and perceptions of quality. Consumers with a higher level of involvement are willing to pay more for a service and have a more favourable perception of quality. Understanding involvement in the context of urban water supply is also crucial because sustainably managing water as a common pool resource requires the active involvement of all users.&lt;/p&gt;
&lt;p&gt;
Cult products have the highest possible level of involvement as customers are fully devoted to a particular product or brand. Commercial organisations use this knowledge to their advantage by maximising consumer involvement through branding and advertising. This strategy is used effectively by the &lt;a href=&#34;https://lucidmanager.org/marketing/bottled-water-marketing/&#34;&gt;bottled water industry&lt;/a&gt;. Manufacturers focus on enhancing the emotional aspects of their products rather than on improving the cognitive elements. Water utilities tend to use a reversed strategy and emphasise the cognitive aspects of tap water, the pipes, plants and pumps rather than trying to create an emotional relationship with their consumers. &lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-3&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-3&#34;&gt;
Measuring Consumer Involvement
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-3&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;For my dissertation about &lt;a href=&#34;https://lucidmanager.org/tags/invisible-water-utility/&#34;&gt;customer service in water utilities&lt;/a&gt;, I measured the level of involvement that consumers have with tap water using the &lt;em&gt;Personal Involvement Index&lt;/em&gt; (PII).&lt;/p&gt;
&lt;p&gt;

  &lt;div class=&#34;box&#34;&gt;
    &lt;div class=&#34;media&#34;&gt;
      &lt;figure class=&#34;media-left&#34;&gt;
        &lt;p class=&#34;image is-128x128&#34;&gt;
          &lt;img src=&#34;https://lucidmanager.org/images/books/2017b_wum.jpg&#34; alt = &#34;Customer Experience Management for Water Utilities: Marketing Urban Water Supply&#34; title = &#34;Customer Experience Management for Water Utilities: Marketing Urban Water Supply&#34;&gt;
        &lt;/p&gt;
      &lt;/figure&gt;
      &lt;div class=&#34;media-content&#34;&gt;
        &lt;div class=&#34;content&#34;&gt;
          &lt;p class=&#34;is-size-5 has-text-weight-bold&#34;&gt;Customer Experience Management for Water Utilities: Marketing Urban Water Supply&lt;/p&gt;
          &lt;p class=&#34;mb-2&#34;&gt;Practical framework for water utilities to become more focused on their customers following Service-Dominant Logic.&lt;/p&gt;
          &lt;div class=&#34;buttons&#34;&gt;
            &lt;a class=&#34;button is-info&#34; href=&#34;https://iwaponline.com/ebooks/book/327/Customer-Experience-Management-for-Water&#34; target=&#34;_blank&#34;&gt;
              &lt;span class=&#34;icon&#34;&gt;
                &lt;i class=&#34;fas fa-shopping-cart&#34;&gt;&lt;/i&gt;
              &lt;/span&gt;
              &lt;span&gt;IWA Publishing&lt;/span&gt;
            &lt;/a&gt;
          &lt;/div&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
Asking consumers directly about their level of involvement would not yield a stable answer, as each respondent would interpret the question differently. The best way to measure psychological states or psychometrics is to ask a series of questions linguistically related to the topic of interest.&lt;/p&gt;
&lt;p&gt;
The most cited method for measuring consumer involvement is the &lt;em&gt;Personal Involvement Index&lt;/em&gt;, developed by Judy Zaichowsky. This index is a two-dimensional scale consisting of the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Cognitive involvement (importance, relevance, meaning, value, and need)&lt;/li&gt;
&lt;li&gt;Affective involvement (involvement, fascination, appeal, excitement, and interest).&lt;/li&gt;
&lt;/ul&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/hydroinformatics/pii-model.png&#34; alt=&#34;Personal Involvement Inventory measurement model&#34; title=&#34;Personal Involvement Inventory measurement model&#34; width=&#34;400&#34;/&gt;
&lt;figcaption&gt;
Personal Involvement Inventory measurement model.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
The survey instrument consists of ten &lt;em&gt;Semantic Differential&lt;/em&gt; items. A Semantic Differential is a rating scale designed to measure the meaning of objects, events, or concepts. The researcher translates the concept, such as involvement, into a list of synonyms and their associated antonyms.&lt;/p&gt;
&lt;p&gt;
In the involvement survey, participants position their views along a continuum, such as Worthless-Valuable or Boring-Interesting. The level of involvement is the sum of all answers, ranging from 10 to 70.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/hydroinformatics/involvement-pii-scale.jpg&#34; alt=&#34;Personal Involvement Inventory semantic-differential scale&#34; title=&#34;Personal Involvement Inventory semantic-differential scale&#34;/&gt;
&lt;figcaption&gt;
Personal Involvement Inventory semantic-differential scale (Zaichowsky 1994).
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-4&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-4&#34;&gt;
Obtaining the Data
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-4&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The data form part of a survey of tap water customers. &lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Consumer Involvement&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(tidyverse)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(psych)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;# Clean data&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  customers &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;read_csv&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;data/customer_survey.csv&amp;#34;&lt;/span&gt;)[&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;-1&lt;/span&gt;, ] &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;type_convert&lt;/span&gt;() &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;filter&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;is.na&lt;/span&gt;(term)) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;select&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;21&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;51&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;-33&lt;/span&gt;)) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;rename&lt;/span&gt;(customer_id &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
The scale uses reverse polarity as some of the items are from low to high (boring – interesting) and others from high to low (important – unimportant), so we need to correct this by reversing the scores for six items. The data also has missing values, so we only use the complete cases.&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#888&#34;&gt;# Correct polarity&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;pii &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;select&lt;/span&gt;(customers, customer_id, &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;starts_with&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;p&amp;#34;&lt;/span&gt;)) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;mutate&lt;/span&gt;(p01 &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;8&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; p01,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;         p02 &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;8&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; p02,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;         p07 &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;8&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; p07,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;         p08 &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;8&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; p08,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;         p09 &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;8&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; p09,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;         p10 &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;8&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; p10)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#888&#34;&gt;# Remove missing values&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;pii &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; pii&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;[complete.cases&lt;/span&gt;(pii), ]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-5&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-5&#34;&gt;
Exploratory Analysis
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-5&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;
This data set contains additional information, and the code selects only variable names that start with &amp;#34;&lt;em&gt;p&lt;/em&gt;&amp;#34; (for the Personal Involvement Inventory). Before we analyse data, we remove customers who provided the same answers to all items or did not respond to all questions. These responses are most likely invalid, which leaves 757 rows of data.&lt;/p&gt;
&lt;p&gt;
A boxplot is a convenient way to visualise responses to multiple survey items in a single chart. This plot immediately shows an interesting pattern in the answers. Responses to the first five items were generally higher than those for the last five. This result indicates a distinction between cognitive and affective involvement.&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#888&#34;&gt;# Visualise PII&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    pii_long &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; pii &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;pivot_longer&lt;/span&gt;(&lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt;customer_id, names_to &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Item&amp;#34;&lt;/span&gt;, values_to &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Response&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ggplot&lt;/span&gt;(pii_long) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;aes&lt;/span&gt;(Item, Response) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;geom_boxplot&lt;/span&gt;(fill &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;#f7941d&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;theme_bw&lt;/span&gt;(base_size &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;12&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;labs&lt;/span&gt;(title &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Personal Involvement Index&amp;#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;           subtitle &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;paste0&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Tap Water Consumers USA and Australia (n = &amp;#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                            &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;nrow&lt;/span&gt;(pii), &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;)&amp;#34;&lt;/span&gt;))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/hydroinformatics/involvement-explore.png&#34; alt=&#34;Responses to Personal Involvement Index by tap water consumers&#34; title=&#34;Responses to Personal Involvement Index by tap water consumers&#34;/&gt;
&lt;figcaption&gt;
Responses to Personal Involvement Index by tap water consumers.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
The next step in the exploratory analysis is to investigate how these factors correlate. The correlation plot below shows that all items strongly correlate with each other. As shown in the boxplots above, the first five and the last five items correlate more strongly. This plot suggests that the two dimensions of the involvement index correlate. The following section shows how to use factor analysis in R to check the significance of these correlation patterns.&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;# Visualise correlation matrix&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  c_matrix &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;cor&lt;/span&gt;(pii[, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;-1&lt;/span&gt;])
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(ggcorrplot)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ggcorrplot&lt;/span&gt;(c_matrix, outline.col &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;white&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;labs&lt;/span&gt;(title &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Personal Inventory Index&amp;#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;         subtitle &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Correlation Matrix&amp;#34;&lt;/span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/hydroinformatics/involvement-correlation.png&#34; alt=&#34;Correlation matrix for the Personal Involvement Index&#34; title=&#34;Correlation matrix for the Personal Involvement Index&#34; width=&#34;600&#34;/&gt;
&lt;figcaption&gt;
Correlation matrix for the Personal Involvement Index.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-6&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-6&#34;&gt;
Factor Analysis in R
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-6&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;Researchers often confuse Factor Analysis with Principal Component Analysis. The outcomes are very similar when applied to the same data set. Both methods are similar but serve different purposes. Principal Component Analysis is a data-reduction technique that reduces the number of variables in a problem. The specific purpose of Factor Analysis is to uncover latent variables. The mathematical principles for both methods are similar but not identical, and they should not be confused.&lt;/p&gt;
&lt;p&gt;
One of the most crucial decisions in factor analysis is to decide how to rotate the factors. There are two types: orthogonal and oblique. In simple terms, orthogonal rotations reduce the correlation between dimensions, whereas oblique rotations allow dimensions to relate to each other. Because of the strong correlations in the correlation plot and the fact that both dimensions measure involvement, this analysis uses oblique rotation. The visualisation below shows how each item and the two dimensions relate to one another.&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(psych)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  pii_fa &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;fa&lt;/span&gt;(pii[, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;-1&lt;/span&gt;], nfactors &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;, rotate &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;oblimin&amp;#34;&lt;/span&gt;, fm &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;ml&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;fa.diagram&lt;/span&gt;(pii_fa, main &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;NULL&lt;/span&gt;, marg &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;8&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/hydroinformatics/involvement-factors.png&#34; alt=&#34;Factor analysis in R with Psych package&#34; title=&#34;Factor analysis in R with Psych package&#34; width=&#34;400&#34;/&gt;
&lt;figcaption&gt;
Factor analysis in R with Psych package.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-7&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-7&#34;&gt;
Consumer Involvement Scores
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-7&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;This analysis suggests that the items for the PII measure an underlying construct, which we can call consumer involvement. To work out the scores for this measure, we can sum the items for each respondent.&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;# Calculating the PII&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  pii_scores &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; pii &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;mutate&lt;/span&gt;(cognitive &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; p01 &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; p02 &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; p03 &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; p04 &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; p05,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;           affective &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; p06 &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; p07 &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; p08 &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; p09 &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; p10) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;select&lt;/span&gt;(customer_id, cognitive, affective) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;pivot_longer&lt;/span&gt;(cols &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt;customer_id)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ggplot&lt;/span&gt;(pii_scores) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;aes&lt;/span&gt;(value) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;geom_histogram&lt;/span&gt;(fill &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;dodgerblue 4&amp;#34;&lt;/span&gt;, binwidth &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;facet_wrap&lt;/span&gt;(&lt;span style=&#34;color:#333&#34;&gt;~&lt;/span&gt;name) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;theme_minimal&lt;/span&gt;(base_size &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;12&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;labs&lt;/span&gt;(title &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Consumer Involvement with Tap Water&amp;#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;         subtitle &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Personal Involvement Index&amp;#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;         x &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Score&amp;#34;&lt;/span&gt;, y &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;NULL&lt;/span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/hydroinformatics/involvement-scores.png&#34; alt=&#34;Personal Involvement Index for tap water&#34; title=&#34;Personal Involvement Index for tap water&#34;/&gt;
&lt;figcaption&gt;
Personal Involvement Index for tap water.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
This simple factor analysis in R shows the basic principle of analysing psychometric data. The psych package has many more specialised tools to dig deeper into the information. This article has not assessed this construct&amp;#39;s validity or evaluated the factors&amp;#39; reliability. That may be for a future article.&lt;/p&gt;
&lt;p&gt;
&lt;a href=&#34;https://lucidmanager.org/data-science/analysing-the-customer-experience/&#34;&gt;Chapter 8&lt;/a&gt; in &lt;em&gt;Data Science for Water Utilities&lt;/em&gt; explains the principles of analysing survey data to understand the customer experience in more detail.&lt;/p&gt;
&lt;p&gt;

  &lt;div class=&#34;box&#34;&gt;
    &lt;div class=&#34;media&#34;&gt;
      &lt;figure class=&#34;media-left&#34;&gt;
        &lt;p class=&#34;image is-128x128&#34;&gt;
          &lt;img src=&#34;https://lucidmanager.org/images/books/2023_ds4wu.jpg&#34; alt = &#34;Data Science for Water Utilities&#34; title = &#34;Data Science for Water Utilities&#34;&gt;
        &lt;/p&gt;
      &lt;/figure&gt;
      &lt;div class=&#34;media-content&#34;&gt;
        &lt;div class=&#34;content&#34;&gt;
          &lt;p class=&#34;is-size-5 has-text-weight-bold&#34;&gt;Data Science for Water Utilities&lt;/p&gt;
          &lt;p class=&#34;mb-2&#34;&gt;Data Science for Water Utilities published by CRC Press is an applied, practical guide that shows water professionals how to use data science to solve urban water management problems using the R language for statistical computing.&lt;/p&gt;
          &lt;div class=&#34;buttons&#34;&gt;
            &lt;a class=&#34;button is-info&#34; href=&#34;https://routledge.com/9781032354545&#34; target=&#34;_blank&#34;&gt;
              &lt;span class=&#34;icon&#34;&gt;
                &lt;i class=&#34;fas fa-shopping-cart&#34;&gt;&lt;/i&gt;
              &lt;/span&gt;
              &lt;span&gt;Routledge&lt;/span&gt;
            &lt;/a&gt;
            &lt;a class=&#34;button is-info&#34; href=&#34;https://www.amazon.com/Data-Science-Water-Utilities-Chapman/dp/1032354550&#34; target=&#34;_blank&#34;&gt;
              &lt;span class=&#34;icon&#34;&gt;
                &lt;i class=&#34;fas fa-shopping-cart&#34;&gt;&lt;/i&gt;
              &lt;/span&gt;
              &lt;span&gt;Amazon&lt;/span&gt;
            &lt;/a&gt;
          &lt;/div&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://www.r-bloggers.com/&#34; target=&#34;_blank&#34; title=&#34;Proudly associated with R-Bloggers&#34;&gt;
  &lt;button class=&#34;button is-link is-medium&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-r-project&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span&gt;As seen on R Bloggers&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;

      </description>
    </item>
    
    
    
    <item>
      <title>5½ Reasons to Ditch Spreadsheets for Data Science</title>
      <link>https://lucidmanager.org/data-science/spreadsheets-for-data-science/</link>
      <pubDate>Tue, 11 Dec 2018 00:00:00 +0000</pubDate>
      <author>Peter Prevos</author>
      <guid>https://lucidmanager.org/data-science/spreadsheets-for-data-science/</guid>
      <description>
	
&lt;p&gt;
When I studied civil engineering some decades ago, we solved all our computing problems by writing code. Writing in BASIC or PASCAL, I could quickly perform fundamental engineering analysis, such as reinforced concrete beams, with my home-brew software library. Soon after I started my career, spreadsheets became widely available, and I fully embraced this fantastic business tool, first Lotus 123 and later grudgingly moved to MS Excel. This article describes why you should ditch spreadsheets for data science and start writing code in R or Python instead. Computer code is Poetry, spreadsheets are chaos.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/data-science/wapening.jpg&#34; alt=&#34;Screendump of Atari BASIC program&#34; title=&#34;Screendump of Atari BASIC program&#34;/&gt;
&lt;figcaption&gt;
Screendump of Atari BASIC program to estimate concrete reinforcement surface area.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
Spreadsheets were excellent in those early days because data, code, visualisations and tabular output are all stored in one convenient file. Creating graphs with computer code was a bit of a nightmare in those days, so spreadsheets were a minor miracle. The next twenty years, I must have created thousands of spreadsheets of varying complexity. I even developed a ‘jungle&amp;#39; of interlinked spreadsheets to manage progress reporting.&lt;/p&gt;
&lt;p&gt;
In the pioneering days of spreadsheets, they provided enormous convenience for engineers and other professionals to quickly develop analytical tools. But after using this tool for a few years, cracks started to appear.&lt;/p&gt;
&lt;figure&gt;
&lt;div style=&#34;position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden;&#34;&gt;
      &lt;iframe allow=&#34;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share; fullscreen&#34; loading=&#34;eager&#34; referrerpolicy=&#34;strict-origin-when-cross-origin&#34; src=&#34;https://www.youtube.com/embed/YDvbDiJZpy0?autoplay=0&amp;amp;controls=1&amp;amp;end=0&amp;amp;loop=0&amp;amp;mute=0&amp;amp;start=0&#34; style=&#34;position: absolute; top: 0; left: 0; width: 100%; height: 100%; border:0;&#34; title=&#34;YouTube video&#34;&gt;&lt;/iframe&gt;
    &lt;/div&gt;

&lt;figcaption&gt;
Meet the inventor of the spreadsheet | Dan Bricklin
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;div id=&#34;outline-container-headline-1&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-1&#34;&gt;
Spreadsheets for Data Science are Chaos
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-1&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;Throughout my career, I had many nightmarish experiences trying to reverse engineer spreadsheets, even the ones I wrote myself. The combination of data, code and output that I loved at the start of my career had reached its limits. Spreadsheets use incomprehensible names for variables (&lt;code class=&#34;verbatim&#34;&gt;AZ346&lt;/code&gt;, &lt;code class=&#34;verbatim&#34;&gt;XC89&lt;/code&gt; and so on) and the formulas are impossible to read because all code is cramped on one line with deeply nested logic. The multiple parentheses make Excel formulas are even harder to read than LISP expressions.&lt;/p&gt;
&lt;p&gt;
Furthermore, spreadsheets hide the formulas behind the results, which renders spreadsheets notoriously hard to understand. My love affair with the spreadsheet came to an end when I started writing my dissertation about customer service for water utilities. Excel was incapable of helping my need for solving complex mathematical problems. A colleague suggested I look into this new thing called &amp;#39;Data Science&amp;#39; and this advice changed my career.&lt;/p&gt;
&lt;p&gt;
My focus is to implement &lt;a href=&#34;https://lucidmanager.org/data-science/strategic-data-science/&#34;&gt;strategic data science&lt;/a&gt; to help organisations to create value from data. One of the ways to achieve this goal is to ditch the spreadsheet and start writing code instead.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/data-science/reverse-engineer-spreadsheet.jpg&#34; alt=&#34;Example of a reverse-engineering a spreadsheet&#34; title=&#34;Example of a reverse-engineering a spreadsheet&#34;/&gt;
&lt;figcaption&gt;
Example of a reverse-engineering a spreadsheet.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-2&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-2&#34;&gt;
Code is Poetry
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-2&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;I decided to learn how to write code in the R language for statistical analysis. The R language is like a Swiss army chainsaw for engineers with capabilities that far exceed anything a spreadsheet can do. Writing in code, such as R or Python, is like writing an instruction manual on how to analyse data. Anyone who understands the language will be able to know how you derived your conclusions. Modern data science languages can generate print-quality visualisations and can output results in many formats, including a spreadsheet. In my job as a data science manager
for a water utility, I use R code for everything.&lt;/p&gt;
&lt;p&gt;
The awesome power of being able to easily combine large data sets, visualise data and undertake complex analysis. Now that I have rediscovered the poetry of writing computer code, I advocate learning to use R or perhaps Python and ditch the spreadsheet. On my this website, I share examples of creating value and having fun with the R language. Computer code is poetry in the sense that you can read it like a poem. Computer code consists of a sequential logic that is easy to follow. A spreadsheet is a chaotic jumble of formulas in a geometrical pattern. Each spreadsheet has a different pattern. The only purpose I still have for spreadsheets is as an interface for small data sets.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-3&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-3&#34;&gt;
5½ Reasons to Ditch the Spreadsheet
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-3&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;If you are still using spreadsheets, or you are trying to convince a colleague to ditch this tool, here are 5½ reasons to start using code to analyse data:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Good analysis is reproducible and can be peer-reviewed. Spreadsheets are hard to understand because of non-sequential references. Computer code is like an instruction book that can be read step-by-step.&lt;/li&gt;
&lt;li&gt;Spreadsheet variables are hard to understand (e.g. &lt;code class=&#34;verbatim&#34;&gt;ZX81:ZX99&lt;/code&gt;). In computer code, you give them meaningful names (e.g. &lt;code class=&#34;verbatim&#34;&gt;sales[81:99]&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;Best practice in data management is to separate data, code and output. In spreadsheets, it is not immediately clear which cell is the result of another cell and which ones are raw data. Computer code separates the data from the code and the output.&lt;/li&gt;
&lt;li&gt;You can only share spreadsheet output with people who have access to the relevant software package. Computer code can produce output in multiple formats, such as HTML, PD or even Excel, including &lt;a href=&#34;https://shiny.rstudio.com&#34;&gt;interactive dashboards&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Functionality in spreadsheets is limited to what is made available by Microsoft. The R and Python languages are extendable and have extensive libraries to solve complex problems.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The bonus reason to ditch the spreadsheet is that the best data science software, such as R and Python, is Open Source and freely available on the web. No license fees and it comes with terrific community support. &lt;/p&gt;
&lt;p&gt;
Feel free to leave a comment if you like to defend the spreadsheet. If you have additional reasons to ditch this venerable but largely obsolete tool. Subscribe to this monthly blog if you are interested in using the R language for practical data science and some fun.&lt;/p&gt;
&lt;p&gt;

  &lt;div class=&#34;box&#34;&gt;
    &lt;div class=&#34;media&#34;&gt;
      &lt;figure class=&#34;media-left&#34;&gt;
        &lt;p class=&#34;image is-128x128&#34;&gt;
          &lt;img src=&#34;https://lucidmanager.org/images/books/2019_sds.jpg&#34; alt = &#34;Principles of Strategic Data Science&#34; title = &#34;Principles of Strategic Data Science&#34;&gt;
        &lt;/p&gt;
      &lt;/figure&gt;
      &lt;div class=&#34;media-content&#34;&gt;
        &lt;div class=&#34;content&#34;&gt;
          &lt;p class=&#34;is-size-5 has-text-weight-bold&#34;&gt;Principles of Strategic Data Science&lt;/p&gt;
          &lt;p class=&#34;mb-2&#34;&gt;Principles of Strategic Data Science helps you join the dots between mathematics, programming, and business analysis. With a unique approach that bridges the gap between mathematics and computer science, this book takes you through the entire data science pipeline.&lt;/p&gt;
          &lt;div class=&#34;buttons&#34;&gt;
            &lt;a class=&#34;button is-info&#34; href=&#34;https://www.packtpub.com/en-us/product/principles-of-strategic-data-science-9781838985509&#34; target=&#34;_blank&#34;&gt;
              &lt;span class=&#34;icon&#34;&gt;
                &lt;i class=&#34;fas fa-shopping-cart&#34;&gt;&lt;/i&gt;
              &lt;/span&gt;
              &lt;span&gt;Packt Publishing&lt;/span&gt;
            &lt;/a&gt;
          &lt;/div&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;

      </description>
    </item>
    
    
    
    <item>
      <title>Discourse Network Analysis: Undertaking Literature Reviews in R</title>
      <link>https://lucidmanager.org/data-science/discourse-network-analysis-literature-review/</link>
      <pubDate>Thu, 15 Nov 2018 00:00:00 +0000</pubDate>
      <author>Peter Prevos</author>
      <guid>https://lucidmanager.org/data-science/discourse-network-analysis-literature-review/</guid>
      <description>
	
&lt;p&gt;
Literature reviews are the cornerstone of science. Keeping abreast of developments within any given field of enquiry has become increasingly difficult given the enormous amounts of new research. Databases and search technology have made finding relevant literature easy but, keeping a coherent overview of the discourse within a field of enquiry is an ever more encompassing task.&lt;/p&gt;
&lt;p&gt;
Scholars have proposed many approaches to analysing literature, which can be placed along a continuum from traditional narrative methods to systematic analytic syntheses of text using machine learning. Traditional reviews are biased because they rely entirely on the interpretation of the researcher. Analytical approaches follow a process
that is more like scientific experimentation. These systematic methods are reproducible in the way literature is searched and collated but still rely on subjective interpretation.&lt;/p&gt;
&lt;p&gt;
Machine learning provides new methods to analyse large swaths of text. Although these methods sound exciting, these methods are incapable of providing insight. Machine learning cannot interpret a text; it can only summarise and structure a corpus. Machine learning still requires human interpretation to make sense of the information.&lt;/p&gt;
&lt;p&gt;
This article introduces a mixed-method technique for reviewing literature, combining qualitative and quantitative methods. I used this method to analyse literature published by the International Water Association as part of my dissertation into &lt;a href=&#34;https://lucidmanager.org/tags/invisible-water-utility/&#34;&gt;water utility marketing&lt;/a&gt;. Detailed information about the methodology is available through &lt;a href=&#34;https://doi.org/10.26181/5bd90ca36da85&#34;&gt;FigShare&lt;/a&gt;.&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-1&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-1&#34;&gt;
A literature review with RQDA
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-1&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The purpose of this review was to ascertain the relevance of marketing theory to the discourse of literature in water management. This analysis uses a sample of 244 journal abstracts, each of which was coded with the &lt;a href=&#34;http://rqda.r-forge.r-project.org/&#34;&gt;RQDA&lt;/a&gt; library. This library provides functionality for &lt;a href=&#34;https://lucidmanager.org/data-science/qualitative-data-science/&#34;&gt;qualitative data analysis&lt;/a&gt;. RQDA provides a graphical user interface to mark sections of text and assign them to a code, as shown below.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/digital-humanities/rqda_abstract.png&#34; alt=&#34;Marking topics in an abstract with RQDA&#34; title=&#34;Marking topics in an abstract with RQDA&#34;/&gt;
&lt;figcaption&gt;
Marking topics in an abstract with RQDA.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
You can load a corpus of text into RQDA and mark each of the texts with a series of codes. The texts and the codes are stored in an SQLite database, which can be easily queried for further analysis.&lt;/p&gt;
&lt;p&gt;
I used a marketing dictionary to assess the abstracts from journals published by the International Water Association from the perspective of marketing. This phase resulted in a database with 244 abstracts and their associated coding.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-2&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-2&#34;&gt;
Discourse Network Analysis
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-2&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;Once all abstracts are coded, we can start analysing the internal structure of the IWA literature. First, let&amp;#39;s have a look at the occurrence of the topics identified for the corpus of abstracts.&lt;/p&gt;
&lt;p&gt;
The first lines in this snippet call the tidyverse and RQDA libraries and open the abstracts database. The &lt;code class=&#34;verbatim&#34;&gt;getCodingTable&lt;/code&gt; function provides a data frame with each of the marked topics and their location. This function allows us to visualise the occurrence of the topics in the literature.&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(tidyverse)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(RQDA)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#888&#34;&gt;## Open project&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;openProject&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;IWA_Abstracts.rqda&amp;#34;&lt;/span&gt;, updateGUI &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;TRUE&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#888&#34;&gt;## Visualise codes&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;getCodingTable&lt;/span&gt;() &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;group_by&lt;/span&gt;(codename) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;count&lt;/span&gt;() &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;arrange&lt;/span&gt;(n) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ungroup&lt;/span&gt;() &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;mutate&lt;/span&gt;(codename &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;factor&lt;/span&gt;(codename, levels &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; codename)) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ggplot&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;aes&lt;/span&gt;(codename, n)) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;geom_col&lt;/span&gt;() &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;coord_flip&lt;/span&gt;() &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;xlab&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Code name&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ylab&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Occurence&amp;#34;&lt;/span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/digital-humanities/code_frequencies.png&#34; alt=&#34;Frequencies of topics in IWA literature&#34; title=&#34;Frequencies of topics in IWA literature&#34;/&gt;
&lt;figcaption&gt;
Frequencies of topics in IWA literature.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
This bar chart tells us that the literature is preoccupied with asset management and the quality of the product (water) or the service (customer perception). This insight is interesting, but not very enlightening information. We can use discourse network analysis to find a deeper structure in the literature.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-3&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-3&#34;&gt;
Discourse Network Structure
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-3&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;We can view each abstract with two or more topics as a network where each topic is connected. The example below shows four abstracts with two or more codes and their internal networks.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/digital-humanities/complete-graphs.png&#34; alt=&#34;Examples of complete networks for four abstracts&#34; title=&#34;Examples of complete networks for four abstracts&#34;/&gt;
&lt;figcaption&gt;
Examples of complete networks for four abstracts.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
The union of these four networks forms a more extensive network that allows us to analyse the structure of the corpus of literature, shown below.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/digital-humanities/mix_example.communities.png&#34; alt=&#34;Union of networks and community detection&#34; title=&#34;Union of networks and community detection&#34;/&gt;
&lt;figcaption&gt;
Union of networks and community detection.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
We can create a network of topics with the &lt;a href=&#34;https://igraph.org/r/&#34;&gt;igraph package&lt;/a&gt;. The first step is to create a &lt;a href=&#34;https://en.wikipedia.org/wiki/Document-term_matrix&#34;&gt;Document-Term-Matrix&lt;/a&gt;. This matrix counts how often a topic occurs within each abstract. From this matrix, we can create a graph by transforming it into an &lt;a href=&#34;https://en.wikipedia.org/wiki/Adjacency_matrix&#34;&gt;Adjacency Matrix&lt;/a&gt;. This matrix describes the graph which can be visualised and analysed. For more detailed information about this method, refer to &lt;a href=&#34;http://hdl.handle.net/1959.9/561679&#34;&gt;my dissertation&lt;/a&gt;.&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(igraph)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(reshape2)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;dtm &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;getCodingTable&lt;/span&gt;()[,&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;5&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;4&lt;/span&gt;)] &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;mutate&lt;/span&gt;(freq &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;acast&lt;/span&gt;(filename &lt;span style=&#34;color:#333&#34;&gt;~&lt;/span&gt; codename, sum)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;adj &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;crossprod&lt;/span&gt;(dtm)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;g &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;graph.adjacency&lt;/span&gt;(adj, weighted &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#007020&#34;&gt;T&lt;/span&gt;, mode &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;undirected&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;g &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;simplify&lt;/span&gt;(g)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#888&#34;&gt;## Network Graphs&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;V&lt;/span&gt;(g)&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;name &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;gsub&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34; &amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;\n&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;V&lt;/span&gt;(g)&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;name)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;par&lt;/span&gt;(mar &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;rep&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;4&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;plot&lt;/span&gt;(g,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;     layout &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; layout.fruchterman.reingold,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;     vertex.label.cex &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;     vertex.size &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;degree&lt;/span&gt;(g),
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;     vertex.label.color &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;black&amp;#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;     vertex.frame.color &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;white&amp;#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;     vertex.color &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Dodgerblue&amp;#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;     edge.width &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;E&lt;/span&gt;(g)&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;weight &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;     edge.color &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;darkgray&amp;#34;&lt;/span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/digital-humanities/iwa_network.jpg&#34; alt=&#34;/images/digital-humanities/iwa_network.jpg&#34; title=&#34;The network of topics in IWA literature&#34;/&gt;
&lt;figcaption&gt;
The network of topics in IWA literature.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
In this graph, each node is a topic in the literature, and each edge implies that a topic is used in the same abstract. This graph uses the Fruchterman-Reingold algorithm to position each of the nodes, with the most connected topic in the centre.&lt;/p&gt;
&lt;p&gt;
The last step is to identify the structure of this graph using community detection. A community is a group of nodes that are more connected with each other than with nodes outside the community.&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;set.seed&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;123&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;comms &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;spinglass.community&lt;/span&gt;(g, spins &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;100&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;par&lt;/span&gt;(mar &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;rep&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;4&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;plot&lt;/span&gt;(comms, g,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;     layout &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; layout.fruchterman.reingold,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;     vertex.label.cex &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;.7&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;     vertex.size &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;degree&lt;/span&gt;(g),
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;     vertex.label.color &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;black&amp;#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;     vertex.frame.color &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;NA&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;     edge.color &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;black&amp;#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;     vertex.label.family &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;sanserif&amp;#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;     mark.border &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;NA&lt;/span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/digital-humanities/iwa_communities.jpg&#34; alt=&#34;Community detection in IWA literature&#34; title=&#34;Community detection in IWA literature&#34;/&gt;
&lt;figcaption&gt;
Community detection in IWA literature.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
We have now succeeded to convert a corpus of 244 journal abstracts to a parsimonious overview of four communities of topics. This analysis resulted in greater insight into how marketing theory applies to water management, which was used to structure a book about water utility marketing.&lt;/p&gt;
&lt;p&gt;

  &lt;div class=&#34;box&#34;&gt;
    &lt;div class=&#34;media&#34;&gt;
      &lt;figure class=&#34;media-left&#34;&gt;
        &lt;p class=&#34;image is-128x128&#34;&gt;
          &lt;img src=&#34;https://lucidmanager.org/images/books/2017b_wum.jpg&#34; alt = &#34;Customer Experience Management for Water Utilities: Marketing Urban Water Supply&#34; title = &#34;Customer Experience Management for Water Utilities: Marketing Urban Water Supply&#34;&gt;
        &lt;/p&gt;
      &lt;/figure&gt;
      &lt;div class=&#34;media-content&#34;&gt;
        &lt;div class=&#34;content&#34;&gt;
          &lt;p class=&#34;is-size-5 has-text-weight-bold&#34;&gt;Customer Experience Management for Water Utilities: Marketing Urban Water Supply&lt;/p&gt;
          &lt;p class=&#34;mb-2&#34;&gt;Practical framework for water utilities to become more focused on their customers following Service-Dominant Logic.&lt;/p&gt;
          &lt;div class=&#34;buttons&#34;&gt;
            &lt;a class=&#34;button is-info&#34; href=&#34;https://iwaponline.com/ebooks/book/327/Customer-Experience-Management-for-Water&#34; target=&#34;_blank&#34;&gt;
              &lt;span class=&#34;icon&#34;&gt;
                &lt;i class=&#34;fas fa-shopping-cart&#34;&gt;&lt;/i&gt;
              &lt;/span&gt;
              &lt;span&gt;IWA Publishing&lt;/span&gt;
            &lt;/a&gt;
          &lt;/div&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;

&lt;a href = &#34;https://www.r-bloggers.com/&#34; target=&#34;_blank&#34; title=&#34;Proudly associated with R-Bloggers&#34;&gt;
  &lt;button class=&#34;button is-link is-medium&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-r-project&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span&gt;As seen on R Bloggers&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;

      </description>
    </item>
    
    
    
    <item>
      <title>Celebrate Halloween with Creepy Computer Games in R</title>
      <link>https://lucidmanager.org/data-science/creepy-computer-games-in-r/</link>
      <pubDate>Sun, 28 Oct 2018 00:00:00 +0000</pubDate>
      <author>Peter Prevos</author>
      <guid>https://lucidmanager.org/data-science/creepy-computer-games-in-r/</guid>
      <description>
	
&lt;p&gt;
Halloween is upon us once more and who ever said that data science can&amp;#39;t be scare. This article translates the &lt;em&gt;Gravedigger&lt;/em&gt; game from the 1983 &lt;em&gt;Creepy Computer Games&lt;/em&gt; book to celebrate Halloween. This article is part of my series on &lt;a href=&#34;https://lucidmanager.org/tags/r-games/&#34;&gt;gaming with the R language&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;
In the 1980s I spent my time writing code on my 8-bit ZX81 and Atari computers. I learnt everything I know about programming from copying and modifying printed code listings from books with computer games. The games in these books are mostly simple text-based games. The authors gave them enticing names, often imaginatively illustrated to visualise the virtual world they represent. A &lt;a href=&#34;https://lucidmanager.org/data-science/pong/&#34;&gt;line and a dot&lt;/a&gt; become a game of tennis, and a computer that was able to play &lt;a href=&#34;https://lucidmanager.org/data-science/tic-tac-toe-war-games/&#34;&gt;Tic Tac Toe&lt;/a&gt; seemed like your machine had come alive.&lt;/p&gt;
&lt;p&gt;
Let&amp;#39;s step into the creepy world of computer games as imagined by Usborne Publishing.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/rgames/creepy-computer-games.jpg&#34; alt=&#34;Creepy Computer Games&#34; title=&#34;Creepy Computer Games&#34;/&gt;
&lt;figcaption&gt;
Reynold, Colin and McCaig, Rob, Creepy Computer Games (Usborne, London).
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;div id=&#34;outline-container-headline-1&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-1&#34;&gt;
Creepy Computer Games - Gravedigger
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-1&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;Alan Ramsey&amp;#39;s &lt;em&gt;Gravedigger&lt;/em&gt; is a typical example of the games of the early days of home computing. You can download the original book for free from the &lt;a href=&#34;https://drive.google.com/file/d/0Bxv0SsvibDMTRUl3SFRONGN0MFk/view&#34;&gt;publisher&amp;#39;s Google drive&lt;/a&gt;. The &lt;em&gt;Gravedigger&lt;/em&gt; listing starts on page 10. The lyrical description of the game provides the instructions:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;It&amp;#39;s dark and windy—not the kind of night to be lost in a graveyard, but that&amp;#39;s where you are. You have until midnight to find your way out. Skeletons lurk in the shadows waiting to scare you to death should you come to close. You can dig holes to help keep them away but digging is tiring work and you cannot manage more than five in one game. You have to be careful not to fall down the holes you have dug. Grave stones (marked &lt;code class=&#34;verbatim&#34;&gt;+&lt;/code&gt;) and  the walls of the graveyard (marked &lt;code class=&#34;verbatim&#34;&gt;:&lt;/code&gt;) block your way. The holes you digs are marked &lt;code class=&#34;verbatim&#34;&gt;O&lt;/code&gt;, you are &lt;code class=&#34;verbatim&#34;&gt;*&lt;/code&gt; and the skeletons are &lt;code class=&#34;verbatim&#34;&gt;X&lt;/code&gt;. See if you can escape.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/rgames/gravedigger.png&#34; alt=&#34;/images/rgames/gravedigger.png&#34; title=&#34;/images/rgames/gravedigger.png&#34; /&gt;&lt;figcaption&gt;
Gravedigger screenshot (Emacs ESS).
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;div id=&#34;outline-container-headline-2&#34; class=&#34;outline-3&#34;&gt;
&lt;h3 id=&#34;headline-2&#34;&gt;
Creepy Alien
&lt;/h3&gt;
&lt;div id=&#34;outline-text-headline-2&#34; class=&#34;outline-text-3&#34;&gt;
&lt;p&gt;Roland Krasser has developed vastly improved version of the code and published the &lt;a href=&#34;https://cran.r-project.org/web/packages/creepyalien/index.html&#34;&gt;Creepy Alien game on CRAN&lt;/a&gt;.&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;install.packages&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;creepyalien&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  creepyalien&lt;span style=&#34;color:#333&#34;&gt;::&lt;/span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;creepyalien&lt;/span&gt;()&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/rgames/creepy-alien.png&#34; alt=&#34;/images/rgames/creepy-alien.png&#34; title=&#34;/images/rgames/creepy-alien.png&#34; /&gt;&lt;figcaption&gt;
Creepy Alien by Roland Kasser.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-3&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-3&#34;&gt;
The Gravedigger code
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-3&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;I translated the BASIC code as close to the original as possible. I changed some of the variable names because, in BASIC, single variables and vectors can have the same name and names of character vectors end in a &lt;code class=&#34;verbatim&#34;&gt;$&lt;/code&gt; character.&lt;/p&gt;
&lt;p&gt;
The game is quite tricky, and I have only managed to escape the graveyard once. It looks like the likelihood of success very much depends on the random distribution of the graves. Perhaps we need some machine learning to optimise strategy.&lt;/p&gt;
&lt;p&gt;
I leave it up to you to deconstruct the program and safely work your way through the graveyard.&lt;/p&gt;
&lt;p&gt;
Happy Halloween!&lt;/p&gt;
&lt;figure&gt;
&lt;p&gt;indicate different versions for various dialects of the BASIC language.
&lt;img src=&#34;https://lucidmanager.org/images/rgames/graveyard.jpg&#34; alt=&#34;/images/rgames/graveyard.jpg&#34; title=&#34;/images/rgames/graveyard.jpg&#34; /&gt;&lt;/p&gt;
&lt;figcaption&gt;
Partial page of the Gravedigger game in BASIC. The markings
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
&lt;a href = &#34;https://github.com/pprevos/RGames/&#34; target=&#34;_blank&#34;
   title=&#34;Download RGames from GitHub&#34;
   alt=&#34;Download RGames from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;RGames&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#888&#34;&gt;## Creepy Computer Games&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#888&#34;&gt;## Reynold, Colin and McCaig, Rob, Creepy Computer Games (Usborne, London).&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#888&#34;&gt;## https://archive.org/details/Creepy_Computer_Games_1983_Usborne_Publishing/&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#888&#34;&gt;## Gravedigger by Alan Ramsey&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#888&#34;&gt;## Initiate board&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;A &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;matrix&lt;/span&gt;(ncol &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;20&lt;/span&gt;, nrow &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;A[, ] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34; &amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#888&#34;&gt;## Starting variables&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;W &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt; &lt;span style=&#34;color:#888&#34;&gt;# Move number&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;X &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;5&lt;/span&gt; &lt;span style=&#34;color:#888&#34;&gt;# Remaining holes&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;death &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt; &lt;span style=&#34;color:#888&#34;&gt;# Game over?&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#888&#34;&gt;## Initiate pieces&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;Y &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;*&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;B &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;+&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;C &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;O&amp;#34;&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;D &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;:&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;E &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;X&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;Z &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34; &amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#888&#34;&gt;## Draw board&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#888&#34;&gt;## Add borders&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;A&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;[c&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt;), ] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; D
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;A[, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; D
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;A[1&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;8&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;20&lt;/span&gt;] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; D
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#888&#34;&gt;## Add graves&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;for&lt;/span&gt; (i &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;in&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;20&lt;/span&gt;){
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    A&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;[floor&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;runif&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;7&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;), &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;floor&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;runif&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;15&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;3&lt;/span&gt;)] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; B
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#888&#34;&gt;## Starting positions&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#888&#34;&gt;## Player&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;M &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;N &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;A[N, M] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; Y
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#888&#34;&gt;## Skeletons&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;S &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;4&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;19&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;3&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;19&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;19&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#888&#34;&gt;## Game play&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;repeat&lt;/span&gt;{    
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#888&#34;&gt;## Position skeletons&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;for&lt;/span&gt; (J &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;in&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;seq&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;5&lt;/span&gt;, by &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;)) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        A[S[J], S[J &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;]] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; E
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    W &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; W &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt; &lt;span style=&#34;color:#888&#34;&gt;## Move counter&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (W &lt;span style=&#34;color:#333&#34;&gt;&amp;gt;&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;60&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;print&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;The clock&amp;#39;s struck midnight&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;print&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Aghhhhh!!!!&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;break&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#888&#34;&gt;## Print board&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    v &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;paste&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;as.vector&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;t&lt;/span&gt;(A)), collapse &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;&amp;#34;&lt;/span&gt;)        
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;for&lt;/span&gt; (i &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;in&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;print&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;substr&lt;/span&gt;(v, (i &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;20&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;, (i &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;20&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;20&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#888&#34;&gt;## Enter move&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    A1 &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;toupper&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;readline&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;paste0&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Enter move &amp;#34;&lt;/span&gt;, W, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34; (You can go N, S, E or W): &amp;#34;&lt;/span&gt;)))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#888&#34;&gt;## Move player&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#007020&#34;&gt;T&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; N
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    U &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; M
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (A1 &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;N&amp;#34;&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#007020&#34;&gt;T&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; N &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    } &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;else&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (A1 &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;E&amp;#34;&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        U &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; M &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    } &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;else&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (A1 &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;S&amp;#34;&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#007020&#34;&gt;T&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; N &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    } &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;else&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (A1 &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;W&amp;#34;&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        U &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; M &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#888&#34;&gt;## Collision detection&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (A[T, U] &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; D &lt;span style=&#34;color:#333&#34;&gt;|&lt;/span&gt; A[T, U] &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; B) { &lt;span style=&#34;color:#888&#34;&gt;# Edge or grave&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;print&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;That way&amp;#39;s blocked&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    } &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;else&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (A[T, U] &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; C) { &lt;span style=&#34;color:#888&#34;&gt;# Hole&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;print&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;You&amp;#39;ve fallen into one of your own holes&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;break&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    } &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;else&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (A[T, U] &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; E) { &lt;span style=&#34;color:#888&#34;&gt;# Skeleton&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        death &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    } &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;else&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (&lt;span style=&#34;color:#007020&#34;&gt;T&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;9&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;&amp;amp;&lt;/span&gt; U &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;20&lt;/span&gt;) { &lt;span style=&#34;color:#888&#34;&gt;# Escaped&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;print&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;You&amp;#39;re free!&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;print&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;paste0&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Your performance rating is &amp;#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;floor&lt;/span&gt;((&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;60&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; W) &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;60&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; (&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;96&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; X)), &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;%&amp;#34;&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;break&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    } &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;else&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (death &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;print&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Urk! You&amp;#39;ve been scared to death by a skeleton.&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;break&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    } &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;else&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (A[T, U] &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; Z) { &lt;span style=&#34;color:#888&#34;&gt;# Player can move&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#888&#34;&gt;## Move player and dig hole&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        A [N, M] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; Z
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (X &lt;span style=&#34;color:#333&#34;&gt;!=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            B1 &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;toupper&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;readline&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Would you like to dig a hole (Y or N): &amp;#34;&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (B1 &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Y&amp;#34;&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                X &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; X &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                A[N, M] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; C
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        N &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#007020&#34;&gt;T&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        M &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; U
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        A[T, U] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; Y
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#888&#34;&gt;## Move skeletons&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;for&lt;/span&gt; (J &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;in&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;seq&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;5&lt;/span&gt;, by &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;)) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            &lt;span style=&#34;color:#888&#34;&gt;## Store skeleton position in temp variable&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            P &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; S[J]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            Q &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; S[J &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;any&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(A[P &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;, Q], A[P &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt; , Q], A[P, Q &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;], A[P, Q &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;]) &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; Y)) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                death &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            } &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;else&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                &lt;span style=&#34;color:#888&#34;&gt;## Move skeletons&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (A1 &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;S&amp;#34;&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;&amp;amp;&lt;/span&gt; A[P &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;, Q] &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; Z){
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                    S[J] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; S[J] &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt; &lt;span style=&#34;color:#888&#34;&gt;# Follow player&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                    A[P, Q] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; Z
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                } &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;else&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (A1 &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;N&amp;#34;&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;&amp;amp;&lt;/span&gt; A[P &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;, Q] &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; Z){
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                    S[J] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; S[J] &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt; &lt;span style=&#34;color:#888&#34;&gt;# Follow player&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                    A[P, Q] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; Z
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                } &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;else&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (A1 &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;E&amp;#34;&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;&amp;amp;&lt;/span&gt; A[P, Q &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;] &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; Z &lt;span style=&#34;color:#333&#34;&gt;&amp;amp;&lt;/span&gt; M &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;&lt;/span&gt; Q){
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                    S[J &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; S[J &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;] &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt; &lt;span style=&#34;color:#888&#34;&gt;# Move towards player&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                    A[P, Q] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; Z
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                } &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;else&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (A1 &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;E&amp;#34;&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;&amp;amp;&lt;/span&gt; A[P, Q &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;] &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; Z &lt;span style=&#34;color:#333&#34;&gt;&amp;amp;&lt;/span&gt; M &lt;span style=&#34;color:#333&#34;&gt;&amp;gt;&lt;/span&gt; Q) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                    S[J &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; S[J &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;] &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt; &lt;span style=&#34;color:#888&#34;&gt;# Reverse direction&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                    A[P, Q] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; Z
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
&lt;a href = &#34;https://www.r-bloggers.com/&#34; target=&#34;_blank&#34; title=&#34;Proudly associated with R-Bloggers&#34;&gt;
  &lt;button class=&#34;button is-link is-medium&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-r-project&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span&gt;As seen on R Bloggers&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;

      </description>
    </item>
    
    
    
    <item>
      <title>Mapping and Geocoding with ggmap and the Google API</title>
      <link>https://lucidmanager.org/data-science/geocoding-with-ggmap/</link>
      <pubDate>Wed, 10 Oct 2018 00:00:00 +0000</pubDate>
      <author>Peter Prevos</author>
      <guid>https://lucidmanager.org/data-science/geocoding-with-ggmap/</guid>
      <description>
	
&lt;p&gt;
In the old days, obtaining latitude and longitude required a physical survey and lots of mathematics. Google Maps has made this task much easier. This article describes how to use the Google Maps API with the &lt;a href=&#34;https://cran.r-project.org/web/packages/ggmap/index.html&#34;&gt;ggmap library&lt;/a&gt;. Geocoding with ggmap is a convenient way to create meaningful spatial visualisations.&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-1&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-1&#34;&gt;
The ggmap package
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-1&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;Several R libraries exist to visualise spatial information. ggmap by David Kahle which aligns with the ggplot philosophy of layered visualisations.&lt;/p&gt;
&lt;p&gt;
The geocode function from the ggmap package extracts longitude and latitude from Google maps, based on a location query. The example below shows the result of a simple geocoding call for the White House and Uluru. The geocode function essentially constructs a URL to obtain the data. However, when you run this code you see an error:&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(ggmap)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;geocode&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;White House&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Uluru&amp;#34;&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  Error&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt; Google now requires an API key.
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;         See &lt;span style=&#34;color:#333&#34;&gt;?&lt;/span&gt;register_google &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;for&lt;/span&gt; details.&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
In the middle of 2018, Google tightened access to the database, which means you need to register an API for it to work. This article explains how to use the latest version of ggmap and a Google account to continue using this function.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-2&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-2&#34;&gt;
The Google API
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-2&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;Before we can start geocoding, we need to obtain an &lt;a href=&#34;https://developers.google.com/maps/documentation/geocoding/get-api-key&#34;&gt;API key from Google&lt;/a&gt;. Go to the &lt;a href=&#34;https://cloud.google.com/maps-platform/#get-started&#34;&gt;registration page&lt;/a&gt;, and follow the instructions (select all mapping options). The geocoding API is a free service, but you nevertheless need to associate a credit card with the account. Please note that the Google Maps API is &lt;a href=&#34;https://developers.google.com/maps/documentation/geocoding/usage-and-billing&#34;&gt;not a free service&lt;/a&gt;. There is a free allowance of 40,000 calls to the geocoding API per month, and beyond that calls are $0.005 each.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-3&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-3&#34;&gt;
Geocoding with ggmap
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-3&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;You will need to ensure that you have the latest version of ggmap installed on your system. The current version on CRAN is 3.0.&lt;/p&gt;
&lt;p&gt;
The code snippet below shows a minimum-working-example of how you can map coordinates using ggplot. The &lt;code class=&#34;verbatim&#34;&gt;register_google&lt;/code&gt; function stores the API key. I have stored the key itself in a private text file. The &lt;code class=&#34;verbatim&#34;&gt;getOption(&amp;#34;ggmap&amp;#34;)&lt;/code&gt; function summarises the Google credentials to check how you are connected.&lt;/p&gt;
&lt;p&gt;
The geocode function converts the request into a URL and captures the output into a data frame. The plot shows the places I have lived, projected orthogonally on the globe. The mapproj library provides the orthogonal projection.&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://github.com/pprevos/geography/&#34; target=&#34;_blank&#34;
   title=&#34;Download geography from GitHub&#34;
   alt=&#34;Download geography from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;geography&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;api &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;readLines&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;google.api&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#888&#34;&gt;# Text file with the API key&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;register_google&lt;/span&gt;(key &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; api)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;getOption&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;ggmap&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;locations &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Hoensbroek&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Johannesburg&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Barrow-in-Furness&amp;#34;&lt;/span&gt;,                
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;               &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Hong Kong&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Singapore&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Tangail&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Maastricht&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Bendigo&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;     &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;geocode&lt;/span&gt;()
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(ggplot2)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(mapproj)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;world &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;map_data&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;world&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ggplot&lt;/span&gt;() &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;     &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;geom_polygon&lt;/span&gt;(data &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; world,  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;aes&lt;/span&gt;(long, lat, group &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; group),
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                  fill &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;grey&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;     &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;geom_point&lt;/span&gt;(data &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; locations, &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;aes&lt;/span&gt;(lon, lat),
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                colour &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;red&amp;#34;&lt;/span&gt;, size &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;5&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;     &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;coord_map&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;ortho&amp;#34;&lt;/span&gt;, orientation &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;30&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;80&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;)) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;     &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;theme_void&lt;/span&gt;()&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;figure&gt;
&lt;p alt=&#34;Places where I have worked and lived&#34; title=&#34;Places where I have worked and lived.&#34;&gt;&lt;img src=&#34;https://lucidmanager.org/images/geography/worldhomes.png&#34; alt=&#34;/images/geography/worldhomes.png&#34; title=&#34;/images/geography/worldhomes.png&#34;/&gt;
&lt;a href = &#34;https://www.r-bloggers.com/&#34; target=&#34;_blank&#34; title=&#34;Proudly associated with R-Bloggers&#34;&gt;
  &lt;button class=&#34;button is-link is-medium&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-r-project&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span&gt;As seen on R Bloggers&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;
&lt;/p&gt;
&lt;figcaption&gt;
Places where I have worked and lived.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;/div&gt;
&lt;/div&gt;

      </description>
    </item>
    
    
    
    <item>
      <title>Flat Earth Mathematics with examples in the R Language</title>
      <link>https://lucidmanager.org/data-science/flat-earth-mathematics/</link>
      <pubDate>Sun, 02 Sep 2018 00:00:00 +0000</pubDate>
      <author>Peter Prevos</author>
      <guid>https://lucidmanager.org/data-science/flat-earth-mathematics/</guid>
      <description>
	
&lt;p&gt;
One of the craziest fake news trends is the flat earth conspiracy. Some of you might ask whether my trip actually &lt;em&gt;around&lt;/em&gt; the world or am I travelling across a &lt;em&gt;flat disk&lt;/em&gt;? This article discusses flat earth mathematics, and how to convert and visualise map projections in R. This article discusses flat earth mathematics, using code I published earlier in articles about &lt;a href=&#34;https://lucidmanager.org/data-science/pacific-island-hopping/&#34;&gt;pacific island hopping&lt;/a&gt; and &lt;a href=&#34;https://lucidmanager.org/data-science/create-air-travel-route-maps/&#34;&gt;creating air travel maps&lt;/a&gt;.&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-1&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-1&#34;&gt;
The Flat Earth
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-1&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;YouTube contains thousands of videos from people that claim the earth is flat. These people pontificate that science as we know it is a &amp;#34;globalist conspiracy&amp;#34;. The claims about a flat earth are hilarious and entertaining. People who believe our planet is flat even conduct simple experiments and engage in naive flat earth mathematics. Needless to say that these experiments are not very convincing, but I respect their entrepreneurial attitude.&lt;/p&gt;
&lt;p&gt;
Adherents to the idea that the world is flat often propose Gleason&amp;#39;s Map as their version of the correct representation of our planet. Every printed map of the earth is flat, but somehow this one seems to have a special status.&lt;/p&gt;
&lt;p&gt;
The Gleason map is not a map of a flat earth but a polar &lt;a href=&#34;https://en.wikipedia.org/wiki/Azimuthal_equidistant_projection&#34;&gt;azimuthal equidistant projection&lt;/a&gt; of the globe. Even though the map itself states that it is a &lt;em&gt;projection&lt;/em&gt; of the world, flat earth believers nevertheless see it as a literal representation of the world they live on.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/geography/gleasons-map.jpg&#34; alt=&#34;Section of Gleason&amp;#39;s map&#34; title=&#34;Section of Gleason&amp;#39;s map&#34;/&gt;
&lt;figcaption&gt;
Section of Gleason&amp;#39;s map.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
Their belief is based on an appeal to common sense as the world appears to be flat when we look at the horizon. The second ingredient is a deep distrust in science, often inspired by religious motives.&lt;/p&gt;
&lt;p&gt;
This article shows two different ways to look at the earth and show that the spherical model better fits the reality of my trip around the world.&lt;/p&gt;
&lt;p&gt;
Projecting the spherical earth on a flat surface is a complex task. Maps require compromise as it is impossible to truthfully draw the surface of the globe on a piece of paper without distortion. The video below provides a practical introduction to map projections and shows how maps are stretched to display them on a flat surface.&lt;/p&gt;
&lt;figure&gt;
&lt;div style=&#34;position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden;&#34;&gt;
      &lt;iframe allow=&#34;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share; fullscreen&#34; loading=&#34;eager&#34; referrerpolicy=&#34;strict-origin-when-cross-origin&#34; src=&#34;https://www.youtube.com/embed/kIID5FDi2JQ?autoplay=0&amp;amp;controls=1&amp;amp;end=0&amp;amp;loop=0&amp;amp;mute=0&amp;amp;start=0&#34; style=&#34;position: absolute; top: 0; left: 0; width: 100%; height: 100%; border:0;&#34; title=&#34;YouTube video&#34;&gt;&lt;/iframe&gt;
    &lt;/div&gt;

&lt;figcaption&gt;
Why all world maps are wrong.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
The Azimuthal equidistant method is popularised in the flag of the United Nations. Antarctica is in this projection displayed as a ring around the world because the south pole projects to infinity. Flat earth evangelists believe that the South Pole a ring of ice that prevents us from proceeding beyond the disc. The edge of this disc cannot be shown because this method projects the south pole at an infinite distance from the centre of the map. We can recreate Gleason&amp;#39;s map with &lt;a href=&#34;https://ggplot2.tidyverse.org/reference/coord_map.html&#34;&gt;ggplot&lt;/a&gt;, which incorporates the &lt;a href=&#34;https://cran.r-project.org/web/packages/mapproj/index.html&#34;&gt;mapproj package&lt;/a&gt; to show maps in various projections.&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://github.com/pprevos/geography/&#34; target=&#34;_blank&#34;
   title=&#34;Download geography from GitHub&#34;
   alt=&#34;Download geography from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;geography&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Flat Earth Mathematics&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(ggplot2)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  world &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;map_data&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;world&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ggplot&lt;/span&gt;(world) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;geom_path&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;aes&lt;/span&gt;(x &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; long, y &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; lat, group &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; group), linewidth &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;.2&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;theme&lt;/span&gt;(axis.text &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;element_blank&lt;/span&gt;(),
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          axis.ticks &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;element_blank&lt;/span&gt;()) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;labs&lt;/span&gt;(x &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;&amp;#34;&lt;/span&gt;, y &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;coord_map&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;azequidistant&amp;#34;&lt;/span&gt;, orientation &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;90&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;270&lt;/span&gt;))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
The &lt;a href=&#34;https://ggplot2.tidyverse.org/reference/coord_map.html&#34;&gt;coord_map&lt;/a&gt; function allows you to change projections. The orientation argument defines the centre point of the projection. The most common location is the North Pole (90, 0). The third value gives the clockwise rotation in degrees. Gleason&amp;#39;s map uses a rotation of 270 degrees.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/geography/azequidistant.png&#34; alt=&#34;/images/geography/azequidistant.png&#34; title=&#34;Polar azimuthal equidistant projection with ggmap&#34; at=&#34;Polar azimuthal equidistant projection with ggmap&#34;/&gt;
&lt;figcaption&gt;
Polar azimuthal equidistant projection with ggmap.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-2&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-2&#34;&gt;
My Round-the-World Itinerary
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-2&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;In September 2018 I embarked on a trip around the world to speak at the &lt;a href=&#34;http://worldwatercongress.org/&#34;&gt;World Water Congress&lt;/a&gt; in Tokyo. I also visited family and friends in the Netherlands and did some tourism in San Francisco. One of the workshops I speak at is about Fake News in the water industry. &lt;/p&gt;
&lt;p&gt;
My trip started in Australia, onward to Japan, from where I go to the Netherlands. The last two legs will bring me back to Australia via San Francisco. The itinerary is stored in a data frame. The ggmap package geocodes the longitude and latitude of each of the locations on my trip. You will need a Google API to enable the &lt;a href=&#34;https://lucidmanager.org/data-science/geocoding-with-ggmap/&#34;&gt;geocoding function&lt;/a&gt;. As the earth is a sphere, an intermediate point needs to be added for trips that pass the dateline, as I explained in my article about &lt;a href=&#34;https://lucidmanager.org/data-science/create-air-travel-route-maps/&#34;&gt;flight maps&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;
The geosphere package helps to estimate the total travel distance. The travel distance is approximately 38,995 km, slightly less than a trip around the equator. This distance is the &lt;a href=&#34;https://en.wikipedia.org/wiki/Great-circle_distance&#34;&gt;great circle distance&lt;/a&gt;, which is the shortest distance between two points on a sphere. The flight paths on this map are curved because of the inevitable distortions when projecting a sphere on a flat surface.&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Round the World Itinerary&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(ggmap)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(dplyr)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  api &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;readLines&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;google.api&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#888&#34;&gt;# Text file with the secret API key&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;register_google&lt;/span&gt;(key &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; api)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  airports &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Melbourne&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Tokyo&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Amsterdam&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;San Francisco&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  itinerary &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;geocode&lt;/span&gt;(airports)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  itinerary &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;rbind&lt;/span&gt;(itinerary, itinerary[1, ]) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;mutate&lt;/span&gt;(location &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(airports, airports[1]))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Split travel past dateline&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  dl &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;which&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;diff&lt;/span&gt;(itinerary&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;lon) &lt;span style=&#34;color:#333&#34;&gt;&amp;gt;&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;180&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  dr &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ifelse&lt;/span&gt;(itinerary&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;lon[dl] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;-180&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;180&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  dateline &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;tibble&lt;/span&gt;(lon &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(dr, &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt;dr),
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                     lat &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;rep&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;mean&lt;/span&gt;(itinerary&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;lat[dl&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;(dl &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;)]), &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;),
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                     location &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;dateline&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  itinerary &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;rbind&lt;/span&gt;(itinerary[1&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;dl, ], dateline,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                     itinerary&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;[&lt;/span&gt;(dl &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;)&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;nrow&lt;/span&gt;(itinerary), ])
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  itinerary
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Visualise&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  worldmap &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;geom_point&lt;/span&gt;(data &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; itinerary, &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;aes&lt;/span&gt;(lon, lat), colour &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;red&amp;#34;&lt;/span&gt;, size &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;4&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;geom_path&lt;/span&gt;(data &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; itinerary, &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;aes&lt;/span&gt;(lon, lat), colour &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;red&amp;#34;&lt;/span&gt;, size &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;coord_map&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;azequidistant&amp;#34;&lt;/span&gt;, orientation &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;90&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;270&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Great Circle Distance&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(geosphere)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sapply&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;nrow&lt;/span&gt;(itinerary) &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;), &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;function&lt;/span&gt;(l)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;distVincentyEllipsoid&lt;/span&gt;(itinerary[l, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;], itinerary&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;[&lt;/span&gt;(l &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;), &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;]) &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1000&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sum&lt;/span&gt;()&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/geography/rtw.png&#34; alt=&#34;Round the world trip in polar Azimuthal equidistant projection&#34; title=&#34;Round the world trip in polar Azimuthal equidistant projection&#34;/&gt;
&lt;figcaption&gt;
Round the world trip in polar Azimuthal equidistant projection.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-3&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-3&#34;&gt;
Flat Earth Mathematics
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-3&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;If the Gleason map were an actual map of the flat earth, then the flight paths on the map would show as straight lines.&lt;/p&gt;
&lt;p&gt;
The &lt;a href=&#34;https://cran.r-project.org/web/packages/mapproj/index.html&#34;&gt;mapproj package&lt;/a&gt; contains the mapproject function that calculates the projected coordinates based on longitude and latitude. The output of this function is a grid with limits from $-\pi$ to  $\pi$. The first part of the code converts the longitude and latitude from the world data frame to projected coordinates.&lt;/p&gt;
&lt;p&gt;
A line from the lon/lat 0,0 to the north pole has a projected distance of \(\pi/2\), which in the spherical world is \(\frac{\pi}{2} 6378.137 = 10018.75\) km. We need to multiply the Euclidean distances with the radius of the Earth to derive the Gleason map coordinates.&lt;/p&gt;
&lt;p&gt;
This last code snippet converts the world map to flat earth coordinates. It calculates the Euclidean distance between the points on the itinerary and multiplies this with the Earth&amp;#39;s diameter. &lt;/p&gt;
&lt;p&gt;
The best way to show that Gleason&amp;#39;s map is not a map of a flat earth is best demonstrated with a flight between Australia and Chili because the distortions are huge in the Southern hemisphere. This last code snippet shows why the Gleason map is not a map of a flat earth. On this map, the shortest distance between Sydney and Santiago de Chili is about 25,000 km, more than twice the real value. The &lt;a href=&#34;https://www.travelmath.com/from/Sydney,+Australia/to/Santiago,+Chile&#34;&gt;actual travel time&lt;/a&gt; is about 14 hours, which implies that passenger jets break the sound barrier. This problem exists for all journeys along the lines of latitude in the Southern hemisphere.&lt;/p&gt;
&lt;p&gt;
This map looks like the first one, but the coordinate system is now Euclidean instead of polar (longitudes and latitudes), as indicated by the square grid. On a projected map, the shortest distance is a curved line, parallel to Antarctica, which is how ships and aeroplanes move between these cities.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/geography/flatearth.png&#34; alt=&#34;Sydney to Santiago de Chili on a flat earth map&#34; title=&#34;Sydney to Santiago de Chili on a flat earth map&#34;/&gt;
&lt;figcaption&gt;
Sydney to Santiago de Chili on a flat earth map.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
This video shows a unique insight into the navigational issues for intercontinental flights in the Southern hemisphere.&lt;/p&gt;
&lt;figure&gt;
&lt;div style=&#34;position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden;&#34;&gt;
      &lt;iframe allow=&#34;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share; fullscreen&#34; loading=&#34;eager&#34; referrerpolicy=&#34;strict-origin-when-cross-origin&#34; src=&#34;https://www.youtube.com/embed/TqWmjtbGs-M?autoplay=0&amp;amp;controls=1&amp;amp;end=0&amp;amp;loop=0&amp;amp;mute=0&amp;amp;start=0&#34; style=&#34;position: absolute; top: 0; left: 0; width: 100%; height: 100%; border:0;&#34; title=&#34;YouTube video&#34;&gt;&lt;/iframe&gt;
    &lt;/div&gt;

&lt;figcaption&gt;
Aerolineas Argentinas Southern Transpolar Flight from Sydney to Buenos Aires.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(mapproj)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  flatearth.coords &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;mapproject&lt;/span&gt;(world&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;long, world&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;lat,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                          &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;azequidistant&amp;#34;&lt;/span&gt;, orientation &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;90&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;270&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  r &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;6378.137&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  flatearth.coords &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;mutate&lt;/span&gt;(world,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                  x &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; flatearth.coords&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;x &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; r,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                  y &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; flatearth.coords&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;y &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; r) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;select&lt;/span&gt;(x, y, group, order, region, subregion)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  flatearth &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ggplot&lt;/span&gt;(flatearth.coords) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;geom_path&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;aes&lt;/span&gt;(x, y, group &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; group), size &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;.2&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;theme&lt;/span&gt;(axis.text &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;element_blank&lt;/span&gt;(),
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            axis.ticks &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;element_blank&lt;/span&gt;()) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;labs&lt;/span&gt;(x &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;&amp;#34;&lt;/span&gt;, y &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Australia - South America&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  airports &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;tibble&lt;/span&gt;(city &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Sydney&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Santiago de Chili&amp;#34;&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  itinerary &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;geocode&lt;/span&gt;(airports&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;city) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;mutate&lt;/span&gt;(location &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; airports&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;city)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  itinerary
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  coords &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;mapproject&lt;/span&gt;(itinerary&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;lon, itinerary&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;lat, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;azequidistant&amp;#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                       orientation &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;90&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;270&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  coords &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;tibble&lt;/span&gt;(x &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; coords&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;x &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; r, y &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; coords&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;y &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; r)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sum&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sqrt&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;diff&lt;/span&gt;(coords&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;x)^2 &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;diff&lt;/span&gt;(coords&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;y)^2))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  flatearth &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;geom_point&lt;/span&gt;(data &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; coords, &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;aes&lt;/span&gt;(x, y), colour &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;red&amp;#34;&lt;/span&gt;, size &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;4&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;geom_path&lt;/span&gt;(data &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; coords, &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;aes&lt;/span&gt;(x, y), colour &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;red&amp;#34;&lt;/span&gt;, size &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
  &lt;a href = &#34;https://github.com/pprevos/geography/&#34; target=&#34;_blank&#34;
     title=&#34;Download geography from GitHub&#34;
     alt=&#34;Download geography from GitHub&#34;&gt;
    &lt;button class=&#34;button is-medium is-primary&#34;&gt;
      &lt;span class=&#34;icon is-large&#34;&gt;
        &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
      &lt;/span&gt;
      &lt;span style=&#34;font-family: monospace&#34;&gt;geography&lt;/span&gt;
    &lt;/button&gt;
  &lt;/a&gt;
  
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-4&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-4&#34;&gt;
Conclusion
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-4&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;This article proves that the Gleason map is not a representation of a flat earth. Aeroplanes would have to break the sound barrier to fly these distances in the time it takes to travel. Whichever way you project the globe on a flat map will lead to inevitable distortions. The Gleason map itself mentions that it is a projection and not a flat earth. However, these facts will not dissuade people from believing in a flat earth. I am after all an engineer and thus part of the globalist science conspiracy.&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://www.r-bloggers.com/&#34; target=&#34;_blank&#34; title=&#34;Proudly associated with R-Bloggers&#34;&gt;
  &lt;button class=&#34;button is-link is-medium&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-r-project&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span&gt;As seen on R Bloggers&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;

      </description>
    </item>
    
    
    
    <item>
      <title>Call Centre Workforce Planning Using Erlang C in R language</title>
      <link>https://lucidmanager.org/data-science/call-centre-workforce-planning-erlang-c-in-r/</link>
      <pubDate>Thu, 19 Jul 2018 00:00:00 +0000</pubDate>
      <author>Peter Prevos</author>
      <guid>https://lucidmanager.org/data-science/call-centre-workforce-planning-erlang-c-in-r/</guid>
      <description>
	
&lt;p&gt;
We all hate the experience of calling a service provider and being placed on hold for a very long time. Organisations that take their level of service seriously plan their call centres. Good planning ensures that waiting times for customers are within acceptable limits. Having said this, making people wait for something can, in some instances, increase perceived value. This article shows how to implement call centre workforce planning with the R language.&lt;/p&gt;
&lt;p&gt;
Contact centre workforce planning involves an exciting mix of rational production management and the soft skills of providing service with a smile. The mathematics of managing a call centre ensures customers have a great experience by limiting waiting times.&lt;/p&gt;
&lt;p&gt;
The Grade of Service can numerically express call centre performance. This KPI is the percentage of calls answered within a specific time. For example, 90% of calls are responded to within 30 seconds. This Grade of Service depends on the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The volume of calls made to the centre.&lt;/li&gt;
&lt;li&gt;The number of available agents&lt;/li&gt;
&lt;li&gt;The time it takes to service a customer.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Working in a call centre can be chaotic, so we need some maths to understand it. The &lt;a href=&#34;https://www.callcentrehelper.com/erlang-c-formula-example-121281.htm&#34;&gt;Erlang C formula&lt;/a&gt; describes the relationship between the Grade of Service and various variables to bring some order to the chaos.&lt;/p&gt;
&lt;p&gt;
Call centre workforce planning is a complex activity that is a perfect problem to solve in R code. This article explains using the Erlang C formula in the R language to manage a contact centre. The presented code calculates the number of agents needed to meet a required Grade of Service. This approach is extended to a Monte Carlo situation to understand the real world&amp;#39;s stochastic nature better.&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-1&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-1&#34;&gt;
The Erlang C Formula
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-1&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The Erlang C formula describes the probability that a customer needs to queue instead of being immediately serviced $(P_w)$. This formula is closely related to the &lt;a href=&#34;https://en.wikipedia.org/wiki/Poisson_distribution&#34;&gt;Poisson distribution&lt;/a&gt;, which describes queues such as traffic lights or hospital waiting lists.&lt;/p&gt;
&lt;p&gt;
$$P_w = \frac{\frac{A^N}{N!}\frac{N}{N-A}}{\Big( \sum_{i=0}^{N-1} \frac{A^i}{i!} \Big)+\frac{A^N}{N!}\frac{N}{N-A}}$$&lt;/p&gt;
&lt;p&gt;
The intensity of traffic $A$ is the number of calls per hour multiplied by the average duration of a call. Traffic intensity is measured in dimensionless Erlang units, which expresses the time it would take to manage all calls if they arrived sequentially. The intensity measures the effort that needs to be undertaken in an hour. In reality, calls arrive at random times during the hour, which is where the Poisson distribution comes in. The waiting time is also influenced by the number of available operators $N$.&lt;/p&gt;
&lt;p&gt;
Both common sense and the formula show that increasing the number of calls or decreasing the number of operators increases the likelihood that customers are placed in a queue. The more staff, the higher the service level, but how many people do you need to achieve your desired grade of service efficiently?&lt;/p&gt;
&lt;p&gt;
The Grade of Service $S$ is a function of the outcome of the Erlang C formula ($P_w$), the number of agents ($N$), the call intensity ($A$), call duration ($\lambda$) and lastly the target answering time ($t$).&lt;/p&gt;
&lt;p&gt;
The Erlang C formula can be reworked to provide that answer. I sourced this solution from &lt;a href=&#34;https://www.callcentrehelper.com/erlang-c-formula-example-121281.htm&#34;&gt;callcenterhelper.com&lt;/a&gt;. We now have a toolset for call centre planning which we can implement in the R language.&lt;/p&gt;
&lt;p&gt;
$$S = 1 - \Large[ P_w e^ {-[(N-A](t/ \lambda)]} \large]$$&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-2&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-2&#34;&gt;
Erlang C in R
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-2&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The Erlang C formula contains some factorials and powers, which become problematic when dealing with significant call volumes or many agents. The Multiple Precision Arithmetic library enables working with &lt;a href=&#34;https://lucidmanager.org/data-science/project-euler-20/&#34;&gt;large integer factorials&lt;/a&gt;, but there is no need to wield such strong computing powers. To make life easier, the Erlang C formula includes the &lt;a href=&#34;https://en.wikipedia.org/wiki/Erlang_(unit)#Erlang_B_formula&#34;&gt;Erlang B formula&lt;/a&gt;, the inverse of which can be calculated using a small loop.&lt;/p&gt;
&lt;p&gt;
This implementation is very similar to an unpublished R package by &lt;a href=&#34;https://github.com/phubers/erlang&#34;&gt;Patrick Hubers&lt;/a&gt;, enhanced with work from &lt;a href=&#34;https://www.callcentrehelper.com/erlang-c-formula-example-121281.htm&#34;&gt;callcenterhelper.com&lt;/a&gt;. This code contains four functions:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;code class=&#34;verbatim&#34;&gt;intensity&lt;/code&gt;: Determines intensity in &lt;a href=&#34;https://en.wikipedia.org/wiki/Erlang_(unit)&#34;&gt;Erlangs&lt;/a&gt; based on the rate of calls per interval, the total call handling time and the interval time in minutes. All functions default to an interval time of sixty minutes. Erlangs are a dimensionless unit as a measure of the offered load.&lt;/li&gt;
&lt;li&gt;&lt;code class=&#34;verbatim&#34;&gt;erlang_c&lt;/code&gt;: Processes the Erlang C formula using the number of agents and the variables determining intensity (call rate and call handling time). The output is the probability that a customer has to wait for service.&lt;/li&gt;
&lt;li&gt;&lt;code class=&#34;verbatim&#34;&gt;service_level&lt;/code&gt;: Calculates the service level. The inputs are the same as above, plus the period for the Grade of Service in seconds.&lt;/li&gt;
&lt;li&gt;&lt;code class=&#34;verbatim&#34;&gt;resource&lt;/code&gt;: Seeks the number of agents needed to meet a Grade of Service. This function starts with the minimum number of agents (the intensity plus one agent) and keeps searching until it finds the number of agents that achieve the desired Grade of Service.&lt;/li&gt;
&lt;/ol&gt;
&lt;div id=&#34;outline-container-headline-3&#34; class=&#34;outline-3&#34;&gt;
&lt;h3 id=&#34;headline-3&#34;&gt;
Erlang C Code
&lt;/h3&gt;
&lt;div id=&#34;outline-text-headline-3&#34; class=&#34;outline-text-3&#34;&gt;
&lt;p&gt;
&lt;a href = &#34;https://github.com/pprevos/r4h2o/&#34; target=&#34;_blank&#34;
   title=&#34;Download r4h2o from GitHub&#34;
   alt=&#34;Download r4h2o from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;r4h2o&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Call Centre Workforce Planning Using Erlang C in R language&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Erlang-C Functions&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  intensity &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;function&lt;/span&gt;(rate, duration, interval &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;60&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      (rate &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt; interval) &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; duration
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  erlang_c &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;function&lt;/span&gt;(agents, rate, duration, interval &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;60&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      int &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;intensity&lt;/span&gt;(rate, duration, interval)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      erlang_b_inv &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;for&lt;/span&gt; (i &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;in&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;agents) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          erlang_b_inv &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; erlang_b_inv &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; i &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt; int
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      erlang_b &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt; erlang_b_inv
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      agents &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; erlang_b &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt; (agents &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; int &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; (&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; erlang_b))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  service_level &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;function&lt;/span&gt;(agents, rate, duration, target, interval &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;60&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      pw &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;erlang_c&lt;/span&gt;(agents, rate, duration, interval)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      int &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;intensity&lt;/span&gt;(rate, duration, interval)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; (pw &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;exp&lt;/span&gt;(&lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt;(agents &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; int) &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; (target &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt; duration)))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  resource &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;function&lt;/span&gt;(rate, duration, target, gos_target, interval &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;60&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      agents &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;round&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;intensity&lt;/span&gt;(rate, duration, interval) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      gos &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;service_level&lt;/span&gt;(agents, rate, duration, target, interval)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;while&lt;/span&gt; (gos &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;&lt;/span&gt; gos_target &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; (gos_target &lt;span style=&#34;color:#333&#34;&gt;&amp;gt;&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;100&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          agents &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; agents &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          gos &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;service_level&lt;/span&gt;(agents, rate, duration, target, interval)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;return&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(agents, gos))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-4&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-4&#34;&gt;
Call centre workforce planning using R
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-4&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;For example, you manage a call centre with 100 calls every 30 minutes and an average handling time of 3 minutes. These numbers result in an intensity of &lt;code class=&#34;verbatim&#34;&gt;intensity(100, 3, 30)&lt;/code&gt; 10 Erlangs or ten calls hours of traffic per hour. This result means we need at least ten agents, assuming all calls arrive neatly after each other.&lt;/p&gt;
&lt;p&gt;
The reality of a contact centre is chaotic, as calls arrive randomly, so there is a high probability that an operator will be busy if we only use ten agents. You can enter this data into the Erlang C in R function to find the chance a customer needs to wait. With ten agents, the likelihood is 100%. With 11 agents, it is 68%. The &lt;code class=&#34;verbatim&#34;&gt;sapply&lt;/code&gt; function helps to view the impact of adding more customer service
agents.&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;erlang_c&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;11&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;100&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;3&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;30&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  [1] &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0.6821182&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sapply&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;20&lt;/span&gt;, erlang_c, rate &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;100&lt;/span&gt;, duration &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;3&lt;/span&gt;, interval &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;30&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  [1] &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1.000000000&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0.682118205&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0.449388224&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0.285270453&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0.174131934&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0.102042367&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  [7] &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0.057340331&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0.030876110&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0.015928277&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0.007873558&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0.003731126&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
The likelihood of a customer having to wait is the main target for any service that involves queueing. We need to set a target service level or Grade of Service. The service_level function calculates the Grade of Service using the previous parameters plus the target in seconds. Let&amp;#39;s make some assumptions:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Required Grade of Service 80%&lt;/li&gt;
&lt;li&gt;Target answer time: 20 seconds&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Using the function, we can quickly see that with 11 agents and 100 calls per 30 minutes, we only have a Grade of Service of 39%, much less than the required 80%. Using &lt;code class=&#34;verbatim&#34;&gt;sapply()&lt;/code&gt;, we can see that we need just over 13 available agents.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/hydroinformatics/call-centre-example.png&#34; alt=&#34;Grade of Service for a call centre with 100 calls per 30 minutes and 3-minute contacts&#34; title=&#34;Grade of Service for a call centre with 100 calls per 30 minutes and 3-minute contacts&#34; width=&#34;600&#34;/&gt;
&lt;figcaption&gt;
Grade of Service for a call centre with 100 calls per 30 minutes and 3-minute contacts.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
The &lt;code class=&#34;verbatim&#34;&gt;resource()&lt;/code&gt; function ties everything together and seeks the number of agents needed to meet the required grade of service. We need at least 14 available agents to answer 80% of calls within 20 seconds, which gives us an actual predicted Grade of Service of 88%.&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Example&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;service_level&lt;/span&gt;(agents &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;11&lt;/span&gt;, rate &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;100&lt;/span&gt;, duration &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;3&lt;/span&gt;, target &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;20&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;60&lt;/span&gt;, interval &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;30&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  service &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;data.frame&lt;/span&gt;(agents &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;20&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                        GoS &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sapply&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;20&lt;/span&gt;, service_level, rate &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;100&lt;/span&gt;, duration &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;3&lt;/span&gt;, target &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;20&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;60&lt;/span&gt;, interval &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;30&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(ggplot2)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ggplot&lt;/span&gt;(service, &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;aes&lt;/span&gt;(agents, GoS)) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;geom_line&lt;/span&gt;() &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;scale_x_continuous&lt;/span&gt;(breaks &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;20&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;scale_y_continuous&lt;/span&gt;(labels &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; scales&lt;span style=&#34;color:#333&#34;&gt;::&lt;/span&gt;percent) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;geom_hline&lt;/span&gt;(yintercept &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0.8&lt;/span&gt;, col &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;blue&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;theme_bw&lt;/span&gt;(base_size &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;labs&lt;/span&gt;(title &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Call Centre Simulation&amp;#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;         subtitle &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Erlang-C Formula&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ggsave&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;call-centre-example.png&amp;#34;&lt;/span&gt;, width &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;6&lt;/span&gt;, height &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;4&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;resource&lt;/span&gt;(rate &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;100&lt;/span&gt;, duration &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;3&lt;/span&gt;, target &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;20&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;60&lt;/span&gt;, gos_target &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;80&lt;/span&gt;, interval&lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;30&lt;/span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-5&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-5&#34;&gt;
Call Centre Workforce Planning with Erlang C Monte Carlo Simulation
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-5&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;I used the Erlang C model to recommend staffing levels in a contact centre some years ago. This experience taught me that the mathematical model is the first step towards call centre workforce planning. Several other metrics can be built on the Erlang C model, such as agents&amp;#39; average occupancy and handling time.&lt;/p&gt;
&lt;p&gt;
Like all mathematical models, the Erlang C formula is an idealised version of reality. Agents are not always available; they need breaks, toilet stops and might even go on leave. Employers call this loss of labour shrinkage, a somewhat negative term to describe something positive for the employee. The Erlang C model provides the number of &amp;#39;bums on seats&amp;#39;.&lt;/p&gt;
&lt;p&gt;
Like every model, the Erlang C formula is not a perfect representation of reality. The formula tends to overestimate the required resources because it assumes that people will stay on hold indefinitely, while the queue will automatically shorten as people lose patience.&lt;/p&gt;
&lt;p&gt;
The number of employees needed to provide this capacity depends on the working conditions at the call centre. For example, if employees are only available to take calls 70% of their contracted time, you will need $1/0.7=1.4$ staff members for each live agent to meet the Grade of Service.&lt;/p&gt;
&lt;p&gt;
Another problem is the stochastic nature of call volumes and handling times. The Erlang C model requires a manager to estimate call volume and handling time (intensity) as a static variable, while in reality, it is stochastic and subject to variation. Time series analysis can help to predict call volumes, but every prediction has a degree of uncertainty. We can manage this uncertainty by using a Monte Carlo simulation.&lt;/p&gt;
&lt;p&gt;
All the functions listed above can be rewritten to provide a vector of possible answers based on the average call volume and duration and their standard deviation. This simulation assumes a normal distribution for both call volume and the length of each call. The outcome of this simulation is a distribution of service levels.&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-6&#34; class=&#34;outline-3&#34;&gt;
&lt;h3 id=&#34;headline-6&#34;&gt;
Monte Carlo Simulation
&lt;/h3&gt;
&lt;div id=&#34;outline-text-headline-6&#34; class=&#34;outline-text-3&#34;&gt;
&lt;p&gt;A &lt;a href=&#34;https://lucidmanager.org/data-science/monte-carlo-cost-estimates/&#34;&gt;Monte Carlo simulation&lt;/a&gt; is a method to simulate reality by defining a probability distribution of the events in the model.&lt;/p&gt;
&lt;p&gt;
For example, a call centre receives, on average, 100 calls per half hour with a standard deviation of 10 calls. The average time to manage a call, including wrap-up time after the call, is 180 seconds, with a standard deviation of 20 seconds. The centre needs to answer 80% of calls within 20 seconds. What is the likelihood of achieving this level of service?&lt;/p&gt;
&lt;p&gt;
The average intensity of this scenario is 10 Erlangs. Using the resource formula suggests that we need 14 agents to meet the Grade of Service. Simulating the intensity of the scenario 1000 times indicates that the intensity is between 5.4 and 15.9 Erlangs (see code at the end of this article).&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;resource&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;100&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;180&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;20&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;80&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;30&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  [1] &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;14.0000000&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0.88835&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;intensity_mc&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;100&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;180&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;20&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;summary&lt;/span&gt;()
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  Min. &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;st Qu. Median Mean &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;3&lt;/span&gt;rd Qu. Max.
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;5.480&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;8.975&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;9.939&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10.025&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10.993&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;15.932&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
The next step is to simulate the expected service level for this scenario. The plot visualises the outcome of the Monte Carlo simulation and shows that in 95% of situations, the Grade of Service is more than 77% and half the time, it is more than 94%.&lt;/p&gt;
&lt;pre class=&#34;example&#34;&gt;
  service_level_mc(15, 100, 10, 180, 20, 20, 30, sims = 1000) %&amp;gt;%
  + quantile(c(.05, .5, .95))
  5% 50% 95%
  0.7261052 0.9427592 0.9914338
&lt;/pre&gt;
&lt;p&gt;
This article shows that Erlang C in R helps managers plan call centre workforce. We may need a Shiny application to develop a tool to manage the complexity of these functions. I would love to hear from people with practical experience in managing call centres in how they analyse data.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/hydroinformatics/call-centre-monte-carlo.png&#34; alt=&#34;Simulated service levels using Erlang C and Monte Carlo simulation&#34; title=&#34;Simulated service levels using Erlang C and Monte Carlo simulation&#34; width=&#34;600&#34;/&gt;
&lt;figcaption&gt;
Simulated service levels using Erlang C and Monte Carlo simulation.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-7&#34; class=&#34;outline-3&#34;&gt;
&lt;h3 id=&#34;headline-7&#34;&gt;
Monte Carlo Simulation Code
&lt;/h3&gt;
&lt;div id=&#34;outline-text-headline-7&#34; class=&#34;outline-text-3&#34;&gt;
&lt;a href = &#34;https://github.com/pprevos/r4h2o/&#34; target=&#34;_blank&#34;
   title=&#34;Download r4h2o from GitHub&#34;
   alt=&#34;Download r4h2o from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;r4h2o&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;


&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Monte Carlo Simulation&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(tidyverse)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  intensity_mc &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;function&lt;/span&gt;(rate_m, rate_sd, duration_m, duration_sd, interval &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;60&lt;/span&gt;, sims &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1000&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    (&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;rnorm&lt;/span&gt;(sims, rate_m, rate_sd) &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt; (&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;60&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; interval)) &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;rnorm&lt;/span&gt;(sims, duration_m, duration_sd)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;intensity_mc&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;100&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;180&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;20&lt;/span&gt;, interval &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;30&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;summary&lt;/span&gt;()
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  erlang_c_mc &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;function&lt;/span&gt;(agents, rate_m, rate_sd, duration_m, duration_sd, interval &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;60&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    int &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;intensity_mc&lt;/span&gt;(rate_m, rate_sd, duration_m, duration_sd, interval)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    erlang_b_inv &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;for&lt;/span&gt; (i &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;in&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;agents) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      erlang_b_inv &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; erlang_b_inv &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; i &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt; int
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    erlang_b &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt; erlang_b_inv
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    agents &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; erlang_b &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt; (agents &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; int &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; (&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; erlang_b))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  service_level_mc &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;function&lt;/span&gt;(agents, rate_m, rate_sd, duration_m, duration_sd, target, interval &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;60&lt;/span&gt;, sims &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1000&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    pw &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;erlang_c_mc&lt;/span&gt;(agents, rate_m, rate_sd, duration_m, duration_sd, interval)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    int &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;intensity_mc&lt;/span&gt;(rate_m, rate_sd, duration_m, duration_sd, interval, sims)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; (pw &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;exp&lt;/span&gt;(&lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt;(agents &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; int) &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; (target &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;rnorm&lt;/span&gt;(sims, duration_m, duration_sd))))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;tibble&lt;/span&gt;(ServiceLevel &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;service_level_mc&lt;/span&gt;(agents &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;14&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                                         rate_m &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;100&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                                         rate_sd &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                                         duration_m &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;180&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                                         duration_sd &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;20&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                                         target &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;20&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                                         interval &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;30&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                                         sims &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1000&lt;/span&gt;)) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ggplot&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;aes&lt;/span&gt;(ServiceLevel)) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;geom_histogram&lt;/span&gt;(binwidth &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0.05&lt;/span&gt;, fill &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;#008da1&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;scale_x_continuous&lt;/span&gt;(labels &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; scales&lt;span style=&#34;color:#333&#34;&gt;::&lt;/span&gt;percent) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;theme_bw&lt;/span&gt;(base_size &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;labs&lt;/span&gt;(title &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Call Centre Monte Carlo Simulation&amp;#34;&lt;/span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
&lt;a href = &#34;https://www.r-bloggers.com/&#34; target=&#34;_blank&#34; title=&#34;Proudly associated with R-Bloggers&#34;&gt;
  &lt;button class=&#34;button is-link is-medium&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-r-project&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span&gt;As seen on R Bloggers&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

      </description>
    </item>
    
    
    
    <item>
      <title>Project Euler 33: Digit Cancelling Fractions</title>
      <link>https://lucidmanager.org/data-science/project-euler-033/</link>
      <pubDate>Mon, 09 Jul 2018 00:00:00 +0000</pubDate>
      <author>Peter Prevos</author>
      <guid>https://lucidmanager.org/data-science/project-euler-033/</guid>
      <description>
	
&lt;p&gt;
Project Euler 33 takes us back to the world of fractions from our primary school days. This article provides a solution for the Euler problem and discusses how to solve this problem using Farey sequences and how to visualise Ford Circles with the &lt;em&gt;ggplot2&lt;/em&gt; package.&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-1&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-1&#34;&gt;
Project Euler 33 Definition
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-1&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The fraction 49/98 is curious, as an inexperienced mathematician in attempting to simplify it may incorrectly believe that 49/98 = 4/8, which is correct, is obtained by cancelling the nines. We shall consider fractions like 30/50 = 3/5, to be trivial examples.&lt;/p&gt;
&lt;p&gt;
There are precisely four nontrivial examples of this type of fraction, less than one in value, and containing two digits in the numerator and denominator. If the product of these four fractions is given in its lowest common terms, &lt;a href=&#34;https://projecteuler.net/problem=33&#34;&gt;find the value of the denominator&lt;/a&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-2&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-2&#34;&gt;
Proposed Solution in R
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-2&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;To solve this problem, we create a pseudo-Farey sequence by generating all different fractions with two decimals in the numerator and denominator. The loop generates all combinations of denominators and numerators, excluding the trivial ones (multiples of 10 and 11). This solution converts the numbers to strings, strips any common duplicates, and tests the condition. The code concatenates vectors, which is not good practice. However, the loop is so short it does not matter much.&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://github.com/pprevos/ProjectEuler/&#34; target=&#34;_blank&#34;
   title=&#34;Download ProjectEuler from GitHub&#34;
   alt=&#34;Download ProjectEuler from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;ProjectEuler&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Project Euler 33: Digit Cancelling Fractions&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  num &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;vector&lt;/span&gt;()
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  den &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;vector&lt;/span&gt;()
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;for&lt;/span&gt; (a &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;in&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;11&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;99&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;for&lt;/span&gt; (b &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;in&lt;/span&gt; (a &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;)&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;99&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          trivial &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; (a &lt;span style=&#34;color:#333&#34;&gt;%%&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;|&lt;/span&gt; b &lt;span style=&#34;color:#333&#34;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;|&lt;/span&gt; a &lt;span style=&#34;color:#333&#34;&gt;%%&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;11&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;|&lt;/span&gt; b &lt;span style=&#34;color:#333&#34;&gt;%%&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;11&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (&lt;span style=&#34;color:#333&#34;&gt;!&lt;/span&gt;trivial) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;              i &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;as.numeric&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;unlist&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;strsplit&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;as.character&lt;/span&gt;(a), &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;&amp;#34;&lt;/span&gt;)))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;              j &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;as.numeric&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;unlist&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;strsplit&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;as.character&lt;/span&gt;(b), &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;&amp;#34;&lt;/span&gt;)))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;              digs &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(i, j)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;              dup &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; digs&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;[duplicated&lt;/span&gt;(digs)]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;              digs &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; digs&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;[which&lt;/span&gt;(digs &lt;span style=&#34;color:#333&#34;&gt;!=&lt;/span&gt; dup)]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;              &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;length&lt;/span&gt;(digs) &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;&amp;amp;&lt;/span&gt; a&lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt;b &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; digs[1]&lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt;digs[2]) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                  num &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(num, a)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                  den &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(den, b)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                  }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;paste&lt;/span&gt;(num, den, sep &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;/&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  answer &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;prod&lt;/span&gt;(den) &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;prod&lt;/span&gt;(num)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;print&lt;/span&gt;(answer)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-3&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-3&#34;&gt;
Farey Sequences and Ford Circles
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-3&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;Next step is to generalise Euler problem 33 and write a function to generate Farey Sequences and visualise them using Ford Circles. This Numberphile video below explains fractions and Farey sequences. A Farey sequence contains all fractions between 0 and 1 with a maximum denominator. More formally, a Farey sequence of order n is the sequence of completely reduced fractions between 0 and 1. These fractions have denominators less than or equal to n, arranged in order of increasing size. For example, the Farey Sequence with order 3 is:&lt;/p&gt;
&lt;p&gt;
$$F_3 = \Big\{ \frac{0}{1},\frac{1}{3},\frac{1}{2},\frac{2}{3},\frac{1}{1}\Big\}$$&lt;/p&gt;
&lt;p&gt;
Ford Circles are a fractal-esque way to visualise Farey sequences, but before we get to this, first solve Euler problem 33.&lt;/p&gt;
&lt;figure&gt;
&lt;div style=&#34;position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden;&#34;&gt;
      &lt;iframe allow=&#34;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share; fullscreen&#34; loading=&#34;eager&#34; referrerpolicy=&#34;strict-origin-when-cross-origin&#34; src=&#34;https://www.youtube.com/embed/0hlvhQZIOQw?autoplay=0&amp;amp;controls=1&amp;amp;end=0&amp;amp;loop=0&amp;amp;mute=0&amp;amp;start=0&#34; style=&#34;position: absolute; top: 0; left: 0; width: 100%; height: 100%; border:0;&#34; title=&#34;YouTube video&#34;&gt;&lt;/iframe&gt;
    &lt;/div&gt;

&lt;figcaption&gt;
Funny Fractions and Ford Circles by Numberphile.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
The farey function generates a data table with the numerators (p) and denominators (q) of a Farey sequence. The function builds a list of all possible fractions for the solution space, excluding those with one as a Greatest Common Denominator, as defined by the &lt;code class=&#34;verbatim&#34;&gt;gcd()&lt;/code&gt; function.&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Farey sequence function&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  farey &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;function&lt;/span&gt;(n) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      fseq &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;list&lt;/span&gt;()
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      fseq[[1]] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      i &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      gcd &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;function&lt;/span&gt;(a, b) { &lt;span style=&#34;color:#888&#34;&gt;# Euclid&amp;#39;s method&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (a &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;) &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;return&lt;/span&gt;(b)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (b &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;) &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;return&lt;/span&gt;(a)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;gcd&lt;/span&gt;(b, a &lt;span style=&#34;color:#333&#34;&gt;%%&lt;/span&gt; b)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;for&lt;/span&gt; (q &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;in&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;n) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;for&lt;/span&gt; (p &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;in&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;(q &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;)){
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;              &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;gcd&lt;/span&gt;(p, q) &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                  fseq[[i]] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(p, q)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                  i &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; i &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                  }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      fseq[[i]] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      fseq &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;as.data.frame&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;do.call&lt;/span&gt;(rbind, fseq))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;names&lt;/span&gt;(fseq) &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;p&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;q&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      fseq &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; fseq&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;[order&lt;/span&gt;(fseq&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;p &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt; fseq&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;q), ]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;return&lt;/span&gt;(fseq)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
Standard ggplot2 cannot draw circles where the radius of the circles is related to the coordinate system. I used a circle function sourced from &lt;a href=&#34;https://stackoverflow.com/questions/6862742/draw-a-circle-with-ggplot2&#34;&gt;StackOverflow&lt;/a&gt;. This function is called in a for-loop to build the circles on top of the empty canvas.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/project-euler/problem-033.png&#34; alt=&#34;/images/project-euler/problem-033.png&#34; title=&#34;Farey Sequence and Ford Circles (n = 20).&#34;/&gt;
&lt;figcaption&gt;
Farey Sequence and Ford Circles (n = 20).
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(ggplot2)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  lm_palette &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;#008da1&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;#005395&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;#262e43&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;#3b2758&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;#865596&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;#f26230&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  ford_circles &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;farey&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;20&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;mutate&lt;/span&gt;(x &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; p &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt; q,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;           y &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt; (&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; q^2),
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;           r &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; y,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;           c &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; lm_palette&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;[&lt;/span&gt;(q &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;)&lt;span style=&#34;color:#333&#34;&gt;%%&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;6&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;])
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  g_circle &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;function&lt;/span&gt;(r, x, y, color &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;NA&lt;/span&gt;, fill &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;black&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;...&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    x &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; x &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; r &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;cos&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;seq&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;, &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;pi&lt;/span&gt;, length.out &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;100&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    ymax &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; y &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; r &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sin&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;seq&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;, &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;pi&lt;/span&gt;, length.out &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;100&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    ymin &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; y &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; r &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sin&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;seq&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;, &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt;&lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;pi&lt;/span&gt;, length.out &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;100&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;annotate&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;ribbon&amp;#34;&lt;/span&gt;, x &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; x, ymin &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; ymin, ymax &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; ymax,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;             color &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; color, fill &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; fill, &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;...&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  p &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ggplot&lt;/span&gt;(ford_circles, &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;aes&lt;/span&gt;(x, y))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;for&lt;/span&gt; (i &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;in&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;nrow&lt;/span&gt;(ford_circles)) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    p &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; p &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;g_circle&lt;/span&gt;(ford_circles&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;r[i], ford_circles&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;x[i], ford_circles&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;y[i],
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                      fill &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; ford_circles&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;c[i])
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  p &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;xlim&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;)) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;coord_fixed&lt;/span&gt;() &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;theme_void&lt;/span&gt;()
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ggsave&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;images/problem-033.png&amp;#34;&lt;/span&gt;, width &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;6&lt;/span&gt;, height &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;4&lt;/span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
&lt;a href = &#34;https://www.r-bloggers.com/&#34; target=&#34;_blank&#34; title=&#34;Proudly associated with R-Bloggers&#34;&gt;
  &lt;button class=&#34;button is-link is-medium&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-r-project&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span&gt;As seen on R Bloggers&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;

      </description>
    </item>
    
    
    
    <item>
      <title>Analyse Definitions of Marketing with Rvest and Tidytext</title>
      <link>https://lucidmanager.org/data-science/marketing-wordcloud/</link>
      <pubDate>Thu, 31 May 2018 00:00:00 +0000</pubDate>
      <author>Peter Prevos</author>
      <guid>https://lucidmanager.org/data-science/marketing-wordcloud/</guid>
      <description>
	
&lt;p&gt;
I am preparing to facilitate another session of the marketing course for the &lt;a href=&#34;https://www.latrobe.edu.au/courses/mba&#34;&gt;La Trobe University MBA&lt;/a&gt;. The first lecture delves into the definition of marketing. Like most other social phenomena, marketing is tough to define. Definitions of social constructs often rely on the perspective taken by the person or group writing the definition. As such, definitions also change over time. While a few decades ago, definitions of marketing revolved around sales and advertising,
&lt;a href=&#34;https://web.archive.org/web/20190201101014/https://www.ama.org/AboutAMA/Pages/Definition-of-Marketing.aspx&#34;&gt;contemporary definitions&lt;/a&gt; are more holistic and reference creating value. &lt;/p&gt;
&lt;p&gt;
Heidi Cohen wrote a blog post where she collated &lt;a href=&#34;https://heidicohen.com/marketing-definition/&#34;&gt;72 definitions of marketing&lt;/a&gt;. So rather than arguing over which definition is the best, why not use all definitions simultaneously? This article attempts to define a new definition of marketing, using a data science approach. We can use the R language to scrape the 72 definitions from Heidi&amp;#39;s website and attempt text analysis to extract the essence of marketing from this data set.&lt;/p&gt;
&lt;p&gt;
I have mentioned in a previous post about &lt;a href=&#34;https://lucidmanager.org/data-science/qualitative-data-science/&#34;&gt;qualitative data science&lt;/a&gt; that automated text analysis is not always a useful method to extract meaning from a text. I decided to delve a little deeper into automated text analysis to see if we find out anything useful about marketing using the rvest and tidytext packages.&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-1&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-1&#34;&gt;
Scraping Definitions of Marketing with Rvest
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-1&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;Web scraping is a technique to download data from websites where this data is not available as a clean data source. Web scraping starts with downloading the HTML code from the website and the filtering the wanted text from this file. The &lt;a href=&#34;https://cran.r-project.org/web/packages/rvest/&#34;&gt;rvest package&lt;/a&gt; simplifies this process.&lt;/p&gt;
&lt;p&gt;
The code for this article uses a pipe (&lt;code class=&#34;verbatim&#34;&gt;%&amp;gt;%&lt;/code&gt;) with three rvest commands. The first step is to download the wanted html code from the web using the &lt;code class=&#34;verbatim&#34;&gt;read_html&lt;/code&gt; function. The output of this function is piped to the &lt;code class=&#34;verbatim&#34;&gt;html_nodes&lt;/code&gt; function, which does all the hard work. In this case, the code picks all lines of text that are embedded in an ordered list. You  can use the &lt;a href=&#34;https://selectorgadget.com/&#34;&gt;SelectorGadget&lt;/a&gt; to target the text you like to scrape. The last scraping step cleans the text by piping the output of the previous commands to the &lt;code class=&#34;verbatim&#34;&gt;html_text&lt;/code&gt; function.&lt;/p&gt;
&lt;p&gt;
The result of the scraping process is converted to a &lt;a href=&#34;https://tibble.tidyverse.org/&#34;&gt;Tibble&lt;/a&gt;, which is a type of data frame used in the Tidyverse. The definition number is added to the data, and the Tibble is converted to the format required by the Tidytext package.
The resulting data frame is much longer than the 72 definitions because there are other lists on the page. Unfortunately, I could not find a way to filter only the 72 definitions.&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://github.com/pprevos/digital-humanities/&#34; target=&#34;_blank&#34;
   title=&#34;Download digital-humanities from GitHub&#34;
   alt=&#34;Download digital-humanities from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;digital-humanities&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(tidyverse)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(rvest)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(tidytext)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(wordcloud)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(RColorBrewer)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  lm_palette &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;#f7881f&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;#55ace1&amp;#34;&lt;/span&gt;,  &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;#5f6c36&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Scrape definitions from website&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  definitions &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;read_html&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;https://heidicohen.com/marketing-definition/&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;html_nodes&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;ol li&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;html_text&lt;/span&gt;() &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;as_tibble&lt;/span&gt;() &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;mutate&lt;/span&gt;(No &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;nrow&lt;/span&gt;(.)) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;select&lt;/span&gt;(No, Definition &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; value)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-2&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-2&#34;&gt;
Tidying the Text
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-2&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The &lt;a href=&#34;https://www.tidytextmining.com/&#34;&gt;Tidytext&lt;/a&gt; package extends the &lt;a href=&#34;http://vita.had.co.nz/papers/tidy-data.html&#34;&gt;tidy data&lt;/a&gt; philosophy to a text. In this approach to text analysis, a corpus consists of a data frame where each word is a separate item. The code snippet below takes the first 72 rows, and the &lt;code class=&#34;verbatim&#34;&gt;unnest_tokens&lt;/code&gt; function extracts each word from the 72 definitions. This function can also extract ngrams and other word groups from the text. The Tidytext package is an extremely versatile piece of software which goes far beyond the scope of this article. Julia Silge and David Robinson have written a book about &lt;a href=&#34;https://www.tidytextmining.com/&#34;&gt;text mining&lt;/a&gt; using this package, which provides a very clear introduction to the craft of analysing text.&lt;/p&gt;
&lt;p&gt;
The last section of the pipe removes the trailing &amp;#34;s&amp;#34; from each word to convert plurals into single words. The mutate function in the Tidyverse creates or recreates a new variable in a data frame.&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## bag of words&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  def_words &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; definitions[1&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;72&lt;/span&gt;, ] &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;unnest_tokens&lt;/span&gt;(word, Definition) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;mutate&lt;/span&gt;(word &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;gsub&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;s$&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;&amp;#34;&lt;/span&gt;, word))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
This section creates a data frame with two variables. The No variable indicates the definition number (1–72) and the word variable is a word within the definition. The order of the words is preserved in the row name. To check the data frame you can run &lt;code class=&#34;verbatim&#34;&gt;unique(def_words$No[which(def_words$word =&lt;/code&gt; &amp;#34;marketing&amp;#34;)])=. This line finds all definition numbers with the word &amp;#34;marketing&amp;#34;, wich results, as expected, in the number 1 to 72.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-3&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-3&#34;&gt;
Using Rvest and Tidytext to define marketing
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-3&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;We can now proceed to analyse the definitions scraped from the website
with Rvest and cleaned with Tidytext. The first step is to create a word
cloud, which is a popular way to visualise word frequencies. This code
creates a data frame for each unique word, excluding the word marketing
itself, and uses the wordcloud package to visualise the fifty most
common words.&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  word_freq &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; def_words &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;anti_join&lt;/span&gt;(stop_words) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;count&lt;/span&gt;(word) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;filter&lt;/span&gt;(&lt;span style=&#34;color:#333&#34;&gt;!&lt;/span&gt;(word &lt;span style=&#34;color:#333&#34;&gt;%in%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;               &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;marketing&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;vice&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;president&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;chief&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;executive&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;’&amp;#34;&lt;/span&gt;)))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;pdf&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;marketingcloud.pdf&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  word_freq &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;with&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;wordcloud&lt;/span&gt;(word, n, max.words &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;50&lt;/span&gt;, rot.per &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;.5&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                     colors &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;rev&lt;/span&gt;(lm_palette)))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;dev.off&lt;/span&gt;()&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/digital-humanities/marketing-definition.png&#34; alt=&#34;Wordcloud of 72 definitios of marketing&#34; title=&#34;Wordcloud of 72 definitios of marketing&#34;/&gt;
&lt;figcaption&gt;
Wordcloud of 72 definitios of marketing.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
While a word cloud is indeed a pretty way to visualise the bag of words in a text, it is not the most useful way to get the reader to understand the data. The words are jumbled, and the reader needs to search for meaning. A better way to visualise word frequencies is a bar chart. This code takes the data frame created in the previous snippet, determines the top ten occurring words. The mutate statement reorders to factor levels so that the words are plotted in order.&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    word_freq &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;top_n&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;mutate&lt;/span&gt;(word &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;reorder&lt;/span&gt;(word, n)) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ggplot&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;aes&lt;/span&gt;(word, n)) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;geom_col&lt;/span&gt;(fill &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; lm_palette[2]) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;coord_flip&lt;/span&gt;() &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;theme&lt;/span&gt;(text &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;element_text&lt;/span&gt;(size &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;15&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ggsave&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;marketing-words.png&amp;#34;&lt;/span&gt;, width &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;6&lt;/span&gt;, height &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;6&lt;/span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/digital-humanities/marketing-words.png&#34; alt=&#34;Top ten words in marketing definitions&#34; title=&#34;/images/digital-humanities/marketing-words.png&#34; tite=&#34;Top ten words in marketing definitions&#34;/&gt;
&lt;figcaption&gt;
Top ten words in marketing definitions.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
A first look at the word cloud and bar chart suggests that marketing is about customers and products and services. Marketing is a process that includes branding and communication. This definition is simplistic but functional.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-4&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-4&#34;&gt;
Topic Modeling using Tidytext
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-4&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;Word frequencies are a weak method to analyse text because it interprets each word as a solitary unit. Topic modelling is a more advanced method that examines the relationships between words, i.e. the distance between them. The first step is to create a &lt;a href=&#34;https://en.wikipedia.org/wiki/Document-term_matrix&#34;&gt;Document-Term Matrix&lt;/a&gt;, which is a matrix that indicates how often a word appears in a text. As each of the 72 texts are very short, I decided to treat the collection of definitions as one text about marketing. The &lt;code class=&#34;verbatim&#34;&gt;cast_dtm()&lt;/code&gt; function converts the data frame to a Document-Term Matrix.&lt;/p&gt;
&lt;p&gt;
The following pipe determines the top words in the topics. Just like k-means clustering, the analyst needs to choose the number of topics before analysing the text. In this case, I have opted for four topics. The code determines the contribution of each word to the four topics and selects the five most common words in each topic. The faceted bar chart shows each of the words in the four topics.&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Topic modeling&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(topicmodels)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  marketing_dtm &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; word_freq &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;mutate&lt;/span&gt;(doc &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;cast_dtm&lt;/span&gt;(doc, word, n)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  marketing_lda &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;LDA&lt;/span&gt;(marketing_dtm, k &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;4&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;tidy&lt;/span&gt;(matrix &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;beta&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;group_by&lt;/span&gt;(topic) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;top_n&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;5&lt;/span&gt;, beta) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ungroup&lt;/span&gt;() &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;arrange&lt;/span&gt;(topic, &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt;beta)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  marketing_lda &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;mutate&lt;/span&gt;(term &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;reorder&lt;/span&gt;(term, beta)) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ggplot&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;aes&lt;/span&gt;(term, beta, fill &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;factor&lt;/span&gt;(topic))) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;geom_col&lt;/span&gt;(show.legend &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;FALSE&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;         &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;facet_wrap&lt;/span&gt;(&lt;span style=&#34;color:#333&#34;&gt;~&lt;/span&gt;topic, scales &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;free&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;         &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;coord_flip&lt;/span&gt;() &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;         &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;theme&lt;/span&gt;(text &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;element_text&lt;/span&gt;(size &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;20&lt;/span&gt;))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/digital-humanities/marketing-topics.png&#34; alt=&#34;Topic modelling 72 definitions of marketing&#34; title=&#34;Topic modelling 72 definitions of marketing&#34;/&gt;
&lt;figcaption&gt;
Topic modelling 72 definitions of marketing.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
This example also does not tell me much more about what marketing is, other than giving a slightly more sophisticated version of the word frequency charts. This chart shows me that marketing is about customers that enjoy a service and a product. Perhaps the original definitions are not distinctive enough to be separated from each other.&lt;/p&gt;
&lt;p&gt;
This article is only a weak summary of the great work by Julia Silge who co-authored the Tidytext package. The video below provides a comprehensive introduction to topic modelling.&lt;/p&gt;
&lt;figure&gt;
&lt;div style=&#34;position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden;&#34;&gt;
      &lt;iframe allow=&#34;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share; fullscreen&#34; loading=&#34;eager&#34; referrerpolicy=&#34;strict-origin-when-cross-origin&#34; src=&#34;https://www.youtube.com/embed/evTuL-RcRpc?autoplay=0&amp;amp;controls=1&amp;amp;end=0&amp;amp;loop=0&amp;amp;mute=0&amp;amp;start=0&#34; style=&#34;position: absolute; top: 0; left: 0; width: 100%; height: 100%; border:0;&#34; title=&#34;YouTube video&#34;&gt;&lt;/iframe&gt;
    &lt;/div&gt;

&lt;figcaption&gt;
Topic modelling with R andtidy data principles.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-5&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-5&#34;&gt;
What have we learnt?
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-5&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;This excursion into text analysis using rvest and Tidytext shows that data science can help us to make some sense out of an unread text. If I did not know what this page was about, then perhaps this analysis would enlighten me. This kind of analysis can assist us in wading through to large amounts of text to select the ones we want to read. I am still not convinced that this type of analysis will provide any knowledge beyond what can be obtained from actually reading and engaging with a text.&lt;/p&gt;
&lt;p&gt;
Although I am a data scientist and want to maximise the use of code in analysing data, I am very much in favour of developing human intelligence before we worry about the artificial kind.&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://www.r-bloggers.com/&#34; target=&#34;_blank&#34; title=&#34;Proudly associated with R-Bloggers&#34;&gt;
  &lt;button class=&#34;button is-link is-medium&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-r-project&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span&gt;As seen on R Bloggers&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;

      </description>
    </item>
    
    
    
    <item>
      <title>Qualitative Data Science: Using RQDA to analyse interviews</title>
      <link>https://lucidmanager.org/data-science/qualitative-data-science/</link>
      <pubDate>Thu, 03 May 2018 00:00:00 +0000</pubDate>
      <author>Peter Prevos</author>
      <guid>https://lucidmanager.org/data-science/qualitative-data-science/</guid>
      <description>
	
&lt;p&gt;
Qualitative data science sounds like a contradiction in terms. Data scientists generally solve problems using numerical solutions. Even the analysis of a text is reduced to a numerical problem using Markov chains, topic analysis, &lt;a href=&#34;https://lucidmanager.org/data-science/tap-water-sentiment-analysis/&#34;&gt;sentiment analysis&lt;/a&gt; and other mathematical tools.&lt;/p&gt;
&lt;p&gt;
Scientists and professionals consider numerical methods to be the gold standard of analysis. There is, however, a price to pay when relying on numbers alone. Numerical analysis reduces the complexity of the social world. When analysing people, numbers present an illusion of precision and accuracy. Giving primacy to quantitative research in the social sciences comes at a high price. The dynamics of reality are reduced to statistics, losing the narrative of the people that the research aims to understand.&lt;/p&gt;
&lt;p&gt;
Being both an engineer and a social scientist, I acknowledge the importance of both numerical and qualitative methods. My dissertation used a mixed-method approach to review the relationship between employee behaviour and customer perception in water utilities. This article introduces some aspects of qualitative data science with an example from my dissertation.&lt;/p&gt;
&lt;p&gt;
In this article, I show how I analysed data from interviews using both quantitative and qualitative methods and demonstrate why qualitative data science is better to understand text than numerical methods. &lt;/p&gt;
&lt;div id=&#34;outline-container-headline-1&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-1&#34;&gt;
Qualitative Data Science
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-1&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The often celebrated artificial intelligence of machine learning is impressive but does not come close to human intelligence and ability to understand the world. Many data scientists are working on automated text analysis to solve this issue (the &lt;a href=&#34;https://cran.r-project.org/web/packages/topicmodels/&#34;&gt;topicmodels&lt;/a&gt; package is an example of such an attempt). These efforts are impressive, but even the smartest text analysis algorithm is not able to derive &lt;em&gt;meaning&lt;/em&gt; from text. To fully embrace all aspects of data science we need to be able to methodically undertake &lt;a href=&#34;https://en.wikipedia.org/wiki/Qualitative_research&#34;&gt;qualitative data analysis&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;
The capabilities of R in numerical analysis are impressive, but it can also assist with Qualitative Data Analysis (QDA). Huang Ronggui from Hong Kong developed the &lt;a href=&#34;http://rqda.r-forge.r-project.org/&#34;&gt;RQDA package&lt;/a&gt; to analyse texts in R. RQDA assists with qualitative data
analysis using a GUI front-end to analyse collections texts. &lt;/p&gt;
&lt;p&gt;
For my dissertation about &lt;a href=&#34;https://lucidmanager.org/tags/invisible-water-utility/&#34;&gt;water utility marketing&lt;/a&gt;, I interviewed seven people from various organisations. The purpose of these interviews was to learn about the value proposition for water utilities. The data consists of the transcripts of six interviews which I manually coded using RQDA. For reasons of agreed anonymity, I cannot provide the raw data file of the interviews through GitHub.&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-2&#34; class=&#34;outline-3&#34;&gt;
&lt;h3 id=&#34;headline-2&#34;&gt;
Numerical Text Analysis
&lt;/h3&gt;
&lt;div id=&#34;outline-text-headline-2&#34; class=&#34;outline-text-3&#34;&gt;
&lt;p&gt;Word clouds are a popular method for exploratory analysis of texts. The wordcloud is created with the &lt;a href=&#34;https://cran.r-project.org/web/packages/tm/index.html&#34;&gt;text mining&lt;/a&gt; and wordcloud packages. The transcribed interviews are converted to a text corpus (the native format for the tm package) and whitespace, punctuation etc is removed. This code snippet opens the RQDA file and extracts the texts from the database. RQDA stores all text in an SQLite database and the package provides a query command to extract data.&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://github.com/pprevos/digital-humanities/&#34; target=&#34;_blank&#34;
   title=&#34;Download digital-humanities from GitHub&#34;
   alt=&#34;Download digital-humanities from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;digital-humanities&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(tidyverse)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(RQDA)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(tm)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(wordcloud)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(igraph)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;openProject&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;stakeholders.rqda&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;interviews &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;data.frame&lt;/span&gt;(name &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;RQDAQuery&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;SELECT name FROM source&amp;#34;&lt;/span&gt;),
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                         text &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;RQDAQuery&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;SELECT file FROM source&amp;#34;&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;interviews &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;Corpus&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;VectorSource&lt;/span&gt;(interviews&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;file))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;interviews &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;tm_map&lt;/span&gt;(interviews, stripWhitespace)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;interviews &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;tm_map&lt;/span&gt;(interviews, &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;content_transformer&lt;/span&gt;(tolower))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;interviews &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;tm_map&lt;/span&gt;(interviews, removeWords, &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;stopwords&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;english&amp;#34;&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;interviews &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;tm_map&lt;/span&gt;(interviews, removePunctuation)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;interviews &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;tm_map&lt;/span&gt;(interviews, removeNumbers)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;interviews &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;tm_map&lt;/span&gt;(interviews, removeWords, &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;interviewer&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;interviewee&amp;#34;&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#888&#34;&gt;# Word cloud&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;set.seed&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1969&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;wordcloud&lt;/span&gt;(interviews, min.freq &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt;, max.words &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;50&lt;/span&gt;, rot.per&lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0.35&lt;/span&gt;, 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          colors &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;brewer.pal&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;8&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Blues&amp;#34;&lt;/span&gt;)[&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;-1&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;-5&lt;/span&gt;])&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/digital-humanities/wordcloud.png&#34; alt=&#34;Word cloud of interview transcripts&#34; title=&#34;Word cloud of interview transcripts&#34;/&gt;
&lt;figcaption&gt;
Word cloud of interview transcripts.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
This word cloud makes it clear that the interviews are about water businesses and customers, which is a pretty obvious statement. The interviews are also about the opinion of the interviewees (think). While the word cloud is aesthetically pleasing and provides a quick snapshot of the content of the texts, they cannot inform us about their meaning.&lt;/p&gt;
&lt;p&gt;
Topic modelling is a more advanced method to extract information from the text by assessing the proximity of words to each other. The &lt;a href=&#34;https://eight2late.wordpress.com/2015/09/29/a-gentle-introduction-to-topic-modeling-using-r/&#34;&gt;topic modelling&lt;/a&gt; package provides functions to perform this analysis. I am not an expert in this field and simply followed basic steps using default settings with four topics.&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;dtm &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;DocumentTermMatrix&lt;/span&gt;(interviews)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;dtm &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;removeSparseTerms&lt;/span&gt;(dtm, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0.99&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;ldaOut &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;LDA&lt;/span&gt;(dtm, k &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;4&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;terms&lt;/span&gt;(ldaOut,&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;6&lt;/span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
This code converts the corpus created earlier into a &lt;a href=&#34;https://en.wikipedia.org/wiki/Document-term_matrix&#34;&gt;Document-Term Matrix&lt;/a&gt;, which is a matrix of words and documents (the interviews) and the frequency at which each of these words occurs. The LDA function applies a &lt;a href=&#34;https://en.wikipedia.org/wiki/Latent_Dirichlet_allocation&#34;&gt;Latent Dietrich Allocation&lt;/a&gt; to the matrix to define the topics. The variable &lt;code class=&#34;verbatim&#34;&gt;k&lt;/code&gt; defines the number of anticipated topics. An LDA is similar to clustering in multivariate data. The final output is a table with six words for each topic.&lt;/p&gt;
&lt;figure&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Topic 1&lt;/th&gt;
&lt;th&gt;Topic 2&lt;/th&gt;
&lt;th&gt;Topic 3&lt;/th&gt;
&lt;th&gt;Topic 4&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;water&lt;/td&gt;
&lt;td&gt;water&lt;/td&gt;
&lt;td&gt;customers&lt;/td&gt;
&lt;td&gt;water&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;think&lt;/td&gt;
&lt;td&gt;think&lt;/td&gt;
&lt;td&gt;water&lt;/td&gt;
&lt;td&gt;think&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;actually&lt;/td&gt;
&lt;td&gt;inaudible&lt;/td&gt;
&lt;td&gt;customer&lt;/td&gt;
&lt;td&gt;companies&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;customer&lt;/td&gt;
&lt;td&gt;people&lt;/td&gt;
&lt;td&gt;think&lt;/td&gt;
&lt;td&gt;yeah&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;businesses&lt;/td&gt;
&lt;td&gt;service&lt;/td&gt;
&lt;td&gt;business&lt;/td&gt;
&lt;td&gt;issues&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;customers&lt;/td&gt;
&lt;td&gt;businesses&lt;/td&gt;
&lt;td&gt;service&lt;/td&gt;
&lt;td&gt;don&amp;#39;t&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;figcaption&gt;
Topic modelling of interviews.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
This table does not tell me much at all about what was discussed in the interviews. Perhaps it is the frequent use of the word &amp;#34;water&amp;#34; or &amp;#34;think&amp;#34;—I did ask people their opinion about water-related issues. To make this analysis more meaningful, I could perhaps manually remove the words water, yeah, and so on. This introduces a bias in the analysis and reduces the reliability of the topic analysis because I would be interfering with the text.&lt;/p&gt;
&lt;p&gt;
Numerical text analysis sees a text as a bag of words instead of a set of meaningful words. It seems that any automated text mining needs a lot of manual cleaning to derive anything meaningful. This excursion shows that automated text analysis is not a sure-fire way to analyse the meaning of a collection of words. After a lot of trial and error to try to make this work, I decided to go back to my roots of qualitative analysis using RQDA as my tool.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-3&#34; class=&#34;outline-3&#34;&gt;
&lt;h3 id=&#34;headline-3&#34;&gt;
Qualitative Data Science Using RQDA
&lt;/h3&gt;
&lt;div id=&#34;outline-text-headline-3&#34; class=&#34;outline-text-3&#34;&gt;
&lt;p&gt;To use RQDA for qualitative data science, you first need to manually analyse each text and assign codes (topics) to parts of the text. The image below shows a question and answer and how it was coded. All marked text is blue, and the codes are shown between markers. Coding a text is an iterative process that aims to extract meaning from a text. The advantage of this method compared to numerical analysis is that the researcher injects meaning into the analysis. The disadvantage is that the analysis will always be biased, which in the social sciences is unavoidable. My list of topics was based on words that appear in a marketing dictionary so that I analysed the interviews from that perspective.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/digital-humanities/coding.png&#34; alt=&#34;Example of text coded with RQDA&#34; title=&#34;Example of text coded with RQDA&#34;/&gt;
&lt;figcaption&gt;
Example of text coded with RQDA.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
My first step was to look at the occurrence of codes (themes) in each of the interviews.&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#888&#34;&gt;## Load and transform data&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;codings &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;getCodingTable&lt;/span&gt;()[,&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;4&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;5&lt;/span&gt;]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;categories &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;RQDAQuery&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;SELECT filecat.name AS category, source.name AS filename 
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;background-color:#fff0f0&#34;&gt;                         FROM treefile, filecat, source 
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;background-color:#fff0f0&#34;&gt;                         WHERE treefile.catid=filecat.catid AND treefile.fid=source.id AND treefile.status=1&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;codings &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;merge&lt;/span&gt;(codings, categories, all.y &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;TRUE&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;head&lt;/span&gt;(codings)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#888&#34;&gt;## Open coding&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;reorder_size &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;function&lt;/span&gt;(x) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;factor&lt;/span&gt;(x, levels &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;names&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sort&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;table&lt;/span&gt;(x))))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ggplot&lt;/span&gt;(codings, &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;aes&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;reorder_size&lt;/span&gt;(codename), fill&lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt;category)) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;geom_bar&lt;/span&gt;(stat&lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;count&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;facet_grid&lt;/span&gt;(&lt;span style=&#34;color:#333&#34;&gt;~&lt;/span&gt;filename) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;coord_flip&lt;/span&gt;() &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;theme&lt;/span&gt;(legend.position&lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;bottom&amp;#34;&lt;/span&gt;, legend.title&lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;element_blank&lt;/span&gt;()) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ylab&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Code frequency in interviews&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;xlab&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Code&amp;#34;&lt;/span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
The code uses an internal RQDA function &lt;em&gt;getCodingTable&lt;/em&gt; to obtain the primary data. The &lt;em&gt;RQDAQuery&lt;/em&gt; function provides more flexibility and enables more complex queries of the data. You can view the structure of the RQDA database using the &lt;em&gt;RQDATables&lt;/em&gt; function. &lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/digital-humanities/interviewcode.png&#34; alt=&#34;Occurrence of themes from six interviews&#34; title=&#34;Occurrence of themes from six interviews&#34;/&gt;
&lt;figcaption&gt;
The occurrence of themes from six interviews.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
This bar chart helps to explore the topics that interviewees discussed, but it does not help to understand how these topic relate to each other. This method provides better insights into the text than the &amp;#39;bag of words&amp;#39; approach. Qualitative data analysis provides meaning to a text that no algorithm can discern. Although this meaning is ultimately subjective, proper methodology clarified any biases.&lt;/p&gt;
&lt;p&gt;
RQDA provides a facility to assign each code to a code category. A network diagram is a good tool to visualise this structure. The igraph package provides extensive functionality to analyse graphs. &lt;/p&gt;
&lt;p&gt;
Qualitative data analysis can create value from a text by interpreting it from a given perspective. This article is not even an introduction to the science and art of qualitative data science. I hope it invites you to explore RQDA and similar tools.&lt;/p&gt;
&lt;p&gt;
If you are interested in finding out more about this method, then feel free to read chapter three of my dissertation on &lt;a href=&#34;http://hdl.handle.net/1959.9/561679&#34;&gt;customer service in water utilities&lt;/a&gt;.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/digital-humanities/network.png&#34; alt=&#34;Network of interview topics&#34; title=&#34;Network of interview topics&#34;/&gt;
&lt;figcaption&gt;
The network of interview topics.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  edges &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;RQDAQuery&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;SELECT codecat.name, freecode.name FROM codecat, freecode, treecode WHERE codecat.catid=treecode.catid AND freecode.id=treecode.cid&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  g &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;graph_from_edgelist&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;as.matrix&lt;/span&gt;(edges), directed &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;FALSE&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;simplify&lt;/span&gt;()
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;V&lt;/span&gt;(g)&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;name &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;gsub&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34; &amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;\n&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;V&lt;/span&gt;(g)&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;name)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  c &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;spinglass.community&lt;/span&gt;(g)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;par&lt;/span&gt;(mar&lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;rep&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;,&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;4&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;set.seed&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;666&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;plot&lt;/span&gt;(c, g, 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;       vertex.size &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;       vertex.color &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;NA&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;       vertex.frame.color &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;NA&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;       layout &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; layout.drl)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
  &lt;a href = &#34;https://www.r-bloggers.com/&#34; target=&#34;_blank&#34; title=&#34;Proudly associated with R-Bloggers&#34;&gt;
    &lt;button class=&#34;button is-link is-medium&#34;&gt;
      &lt;span class=&#34;icon is-large&#34;&gt;
        &lt;i class=&#34;fab fa-r-project&#34;&gt;&lt;/i&gt;
      &lt;/span&gt;
      &lt;span&gt;As seen on R Bloggers&lt;/span&gt;
    &lt;/button&gt;
  &lt;/a&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

      </description>
    </item>
    
    
    
    <item>
      <title>Tap Water Sentiment Analysis using Twitter and Tidytext</title>
      <link>https://lucidmanager.org/data-science/tap-water-sentiment-analysis/</link>
      <pubDate>Thu, 29 Mar 2018 00:00:00 +0000</pubDate>
      <author>Peter Prevos</author>
      <guid>https://lucidmanager.org/data-science/tap-water-sentiment-analysis/</guid>
      <description>
	
&lt;p&gt;
In developed countries, tap water is safe to drink and available cheaply. Even though high-quality drinking water is almost freely available, the consumption of bottled water is increasing every year. Bottled water companies use sophisticated marketing strategies, while water utilities are mostly passive public service providers. Australian marketing expert Russell Howcroft even called water utilities &amp;#34;lazy marketers&amp;#34;. Can we use a tap water sentiment analysis to learn how people feel about tap water? What can we learn about the reasons behind this loss of trust in the municipal water supply?&lt;/p&gt;
&lt;figure&gt;
&lt;div style=&#34;position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden;&#34;&gt;
      &lt;iframe allow=&#34;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share; fullscreen&#34; loading=&#34;eager&#34; referrerpolicy=&#34;strict-origin-when-cross-origin&#34; src=&#34;https://www.youtube.com/embed/Cg9YH4bm2Sk?autoplay=0&amp;amp;controls=1&amp;amp;end=0&amp;amp;loop=0&amp;amp;mute=0&amp;amp;start=0&#34; style=&#34;position: absolute; top: 0; left: 0; width: 100%; height: 100%; border:0;&#34; title=&#34;YouTube video&#34;&gt;&lt;/iframe&gt;
    &lt;/div&gt;

&lt;figcaption&gt;
Gruen Transfer (2009).
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
This tap water sentiment analysis estimates people&amp;#39;s attitudes towards tap water by analysing tweets. This article explains how to examine tweets about tap water using the R language and the &lt;a href=&#34;https://www.tidytextmining.com/sentiment.html&#34;&gt;Tidytext package&lt;/a&gt;. &lt;/p&gt;
&lt;div id=&#34;outline-container-headline-1&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-1&#34;&gt;
Sentiment Analysis
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-1&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;Sentiment indicates the perception a group of people has about a particular topic. Traditionally, assessing the sentiment of a group of people &lt;a href=&#34;https://lucidmanager.org/data-science/analysing-the-customer-experience/&#34;&gt;requires surveys&lt;/a&gt; or interviews. These methods are problematic because it creates an artificial environment where the respondent often answers to meet the perceived expectations of the study or customers exaggerate to get their point across. Using sentiment analysis of ego documents written by consumers can overcome these problems. Ego-documents, i.e. forms of personal writing, are a more direct way to
find out what consumers think, but they are challenging to obtain and analyse.&lt;/p&gt;
&lt;p&gt;
With the advent of social media, access to ego documents has become much more straightforward, and many tools exist to collect and interpret this data. Using ego documents brings you closer to the consumer than possible with surveys or focus groups. One medium gaining popularity with market researchers is &lt;a href=&#34;https://twitter.com/&#34;&gt;Twitter&lt;/a&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-2&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-2&#34;&gt;
Tap Water Sentiment Analysis
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-2&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;Each tweet containing &amp;#34;tap water&amp;#34; reveals the author&amp;#39;s attitude towards that topic. Each text expresses a sentiment about the subject it describes. &lt;/p&gt;
&lt;p&gt;
&lt;a href=&#34;https://en.wikipedia.org/wiki/Sentiment_analysis&#34;&gt;Sentiment analysis&lt;/a&gt; is a data science technique that extracts personal information from a text. The basic method compares a string of words with a set of words with calibrated sentiments. These calibrated sets are created by asking many people how they feel about a specific term. For example, &amp;#34;stink&amp;#34; expresses a negative sentiment, while &amp;#34;nice&amp;#34; would be a positive sentiment.&lt;/p&gt;
&lt;p&gt;
This tap water sentiment analysis consists of three steps. The first step extracts 1000 tweets containing &amp;#34;tap water&amp;#34; from Twitter. The second step cleans the data, and the third step analyses the results.&lt;/p&gt;
&lt;p&gt;
The &lt;a href=&#34;https://sites.google.com/view/watercoinzerocom/home&#34;&gt;Water Data Aggregator&lt;/a&gt; is a commercial application of sentiment analysis for tap water. This website sells the regional social credit score to interested parties.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-3&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-3&#34;&gt;
Extracting tweets using the TwitteR package
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-3&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The &lt;a href=&#34;https://cran.r-project.org/web/packages/twitteR/index.html&#34;&gt;TwitteR package&lt;/a&gt; by Geoff Gentry makes it very easy to retrieve tweets using search criteria. You must create an API on Twitter to receive the keys and tokens. In the code below, the actual values have been removed.&lt;/p&gt;
&lt;p&gt;
Follow the instructions in &lt;a href=&#34;https://www.r-bloggers.com/setting-up-the-twitter-r-package-for-text-analytics/&#34;&gt;this article&lt;/a&gt; to obtain these codes for yourself. This code snippet calls a private file to load the API codes, extracts the tweets and creates a data frame with a tweet id number and its text.&lt;/p&gt;
&lt;p&gt;
The TwitteR package is no longer availabe. A CSV file of tap water tweets is available on the GitHub repository.&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://github.com/pprevos/r4h2o/&#34; target=&#34;_blank&#34;
   title=&#34;Download r4h2o from GitHub&#34;
   alt=&#34;Download r4h2o from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;r4h2o&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Tapwater tweet sentiment analysis&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(tidyverse)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(tidytext)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  tapwater_tweets &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;read_csv&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;paste0&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;data/tapwater-tweets-&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;Sys.time&lt;/span&gt;(), &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;.csv&amp;#34;&lt;/span&gt;))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
When I first extracted these tweets, a tweet by CNN about &lt;a href=&#34;https://edition.cnn.com/2018/03/30/health/kentucky-water-crisis/index.html&#34;&gt;tap water in Kentucky that smells like diesel&lt;/a&gt; was retweeted many times, so I removed all duplicate tweets from the set. Unfortunately, this left less than 300 original tweets in the corpus.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-4&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-4&#34;&gt;
Sentiment analysis with Tidytext
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-4&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;Text analysis can be a powerful tool to help to analyse large amounts of text. The R language has an extensive collection of packages to help you undertake such a task. The &lt;a href=&#34;https://www.tidytextmining.com/&#34;&gt;Tidytext&lt;/a&gt; package extends the &lt;a href=&#34;http://vita.had.co.nz/papers/tidy-data.html&#34;&gt;Tidy Data&lt;/a&gt; logic promoted by Hadley Wickham and his Tidyverse software collection.&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-5&#34; class=&#34;outline-3&#34;&gt;
&lt;h3 id=&#34;headline-5&#34;&gt;
Data Cleaning
&lt;/h3&gt;
&lt;div id=&#34;outline-text-headline-5&#34; class=&#34;outline-text-3&#34;&gt;
&lt;p&gt;The first step in cleaning the data is to create unigrams, which involve splitting the tweets into individual words that can be analysed. The first step is determining which words are most commonly used in the tap water tweets and visualising the result. The most common phrases are related to drinking and bottled water, which makes sense—also, the recent issues in Kentucky feature in this list.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/hydroinformatics/tapwater-common-words.png&#34; alt=&#34;Most common words in tweets about tap water&#34; title=&#34;Most common words in tweets about tap water&#34;/&gt;
&lt;figcaption&gt;
Most common words in tweets about tap water.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Tokenise and clean the tweets&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  tidy_tweets &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; tapwater_tweets &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;unnest_tokens&lt;/span&gt;(word, text)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;data&lt;/span&gt;(stop_words)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  tidy_tweets &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; tidy_tweets &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;anti_join&lt;/span&gt;(stop_words) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;filter&lt;/span&gt;(&lt;span style=&#34;color:#333&#34;&gt;!&lt;/span&gt;word &lt;span style=&#34;color:#333&#34;&gt;%in%&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;tap&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;water&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;rt&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;https&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;t.co&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;gt&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;amp&amp;#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                        &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;as.character&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;9&lt;/span&gt;)))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Most common words&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  tidy_tweets &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;count&lt;/span&gt;(word, sort &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;TRUE&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;top_n&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;mutate&lt;/span&gt;(word &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;reorder&lt;/span&gt;(word, n)) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ggplot&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;aes&lt;/span&gt;(word, n)) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;geom_col&lt;/span&gt;(fill &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;dodgerblue4&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;xlab&lt;/span&gt;(&lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;NULL&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;coord_flip&lt;/span&gt;() &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;theme_bw&lt;/span&gt;(base_size &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;20&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ggtitle&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Most common words in tap water tweets&amp;#34;&lt;/span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-6&#34; class=&#34;outline-3&#34;&gt;
&lt;h3 id=&#34;headline-6&#34;&gt;
Sentiment Analysis
&lt;/h3&gt;
&lt;div id=&#34;outline-text-headline-6&#34; class=&#34;outline-text-3&#34;&gt;
&lt;p&gt;The Tidytext package contains three lexicons of thousands of single English words (unigrams) that were manually assessed for their sentiment. The principle of sentiment analysis is to compare the words in the text with the words in the lexicon and analyse the results. For example, the statement: &amp;#34;This tap water tastes horrible&amp;#34; has a sentiment score of -3 in the AFFIN system by &lt;a href=&#34;http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6010&#34;&gt;Finn Årup Nielsen&lt;/a&gt; due to the word &amp;#34;horrible&amp;#34;. In this analysis, I have used the &amp;#34;Bing&amp;#34; method published by &lt;a href=&#34;https://www.cs.uic.edu/~liub/FBS/sentiment-analysis.html&#34;&gt;Liu et al. in 2005&lt;/a&gt;. This method assigns a word to either positive, negative or neutral sentiment.&lt;/p&gt;
&lt;p&gt;
This method is not foolproof, as words with the same spelling can mean different things. For example, &amp;#34;This tap water contains too much lead&amp;#34; will be assessed as a positive sentiment because the verb lead is seen as positive. The noun lead has no sentiment, as it depends on context.&lt;/p&gt;
&lt;p&gt;
The other problem with sentiment analysis is that we only see negative commentary. Very few people contact a water utility about an excellent morning shower or a wonderful glass of water. Tap water resides in the background of everyday life, and people don&amp;#39;t have any opinion about it unless it is unavailable or does not meet their aesthetic expectations.&lt;/p&gt;
&lt;p&gt;
This tap water sentiment analysis shows that two-thirds of the words that express a sentiment were negative. The most common negative comments were &amp;#34;smells&amp;#34; and &amp;#34;scared&amp;#34;. This analysis is not a positive result for water utilities. Unfortunately, most tweets were not spatially located, so that I couldn&amp;#39;t determine the origin of the sentiment.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/hydroinformatics/tapwater-sentiment.png&#34; alt=&#34;Tap water sentiment analysis&#34; title=&#34;Tap water sentiment analysis&#34;/&gt;
&lt;figcaption&gt;
Tap water sentiment analysis.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Sentiment analysis&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  sentiment_bing &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; tidy_tweets &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;inner_join&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;get_sentiments&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;bing&amp;#34;&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  sentiment_bing &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;summarise&lt;/span&gt;(Negative &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sum&lt;/span&gt;(sentiment &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;negative&amp;#34;&lt;/span&gt;), 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;              positive &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sum&lt;/span&gt;(sentiment &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;positive&amp;#34;&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  sentiment_bing &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;group_by&lt;/span&gt;(sentiment) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;count&lt;/span&gt;(word, sort &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;TRUE&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;top_n&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ggplot&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;aes&lt;/span&gt;(word, n, fill &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; sentiment)) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;geom_col&lt;/span&gt;(show.legend &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;FALSE&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;coord_flip&lt;/span&gt;() &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;facet_wrap&lt;/span&gt;(&lt;span style=&#34;color:#333&#34;&gt;~&lt;/span&gt;sentiment, scales &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;free_y&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;labs&lt;/span&gt;(title &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Contribution to sentiment&amp;#34;&lt;/span&gt;, x &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;NULL&lt;/span&gt;, y &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;NULL&lt;/span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-7&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-7&#34;&gt;
Using Tap Water Sentiment Analysis
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-7&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;Sentiment analysis is an exciting exploration technique but not an absolute truth. This method cannot detect sarcasm or irony, and words don&amp;#39;t always have the same meaning as described in the dictionary. For example, the algorithm interprets the word &amp;#34;lead&amp;#34; as positive sentiment in its function as a verb. In water, however, the chemical lead is never a positive sentiment. Interpreting tweets as bags of words is exciting but can lead to misunderstanding of the context.&lt;/p&gt;
&lt;p&gt;
The critical message for water utilities is that they need to start taking the aesthetic properties of tap water as seriously as the health parameters. A lack of trust will drive consumers to bottled water or less healthy alternatives such as soft drinks are alternative water sources.&lt;/p&gt;
&lt;p&gt;

  &lt;div class=&#34;box&#34;&gt;
    &lt;div class=&#34;media&#34;&gt;
      &lt;figure class=&#34;media-left&#34;&gt;
        &lt;p class=&#34;image is-128x128&#34;&gt;
          &lt;img src=&#34;https://lucidmanager.org/images/books/2023_ds4wu.jpg&#34; alt = &#34;Data Science for Water Utilities&#34; title = &#34;Data Science for Water Utilities&#34;&gt;
        &lt;/p&gt;
      &lt;/figure&gt;
      &lt;div class=&#34;media-content&#34;&gt;
        &lt;div class=&#34;content&#34;&gt;
          &lt;p class=&#34;is-size-5 has-text-weight-bold&#34;&gt;Data Science for Water Utilities&lt;/p&gt;
          &lt;p class=&#34;mb-2&#34;&gt;Data Science for Water Utilities published by CRC Press is an applied, practical guide that shows water professionals how to use data science to solve urban water management problems using the R language for statistical computing.&lt;/p&gt;
          &lt;div class=&#34;buttons&#34;&gt;
            &lt;a class=&#34;button is-info&#34; href=&#34;https://routledge.com/9781032354545&#34; target=&#34;_blank&#34;&gt;
              &lt;span class=&#34;icon&#34;&gt;
                &lt;i class=&#34;fas fa-shopping-cart&#34;&gt;&lt;/i&gt;
              &lt;/span&gt;
              &lt;span&gt;Routledge&lt;/span&gt;
            &lt;/a&gt;
            &lt;a class=&#34;button is-info&#34; href=&#34;https://www.amazon.com/Data-Science-Water-Utilities-Chapman/dp/1032354550&#34; target=&#34;_blank&#34;&gt;
              &lt;span class=&#34;icon&#34;&gt;
                &lt;i class=&#34;fas fa-shopping-cart&#34;&gt;&lt;/i&gt;
              &lt;/span&gt;
              &lt;span&gt;Amazon&lt;/span&gt;
            &lt;/a&gt;
          &lt;/div&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://www.r-bloggers.com/&#34; target=&#34;_blank&#34; title=&#34;Proudly associated with R-Bloggers&#34;&gt;
  &lt;button class=&#34;button is-link is-medium&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-r-project&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span&gt;As seen on R Bloggers&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;

      </description>
    </item>
    
    
    
    <item>
      <title>Analyse Digital Water Meter Data using the Tidyverse</title>
      <link>https://lucidmanager.org/data-science/analyse-digital-water-meter-data/</link>
      <pubDate>Thu, 08 Feb 2018 00:00:00 +0000</pubDate>
      <author>Peter Prevos</author>
      <guid>https://lucidmanager.org/data-science/analyse-digital-water-meter-data/</guid>
      <description>
	
&lt;p&gt;
In &lt;a href=&#34;https://lucidmanager.org/data-science/simulating-water-consumption/&#34;&gt;last week&amp;#39;s article&lt;/a&gt;, I discussed how to simulate water consumption data to help develop analytics and reporting. This post describes how to analyse digital water meter data and create a diurnal consumption curve.&lt;/p&gt;
&lt;p&gt;
The code is available in the GitHub repository in the &lt;code class=&#34;verbatim&#34;&gt;case-studies&lt;/code&gt; folder.&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://github.com/pprevos/r4h2o/&#34; target=&#34;_blank&#34;
   title=&#34;Download r4h2o from GitHub&#34;
   alt=&#34;Download r4h2o from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;r4h2o&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-1&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-1&#34;&gt;
Data Source
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-1&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The simulated data consists  of three fields:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code class=&#34;verbatim&#34;&gt;device_id&lt;/code&gt;: Unique identifier of the digital water meter.&lt;/li&gt;
&lt;li&gt;&lt;code class=&#34;verbatim&#34;&gt;timestamp&lt;/code&gt;: Time stamp in &lt;a href=&#34;https://en.wikipedia.org/wiki/Coordinated_Universal_Time&#34;&gt;Coordinated Universal Time&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;code class=&#34;verbatim&#34;&gt;count&lt;/code&gt;: Cumulative &lt;em&gt;count&lt;/em&gt; of pulses.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Note that all data is recorded in UTC. The digital water meters send an hourly pulse at a random time within the hour. Each transmitter (RTU) uses a random offset to avoid network congestion. The digital meter counts each time the impeller makes a full turn, and for this analysis, we assume that this equates to a five-litre volume. The ratio between volume and count depends on the meter brand and type. The image below shows a typical data set for an RTU, including some missing data points.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/hydroinformatics/simulated-missing.png&#34; alt=&#34;Simulated water consumption&#34; title=&#34;Simulated water consumption&#34;/&gt;
&lt;figcaption&gt;
Simulated water consumption with missing reads (In Australian time).
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
To analyse the data we need two auxiliary functions: one to slice the data we need and one to interpolate data for the times we need it. The &lt;a href=&#34;https://www.tidyverse.org/&#34;&gt;Tidyverse&lt;/a&gt; heavily influences the code in this article. I prefer the Tidyverse way of doing things because it leads to elegant code that is easy to understand.&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Analyse digital meter data&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(tidyverse)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(lubridate)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(magrittr)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;# Load data&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  meter_reads &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;read_csv&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;data/meter_reads.csv&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  rtu &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;unique&lt;/span&gt;(meter_reads&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;device_id)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-2&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-2&#34;&gt;
Prepare the Analysis
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-2&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;To construct a diurnal curve we need to slice to data to the required window and interpolate the hourly flows.&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-3&#34; class=&#34;outline-3&#34;&gt;
&lt;h3 id=&#34;headline-3&#34;&gt;
Slicing Digital Water Metering Data
&lt;/h3&gt;
&lt;div id=&#34;outline-text-headline-3&#34; class=&#34;outline-text-3&#34;&gt;
&lt;p&gt;Analysis is undertaken on slices of the complete data set. This function slices the available data by a vector of RTU ids and a timestamp range in AEST. This function adds a new timestamp variable in AEST. If no date range is provided, all available data for the selected RTUs is provided. The output of this function is a data frame (a Tibble in Tydiverse language).&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;# Slicing meter reads&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  slice_reads &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;function&lt;/span&gt;(rtus, dates &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;range&lt;/span&gt;(meter_reads&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;timestamp)) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;filter&lt;/span&gt;(meter_reads, device_id &lt;span style=&#34;color:#333&#34;&gt;%in%&lt;/span&gt; rtus) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;filter&lt;/span&gt;(timestamp &lt;span style=&#34;color:#333&#34;&gt;&amp;gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;as.POSIXct&lt;/span&gt;(dates[1], tz &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;UTC&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;&amp;amp;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;             timestamp &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;as.POSIXct&lt;/span&gt;(dates[2], tz &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;UTC&amp;#34;&lt;/span&gt;)) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;arrange&lt;/span&gt;(device_id, timestamp)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;slice_reads&lt;/span&gt;(rtu[12], &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;2050-02-12&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;2050-02-13&amp;#34;&lt;/span&gt;))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-4&#34; class=&#34;outline-3&#34;&gt;
&lt;h3 id=&#34;headline-4&#34;&gt;
Interpolation of Meter Reads
&lt;/h3&gt;
&lt;div id=&#34;outline-text-headline-4&#34; class=&#34;outline-text-3&#34;&gt;
&lt;p&gt;This function interpolates the cumulative counts for a series of RTUs over a vector of timestamps in AEST. The function creates a list to store the results for each RTU, interpolates the data using the &lt;a href=&#34;https://stat.ethz.ch/R-manual/R-devel/library/stats/html/approxfun.html&#34;&gt;approx function&lt;/a&gt; and then flattens the list back to a data frame. The interpolation function contains a different type of pipe because the approx for interpolation function does not take a data argument. The &lt;code class=&#34;verbatim&#34;&gt;%$%&lt;/code&gt; pipe from the Magrittr &lt;a href=&#34;https://cran.r-project.org/web/packages/magrittr/&#34;&gt;package&lt;/a&gt; solves that problem.&lt;/p&gt;
&lt;p&gt;
The output is a data frame with DevEUI, the timestamp in AEST and the interpolated cumulative count. The image above shows the counts for two meters over two days an the graph superimposes an interpolated point over the raw data. Although the actual data consists of integer counts, interpolated values are numeric values. The decimals are retained to distinguish them from real reads.&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;# Interpolation&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  interpolate_count &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;function&lt;/span&gt;(rtus, timestamps) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      timestamps &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;as.POSIXct&lt;/span&gt;(timestamps, tz &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;UTC&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      results &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;vector&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;list&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;length&lt;/span&gt;(rtus))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;for&lt;/span&gt; (r &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;in&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;seq_along&lt;/span&gt;(rtus)) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          interp &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;slice_reads&lt;/span&gt;(rtus[r]) &lt;span style=&#34;color:#333&#34;&gt;%$%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;              &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;approx&lt;/span&gt;(timestamp, count, timestamps)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          results[[r]] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;data_frame&lt;/span&gt;(device_id &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;rep&lt;/span&gt;(rtus[r], 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                                                     &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;length&lt;/span&gt;(timestamps)),
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                                     timestamp &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; timestamps, count &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; interp&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;y)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;return&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;do.call&lt;/span&gt;(rbind, results))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;interpolate_count&lt;/span&gt;(rtu[2&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;3&lt;/span&gt;], &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;seq.POSIXt&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;as.POSIXct&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;2050-02-01&amp;#34;&lt;/span&gt;), 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                                         by &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;day&amp;#34;&lt;/span&gt;, length.out &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;3&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  sample &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;4&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;5&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;slice_reads&lt;/span&gt;(rtu[sample], &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;2050-02-01 20:00&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;2050-02-02 04:00&amp;#34;&lt;/span&gt;)) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ggplot&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;aes&lt;/span&gt;(timestamp, count, col &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;factor&lt;/span&gt;(device_id))) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;geom_line&lt;/span&gt;() &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;geom_point&lt;/span&gt;() &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;geom_point&lt;/span&gt;(data &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;interpolate_count&lt;/span&gt;(rtu[sample], &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;2050-02-02 00:00&amp;#34;&lt;/span&gt;), 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                 col &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;blue&amp;#34;&lt;/span&gt;, size &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;3&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;facet_wrap&lt;/span&gt;(&lt;span style=&#34;color:#333&#34;&gt;~&lt;/span&gt;device_id, scale &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;free_y&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ggtitle&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;paste&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Device&amp;#34;&lt;/span&gt;, rtu[2&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;3&lt;/span&gt;])) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;theme_bw&lt;/span&gt;(base_size &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/hydroinformatics/digital-meters-interpolation.png&#34; alt=&#34;Interpolated digital metering data&#34; title=&#34;Interpolated digital metering data&#34;/&gt;
&lt;figcaption&gt;
Interpolated digital metering data.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
With these two auxiliary functions, we can start analysing the data.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-5&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-5&#34;&gt;
Analyse Daily Consumption
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-5&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;Daily consumption for each connection is a critical metric in managing water resources and billing customers. The daily consumption of any water connection is defined by the difference between the cumulative counts at midnight. The interpolation function makes it easy to determine daily consumption. This function interpolates the midnight reads for each of the RTUs over the period, starting the previous day. The output of the function is a data frame that can be piped into the plotting function to visualise the data. When you group the data by date, you can also determine the total consumption over a group of services.&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;# Daily consumption&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  daily_consumption &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;function&lt;/span&gt;(rtus, dates) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    timestamps &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;seq.POSIXt&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;as.POSIXct&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;min&lt;/span&gt;(dates), tz &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;UTC&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;24&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;3600&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                             &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;as.POSIXct&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;max&lt;/span&gt;(dates), tz &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;UTC&amp;#34;&lt;/span&gt;), by &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;day&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;interpolate_count&lt;/span&gt;(rtus, timestamps) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;group_by&lt;/span&gt;(device_id) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;mutate&lt;/span&gt;(consumption &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;, &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;diff&lt;/span&gt;(count)) &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;5&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;filter&lt;/span&gt;(timestamp &lt;span style=&#34;color:#333&#34;&gt;!=&lt;/span&gt; timestamps[1])
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;daily_consumption&lt;/span&gt;(rtu[32&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;33&lt;/span&gt;], &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;2050-02-01&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;2050-02-07&amp;#34;&lt;/span&gt;)) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ggplot&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;aes&lt;/span&gt;(timestamp, consumption)) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;geom_col&lt;/span&gt;() &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;facet_wrap&lt;/span&gt;(&lt;span style=&#34;color:#333&#34;&gt;~&lt;/span&gt;device_id) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;theme_bw&lt;/span&gt;(base_size &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ggtitle&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Daily consumption&amp;#34;&lt;/span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/hydroinformatics/digital-meters-daily.png&#34; alt=&#34;/images/hydroinformatics/digital-meters-daily.png&#34; title=&#34;Daily water consumption&#34;/&gt;
&lt;figcaption&gt;
Daily water consumption.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-6&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-6&#34;&gt;
Diurnal Curves
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-6&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The diurnal curve is one of the most important pieces of information used in the design of water supply systems. This curve shows the usage of one or more services for each hour in the day. This curve is a reflection of human behaviour, as we use most water in the morning and the evenings.&lt;/p&gt;
&lt;p&gt;
This function slices data for a vector of RTUs over a period and then plots the average diurnal curve. The data is obtained by interpolating the cumulative counts for each whole hour in the period. The function then calculates the flow in litres per hour and visualises the minimum, mean and maximum value.&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;# Diurnal curves&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  plot_diurnal_connections &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;function&lt;/span&gt;(rtus, dates) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    timestamps &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;seq.POSIXt&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;as.POSIXct&lt;/span&gt;(dates[1], tz &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;UTC&amp;#34;&lt;/span&gt;),
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                             &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;as.POSIXct&lt;/span&gt;(dates[2], tz &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;UTC&amp;#34;&lt;/span&gt;), by &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;hour&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;interpolate_count&lt;/span&gt;(rtus, timestamps) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;group_by&lt;/span&gt;(device_id) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;mutate&lt;/span&gt;(rate &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;, &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;diff&lt;/span&gt;(count &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;5&lt;/span&gt;)),
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;             hour &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;as.integer&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;format&lt;/span&gt;(timestamp, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;%H&amp;#34;&lt;/span&gt;))) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;filter&lt;/span&gt;(rate &lt;span style=&#34;color:#333&#34;&gt;&amp;gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;group_by&lt;/span&gt;(hour) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;summarise&lt;/span&gt;(min &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;min&lt;/span&gt;(rate), mean &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;mean&lt;/span&gt;(rate), max &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;max&lt;/span&gt;(rate)) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ggplot&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;aes&lt;/span&gt;(x &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; hour, ymin &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; min, ymax &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; max)) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;geom_ribbon&lt;/span&gt;(fill &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;lightblue&amp;#34;&lt;/span&gt;, alpha &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0.5&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;geom_line&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;aes&lt;/span&gt;(x &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; hour, y &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; mean), col &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;orange&amp;#34;&lt;/span&gt;, linewidth &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ggtitle&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Connections Diurnal flow&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ylab&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Flow rate [L/h]&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;theme_bw&lt;/span&gt;(base_size &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;plot_diurnal_connections&lt;/span&gt;(rtu[10&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;20&lt;/span&gt;], &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;2050-02-01&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;2050-03-01&amp;#34;&lt;/span&gt;))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
Boxplots are also an informative way to visualise this curve. This
method provides more statistical information on one page, and the ggplot
function performs the statistical analysis.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/hydroinformatics/digital-meters-diurnal-curve.png&#34; alt=&#34;Diurnal curve in UTC&#34; title=&#34;Diurnal curve in UTC&#34;/&gt;
&lt;figcaption&gt;
Diurnal curve in UTC.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
Analysing digital metering data is explained in more detail in &lt;a href=&#34;https://lucidmanager.org/data-science/working-with-dates-and-times-in-r/&#34;&gt;chapter 11&lt;/a&gt; of &lt;em&gt;Data Science for Water Utilities&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;

  &lt;div class=&#34;box&#34;&gt;
    &lt;div class=&#34;media&#34;&gt;
      &lt;figure class=&#34;media-left&#34;&gt;
        &lt;p class=&#34;image is-128x128&#34;&gt;
          &lt;img src=&#34;https://lucidmanager.org/images/books/2023_ds4wu.jpg&#34; alt = &#34;Data Science for Water Utilities&#34; title = &#34;Data Science for Water Utilities&#34;&gt;
        &lt;/p&gt;
      &lt;/figure&gt;
      &lt;div class=&#34;media-content&#34;&gt;
        &lt;div class=&#34;content&#34;&gt;
          &lt;p class=&#34;is-size-5 has-text-weight-bold&#34;&gt;Data Science for Water Utilities&lt;/p&gt;
          &lt;p class=&#34;mb-2&#34;&gt;Data Science for Water Utilities published by CRC Press is an applied, practical guide that shows water professionals how to use data science to solve urban water management problems using the R language for statistical computing.&lt;/p&gt;
          &lt;div class=&#34;buttons&#34;&gt;
            &lt;a class=&#34;button is-info&#34; href=&#34;https://routledge.com/9781032354545&#34; target=&#34;_blank&#34;&gt;
              &lt;span class=&#34;icon&#34;&gt;
                &lt;i class=&#34;fas fa-shopping-cart&#34;&gt;&lt;/i&gt;
              &lt;/span&gt;
              &lt;span&gt;Routledge&lt;/span&gt;
            &lt;/a&gt;
            &lt;a class=&#34;button is-info&#34; href=&#34;https://www.amazon.com/Data-Science-Water-Utilities-Chapman/dp/1032354550&#34; target=&#34;_blank&#34;&gt;
              &lt;span class=&#34;icon&#34;&gt;
                &lt;i class=&#34;fas fa-shopping-cart&#34;&gt;&lt;/i&gt;
              &lt;/span&gt;
              &lt;span&gt;Amazon&lt;/span&gt;
            &lt;/a&gt;
          &lt;/div&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://www.r-bloggers.com/&#34; target=&#34;_blank&#34; title=&#34;Proudly associated with R-Bloggers&#34;&gt;
  &lt;button class=&#34;button is-link is-medium&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-r-project&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span&gt;As seen on R Bloggers&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;

      </description>
    </item>
    
    
    
    <item>
      <title>Simulating Water Consumption to Develop Analysis Tools</title>
      <link>https://lucidmanager.org/data-science/simulating-water-consumption/</link>
      <pubDate>Thu, 01 Feb 2018 10:00:19 +0000</pubDate>
      <author>Peter Prevos</author>
      <guid>https://lucidmanager.org/data-science/simulating-water-consumption/</guid>
      <description>
	
&lt;p&gt;
I am currently working on developing analytics for a digital water metering project. Over the next five years, we are enabling 70,000 customer water meters with digital readers and transmitters. The data is yet to be available, but we want to build reporting systems before the data is live. The R language comes to the rescue as it has magnificent capabilities to simulate data. Simulating data is a valuable technique to progress a project when data is being collected. Simulated data also helps because the analysis outcomes are known, which allows for validating the results.&lt;/p&gt;
&lt;p&gt;
The raw data that we will eventually receive from the digital customer meters has the following basic structure:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code class=&#34;verbatim&#34;&gt;device_id&lt;/code&gt;: Unique device identifier.&lt;/li&gt;
&lt;li&gt;&lt;code class=&#34;verbatim&#34;&gt;timestamp&lt;/code&gt;: Date and time in (UTC) of the transmission.&lt;/li&gt;
&lt;li&gt;&lt;code class=&#34;verbatim&#34;&gt;count&lt;/code&gt;: The number of revolutions the water meter makes. Each revolution is a pulse which equates to five litres of water in a regular customer water meter.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Every device will send an hourly data burst which contains the cumulative meter read in pulse counts. The transmitters are set at a random offset from the whole hour to minimise the risk of congestion at the receivers. The timestamp for each read is set in the &lt;a href=&#34;https://en.wikipedia.org/wiki/Coordinated_Universal_Time&#34;&gt;Coordinated Universal Time&lt;/a&gt; (UTC). Using this time zone prevents issues with daylight savings. All analysis will be undertaken in the Australian Eastern (Daylight) Time zone.&lt;/p&gt;
&lt;p&gt;
This article explains how we simulated test data to assist with developing reporting and analysis. The analysis of digital metering data follows in a future post.&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-1&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-1&#34;&gt;
Simulating water consumption
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-1&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;For simplicity, this simulation assumes a standard domestic diurnal curve (average daily usage pattern) for indoor water use. Diurnal curves are an essential piece of information in water management. The curve shows water consumption over a day, averaged over a fixed period. The example below is sourced from a journal article. This generic diurnal curve consists of 24 data points based on measured indoor water consumption, shown in the graph below.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/hydroinformatics/diurnal-curve.jpg&#34; alt=&#34;Diurnal curve&#34; title=&#34;/images/hydroinformatics/diurnal-curve.jpg&#34; titel=&#34;Diurnal curve&#34;/&gt;
&lt;figcaption&gt;
Source: Gurung et al. (2014) Smart meters for enhanced water supply network modelling and infrastructure planning. Resources, Conservation and Recycling (90), 34-50.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
This diurnal curve only includes indoor water consumption and is assumed independent of seasonal variation. This assumption is unrealistic, but the purpose of this simulation is not to accurately model water consumption but to provide a data set to validate the reporting and analyses.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-2&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-2&#34;&gt;
Simulating water consumption in R
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-2&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The first code snippet sets the parameters used in this simulation. The unique device identifiers (DevEUI) are simulated as six-digit random numbers. The timestamps vector consists of hourly date-time variables in UTC. For each transmitter, this timestamp is offset by a random time. Each transmitter is also associated with the number of people living in each house. This number is based on a Poisson distribution.&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;# Simulate water consumption&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(tidyverse)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;rm&lt;/span&gt;(list &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ls&lt;/span&gt;())
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;# Boundary conditions&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  n &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;100&lt;/span&gt; &lt;span style=&#34;color:#888&#34;&gt;# Number of simulated meters&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  d &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;100&lt;/span&gt; &lt;span style=&#34;color:#888&#34;&gt;# Number of days to simulate&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  s &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;as.POSIXct&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;2050-01-01&amp;#34;&lt;/span&gt;, tz &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;UTC&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#888&#34;&gt;# Start of simulation&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;set.seed&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1969&lt;/span&gt;) &lt;span style=&#34;color:#888&#34;&gt;# Seed random number generator for reproducibility&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  rtu &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sample&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1E6&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2E6&lt;/span&gt;, n, replace &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;FALSE&lt;/span&gt;) &lt;span style=&#34;color:#888&#34;&gt;# 6-digit id&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  offset &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sample&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;3599&lt;/span&gt;, n, replace &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;TRUE&lt;/span&gt;) &lt;span style=&#34;color:#888&#34;&gt;# Unique Random offset for each RTU&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;# Number of occupants per connection&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  occupants &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;rpois&lt;/span&gt;(n, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1.5&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;# Visualise&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;as_tibble&lt;/span&gt;(occupants) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ggplot&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;aes&lt;/span&gt;(occupants)) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;geom_bar&lt;/span&gt;(fill &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;dodgerblue2&amp;#34;&lt;/span&gt;, alpha &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0.5&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;xlab&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Occupants&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ylab&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Connections&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;theme_bw&lt;/span&gt;(base_size &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;   
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;labs&lt;/span&gt;(title &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Occupants per connection&amp;#34;&lt;/span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/hydroinformatics/simulated-occupants.png&#34; alt=&#34;Simulated number of occupants per connection&#34; title=&#34;Simulated number of occupants per connection&#34;/&gt;
&lt;figcaption&gt;
Simulated number of occupants per connection.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
The diurnal curve is based on actual data, including leaks, as the nighttime use shows a consistent flow of about one litre per hour. Therefore, the figures are rounded and reduced by one litre per hour to show a zero flow when people are usually asleep. The curve is also shifted by eleven hours because the raw data is stored in UTC.&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;# Diurnal Curve&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  diurnal_au &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;round&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1.36&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1.085&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0.98&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1.05&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1.58&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;3.87&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;9.37&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;13.3&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;12.1&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10.3&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;8.44&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;7.04&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;6.11&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;5.68&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;5.58&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;6.67&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;8.32&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10.0&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;9.37&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;7.73&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;6.59&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;5.18&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;3.55&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2.11&lt;/span&gt;)) &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  tdiff &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;11&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  diurnal_utc &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(diurnal_au&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;[&lt;/span&gt;(tdiff &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;)&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;24&lt;/span&gt;], diurnal_au[1&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;tdiff])&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
This simulation only aims to simulate a realistic data set and not to present an accurate depiction of reality. This simulation could be enhanced by using different diurnal curves for various customer segments, including outdoor watering, temperature dependencies and so on.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-3&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-3&#34;&gt;
Simulating Water Consumption
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-3&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;A leak is defined by a constant flow through the meter and the idealised diurnal curve. A weighted binomial distribution (θ = 0.1) models approximately one in ten properties with a leak. The size of the leak is derived from a random number between 10 and 50 litres per hour.&lt;/p&gt;
&lt;p&gt;
The data is stored in a matrix through a loop that cycles through each connection. The DevEUI is repeated over the simulated period (24 times the number of days). The second variable is the timestamp plus the predetermined offset for each RTU. &lt;/p&gt;
&lt;p&gt;
The meter count is defined by the cumulative sum of the diurnal flow multiplied by the number of occupants. Each point in the diurnal deviates from the model curve by ±10%. Any predetermined leakage is added to each meter read over the whole period of 100 days. The hourly volumes are summed cumulatively to simulate meter reads. The flow is divided by five as each meter revolution indicates five litres.&lt;/p&gt;
&lt;p&gt;
The following code snippet simulates the digital metering data using the above assumptions and parameters.&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;# Leaking properties&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  leaks &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;rbinom&lt;/span&gt;(n, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;, prob &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;.1&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sample&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;50&lt;/span&gt;, n, replace &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;TRUE&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;data.frame&lt;/span&gt;(device_id &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; rtu, leak &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; leaks) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;subset&lt;/span&gt;(leak &lt;span style=&#34;color:#333&#34;&gt;&amp;gt;&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;# Digital metering data simulation&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  meter_reads &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;matrix&lt;/span&gt;(ncol &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;3&lt;/span&gt;, nrow &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;24&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; n &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; d)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;colnames&lt;/span&gt;(meter_reads) &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;device_id&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;timestamp&amp;#34;&lt;/span&gt; , &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;count&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;for&lt;/span&gt; (i &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;in&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;n) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    r &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; ((i &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;24&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; d &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;)&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;(i &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;24&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; d)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    meter_reads[r, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;rep&lt;/span&gt;(rtu[i], each &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; (&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;24&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; d))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    meter_reads[r, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;seq.POSIXt&lt;/span&gt;(s, by &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;hour&amp;#34;&lt;/span&gt;, length.out &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;24&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; d) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; offset[i]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    meter_reads[r, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;3&lt;/span&gt;] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;round&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;cumsum&lt;/span&gt;((&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;rep&lt;/span&gt;(diurnal_utc &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; occupants[i], d) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                                       leaks[i]) &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;runif&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;24&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; d, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0.9&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1.1&lt;/span&gt;)) &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;5&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  meter_reads &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;as_tibble&lt;/span&gt;(meter_reads) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;mutate&lt;/span&gt;(timestamp &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;as.POSIXct&lt;/span&gt;(timestamp, origin &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;1970-01-01&amp;#34;&lt;/span&gt;, tz &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;UTC&amp;#34;&lt;/span&gt;))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-4&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-4&#34;&gt;
Missing Data Points
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-4&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The data transmission process is not 100% reliable, and the base station will not receive some reads. This simulation identifies reads to be removed from the data through the temporary variable removal. This simulation includes two types of failures:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Faulty RTUs (2% of RTUs with missing 95% of data)&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Randomly missing data points (1% of data)&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;# Set missing indicator&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  meter_reads &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;mutate&lt;/span&gt;(meter_reads, remove &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;# Define faulty RTUs (2% of the fleet)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  faulty &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; rtu&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;[rbinom&lt;/span&gt;(n, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;, prob &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0.02&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  meter_reads&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;remove[meter_reads&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;device_id &lt;span style=&#34;color:#333&#34;&gt;%in%&lt;/span&gt; faulty] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;rbinom&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sum&lt;/span&gt;(meter_reads&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;device_id &lt;span style=&#34;color:#333&#34;&gt;%in%&lt;/span&gt; faulty), &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;, prob &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;.5&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;# Data loss&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  missing &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sample&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;nrow&lt;/span&gt;(meter_reads) &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;5&lt;/span&gt;), &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0.005&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;nrow&lt;/span&gt;(meter_reads))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;for&lt;/span&gt; (m &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;in&lt;/span&gt; missing){
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    meter_reads[m&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;(m &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sample&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;5&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;)), &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;remove&amp;#34;&lt;/span&gt;] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;# Remove missing reads&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  meter_reads &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;filter&lt;/span&gt;(meter_reads, remove &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;select&lt;/span&gt;(&lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt;remove)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;# Write to disk&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;write_csv&lt;/span&gt;(meter_reads, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;data/meter_reads.csv&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;##Visualise&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;filter&lt;/span&gt;(meter_reads, device_id &lt;span style=&#34;color:#333&#34;&gt;%in%&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sample&lt;/span&gt;(faulty, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;)) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;mutate&lt;/span&gt;(timestamp &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;as.POSIXct&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;format&lt;/span&gt;(timestamp,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                                         tz &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Australia/Melbourne&amp;#34;&lt;/span&gt;))) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;filter&lt;/span&gt;(timestamp &lt;span style=&#34;color:#333&#34;&gt;&amp;gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;as.POSIXct&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;2050-02-06&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;&amp;amp;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;           timestamp &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;as.POSIXct&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;2050-02-08&amp;#34;&lt;/span&gt;)) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;arrange&lt;/span&gt;(device_id, timestamp) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ggplot&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;aes&lt;/span&gt;(x &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; timestamp, y &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; count, colour &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;factor&lt;/span&gt;(device_id))) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;geom_line&lt;/span&gt;() &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;geom_point&lt;/span&gt;()&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The graph shows an example of the cumulative reads and some missing data points.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/hydroinformatics/simulated-missing.png&#34; alt=&#34;Simulated water consumption&#34; title=&#34;Simulated water consumption&#34;/&gt;
&lt;figcaption&gt;
Simulated water consumption with missing reads.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-5&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-5&#34;&gt;
Analysing Digital Metering Data
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-5&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;Data simulation is a good way to develop your analysis algorithms before you have real data. I have also used this technique while waiting for survey results during my dissertation. When the data finally arrived, I plugged it into the code and finetuned it. R has great capabilities to simulate reality to help you understand the data. The ggplot package provides excellent functionality to &lt;a href=&#34;https://lucidmanager.org/data-science/geographic-bubble-chart/&#34;&gt;visualise water consumption&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;
In &lt;a href=&#34;https://lucidmanager.org/data-science/analyse-digital-water-meter-data/&#34;&gt;next week&amp;#39;s article&lt;/a&gt;, I will outline how I used R and the Tidyverse package to develop libraries to analyse digital metering data.&lt;/p&gt;
&lt;p&gt;
This data was used as a case study in the book &lt;em&gt;Data Science for Water Utilities&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;

  &lt;div class=&#34;box&#34;&gt;
    &lt;div class=&#34;media&#34;&gt;
      &lt;figure class=&#34;media-left&#34;&gt;
        &lt;p class=&#34;image is-128x128&#34;&gt;
          &lt;img src=&#34;https://lucidmanager.org/images/books/2023_ds4wu.jpg&#34; alt = &#34;Data Science for Water Utilities&#34; title = &#34;Data Science for Water Utilities&#34;&gt;
        &lt;/p&gt;
      &lt;/figure&gt;
      &lt;div class=&#34;media-content&#34;&gt;
        &lt;div class=&#34;content&#34;&gt;
          &lt;p class=&#34;is-size-5 has-text-weight-bold&#34;&gt;Data Science for Water Utilities&lt;/p&gt;
          &lt;p class=&#34;mb-2&#34;&gt;Data Science for Water Utilities published by CRC Press is an applied, practical guide that shows water professionals how to use data science to solve urban water management problems using the R language for statistical computing.&lt;/p&gt;
          &lt;div class=&#34;buttons&#34;&gt;
            &lt;a class=&#34;button is-info&#34; href=&#34;https://routledge.com/9781032354545&#34; target=&#34;_blank&#34;&gt;
              &lt;span class=&#34;icon&#34;&gt;
                &lt;i class=&#34;fas fa-shopping-cart&#34;&gt;&lt;/i&gt;
              &lt;/span&gt;
              &lt;span&gt;Routledge&lt;/span&gt;
            &lt;/a&gt;
            &lt;a class=&#34;button is-info&#34; href=&#34;https://www.amazon.com/Data-Science-Water-Utilities-Chapman/dp/1032354550&#34; target=&#34;_blank&#34;&gt;
              &lt;span class=&#34;icon&#34;&gt;
                &lt;i class=&#34;fas fa-shopping-cart&#34;&gt;&lt;/i&gt;
              &lt;/span&gt;
              &lt;span&gt;Amazon&lt;/span&gt;
            &lt;/a&gt;
          &lt;/div&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://www.r-bloggers.com/&#34; target=&#34;_blank&#34; title=&#34;Proudly associated with R-Bloggers&#34;&gt;
  &lt;button class=&#34;button is-link is-medium&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-r-project&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span&gt;As seen on R Bloggers&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;

      </description>
    </item>
    
    
    
    <item>
      <title>Project Euler 32: Pandigital Products</title>
      <link>https://lucidmanager.org/data-science/project-euler-32/</link>
      <pubDate>Thu, 21 Sep 2017 00:00:00 +0000</pubDate>
      <author>Peter Prevos</author>
      <guid>https://lucidmanager.org/data-science/project-euler-32/</guid>
      <description>
	
&lt;p&gt;
Project Euler 32 returns to pandigital numbers, which are numbers that contain one of each digit. Like so many of the Euler Problems, these numbers serve no practical purpose whatsoever. Just like palindromic products, other than some entertainment value and increasing your understanding of numbers.&lt;/p&gt;
&lt;p&gt;
You can find all pandigital numbers with digits zero to nine in &lt;a href=&#34;https://oeis.org/A050278&#34;&gt;sequence A050278&lt;/a&gt; of the Online Encyclopedia of Integers. The Numberphile video below explains everything you ever wanted to know about pandigital numbers but were afraid to ask.&lt;/p&gt;
&lt;figure&gt;
&lt;div style=&#34;position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden;&#34;&gt;
      &lt;iframe allow=&#34;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share; fullscreen&#34; loading=&#34;eager&#34; referrerpolicy=&#34;strict-origin-when-cross-origin&#34; src=&#34;https://www.youtube.com/embed/gaVMrqzb91w?autoplay=0&amp;amp;controls=1&amp;amp;end=0&amp;amp;loop=0&amp;amp;mute=0&amp;amp;start=0&#34; style=&#34;position: absolute; top: 0; left: 0; width: 100%; height: 100%; border:0;&#34; title=&#34;YouTube video&#34;&gt;&lt;/iframe&gt;
    &lt;/div&gt;

&lt;figcaption&gt;
Why 381,654,729 is awesome – Numberphile.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;div id=&#34;outline-container-headline-1&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-1&#34;&gt;
Project Euler 32 Definition
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-1&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once; for example, the 5-digit number, 15234, is 1 through 5 pandigital.&lt;/p&gt;
&lt;p&gt;
The product 7254 is unusual, as the identity, 39 × 186 = 7254, containing multiplicand, multiplier, and product is 1 through 9 pandigital.&lt;/p&gt;
&lt;p&gt;
Find the sum of all products whose multiplicand/multiplier/product identity can be written as a 1 through 9 pandigital.&lt;/p&gt;
&lt;p&gt;
HINT: Some products can be obtained in more than one way, so be sure to only include it once in your sum.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-2&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-2&#34;&gt;
Proposed Solution
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-2&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The &lt;code class=&#34;verbatim&#34;&gt;pandigital.9()&lt;/code&gt; function tests whether a string classifies as a pandigital number. The &lt;code class=&#34;verbatim&#34;&gt;pandigital.prod&lt;/code&gt; vector stores the multiplication.&lt;/p&gt;
&lt;p&gt;
The only way to solve this problem is by brute force and try all multiplications. We can limit the solution space to a manageable number. The multiplication result needs to have ten digits. For example, when the starting number has two digits, the second number should have three digits so that the total has four digits, e.g., 39 × 186 = 7254. When the first number only has one digit, the second number needs to have four digits.&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://github.com/pprevos/ProjectEuler/&#34; target=&#34;_blank&#34;
   title=&#34;Download ProjectEuler from GitHub&#34;
   alt=&#34;Download ProjectEuler from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;ProjectEuler&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Project Euler 32: Pandigital Products&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  pandigital.9 &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;function&lt;/span&gt;(x) &lt;span style=&#34;color:#888&#34;&gt;# Test if string is 9-pandigital&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      (&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;length&lt;/span&gt;(x)&lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;9&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;&amp;amp;&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sum&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;duplicated&lt;/span&gt;(x))&lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;&amp;amp;&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sum&lt;/span&gt;(x&lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;)&lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Brute force&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  pandigital.prod &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;vector&lt;/span&gt;()
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  i &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;for&lt;/span&gt; (m &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;in&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;100&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (m &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt;) 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      n_start &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1234&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;else&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      n_start &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;123&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;for&lt;/span&gt; (n &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;in&lt;/span&gt; n_start&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;round&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10000&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt; m)) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                                          &lt;span style=&#34;color:#888&#34;&gt;# List of digits&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      digs &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;as.numeric&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;unlist&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;strsplit&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;paste0&lt;/span&gt;(m, n, m &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; n), &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;&amp;#34;&lt;/span&gt;)))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                                          &lt;span style=&#34;color:#888&#34;&gt;# is Pandigital?&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;pandigital.9&lt;/span&gt;(digs)) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        pandigital.prod[i] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; m &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; n
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        i &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; i &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;print&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;paste&lt;/span&gt;(m, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;*&amp;#34;&lt;/span&gt;, n, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;=&amp;#34;&lt;/span&gt;, m &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; n))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  answer &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sum&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;unique&lt;/span&gt;(pandigital.prod))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;print&lt;/span&gt;(answer)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;a href = &#34;https://www.r-bloggers.com/&#34; target=&#34;_blank&#34; title=&#34;Proudly associated with R-Bloggers&#34;&gt;
  &lt;button class=&#34;button is-link is-medium&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-r-project&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span&gt;As seen on R Bloggers&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/div&gt;
&lt;/div&gt;

      </description>
    </item>
    
    
    
    <item>
      <title>Analysing soil moisture data with the ncdf4 library</title>
      <link>https://lucidmanager.org/data-science/ncdf4-library/</link>
      <pubDate>Thu, 07 Sep 2017 00:00:00 +0000</pubDate>
      <author>Peter Prevos</author>
      <guid>https://lucidmanager.org/data-science/ncdf4-library/</guid>
      <description>
	
&lt;p&gt;
The &lt;a href=&#34;https://www.unidata.ucar.edu/software/netcdf/&#34;&gt;netCDF&lt;/a&gt; format is popular in sciences that analyse sequential spatial data. It is a self-describing and machine-independent data format for creating, accessing and sharing array-oriented information. The netCDF format provides spatial time series such as meteorological or environmental data. This article shows how to load this data type with the ncdf4 library. This article also shows how to visualise and analyse this data format by reviewing soil moisture data published by the Australian Bureau of Meteorology.&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-1&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-1&#34;&gt;
Soil Moisture data
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-1&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The &lt;a href=&#34;https://awo.bom.gov.au/products/historical/soilMoisture-rootZone/7,-37.405,144.635/nat,-25.609,134.362/r/d/2025-06-22&#34;&gt;Australian Bureau of Meteorology&lt;/a&gt; publishes hydrological data in the NetCDF format on their &lt;em&gt;Australian Water Outlook&lt;/em&gt;. The NetCDF format provides a three-dimensional matrix of spatial data over time. You need to download the monthly time aggregation, which is approximately 1GB large.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/hydroinformatics/soil-moisture.jpg&#34; alt=&#34;Australian Landscape Water Balance (Bureau of Meteorology)&#34; title=&#34;Australian Landscape Water Balance (Bureau of Meteorology)&#34; width=&#34;300&#34;/&gt;
&lt;figcaption&gt;
Australian Landscape Water Balance (Bureau of Meteorology).
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-2&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-2&#34;&gt;
Reading, Extracting and Transforming with the ncdf4 library
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-2&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The &lt;a href=&#34;https://cran.r-project.org/web/packages/ncdf4/index.html&#34;&gt;ncdf4 library&lt;/a&gt;, developed by David W. Pierce, provides the necessary functionality to manage this data. To install this R library on a non-Windows system, you must first install the NetCDF package for your relevant operating system. &lt;/p&gt;
&lt;p&gt;
The first step is to load the data, extract the relevant information and transform the data for visualisation and analysis. The data forms a complex list containing the metadata and measurements. The &lt;code class=&#34;verbatim&#34;&gt;ncvar_get()&lt;/code&gt; function extracts  data from the list.&lt;/p&gt;
&lt;p&gt;
The time data is stored as the number of days since 1 January 1900. The spatial coordinates are stored in decimal degrees with 0.05-decimal degree intervals. The moisture data is transformed to a three-dimensional matrix with dimensions of longitude, latitude, and time.&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://github.com/pprevos/r4h2o/&#34; target=&#34;_blank&#34;
   title=&#34;Download r4h2o from GitHub&#34;
   alt=&#34;Download r4h2o from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;r4h2o&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Analysing soil moisture data with the ncdf4 library&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Reading, Extracting and Transforming with the ncdf4 library&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(ncdf4)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  bom &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;nc_open&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;data/sm_pct.nc&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Generate matrix&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  longitude &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ncvar_get&lt;/span&gt;(bom, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;longitude&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  latitude &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ncvar_get&lt;/span&gt;(bom, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;latitude&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  month &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;as.Date&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;1900-01-01&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ncvar_get&lt;/span&gt;(bom, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;time&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  moisture &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ncvar_get&lt;/span&gt;(bom, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;sm_pct&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;dimnames&lt;/span&gt;(moisture) &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;list&lt;/span&gt;(longitude, latitude, month)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-3&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-3&#34;&gt;
Visualising the data with ggplot
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-3&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;This first code snippet extracts and plots a matrix from the cube for 31 May 2025. This code creates a data frame with the grid for the required date. Although I use the Tidyverse, I still need reshape2 because the &lt;code&gt;pivot_longer&lt;/code&gt; function does not like matrices.&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Visualising the data with ggplot&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(ggplot2)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(dplyr)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(reshape2)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(ozmaps)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  plot_date &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;2025-05-31&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Generate data frame&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  soil_moisture &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; moisture[, , &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;which&lt;/span&gt;(month &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; plot_date)] &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;melt&lt;/span&gt;(varnames &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;longitude&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;latitude&amp;#34;&lt;/span&gt;)) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;subset&lt;/span&gt;(&lt;span style=&#34;color:#333&#34;&gt;!&lt;/span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;is.na&lt;/span&gt;(value))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## State boundaries&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  sf_aus &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ozmap&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;states&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ggplot&lt;/span&gt;(soil_moisture) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;geom_tile&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;aes&lt;/span&gt;(x &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; longitude, y &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; latitude, fill &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; value)) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;scale_fill_gradient&lt;/span&gt;(low &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;chocolate4&amp;#34;&lt;/span&gt;, high &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;dodgerblue&amp;#34;&lt;/span&gt;, name &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;NULL&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;geom_sf&lt;/span&gt;(data &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; sf_aus, fill &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;NA&lt;/span&gt;, col &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;white&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;labs&lt;/span&gt;(title &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Relative Total rootzone soil moisture (0-100 cm)&amp;#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;         subtitle &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;format&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;as.Date&lt;/span&gt;(plot_date), &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;%d %B %Y&amp;#34;&lt;/span&gt;),
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;         caption &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Source: Australian Landscape Water Balance AWRA-L Model&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;theme_void&lt;/span&gt;()&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/hydroinformatics/soil-moisture-australia.jpg&#34; alt=&#34;Relative soil moisture in Australia, May 2025&#34; title=&#34;Relative soil moisture in Australia, May 2025&#34;/&gt;
&lt;figcaption&gt;
Relative soil moisture in Australia, May 2025.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
With the ggmap package we can add some mapping tiles. You will need a &lt;a href=&#34;https://lucidmanager.org/data-science/geocoding-with-ggmap/&#34;&gt;Google Maps API&lt;/a&gt; to enable this functionality.&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Plot on Google Maps&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(ggmap)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  api &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;readLines&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;case-studies/google-maps.api&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#888&#34;&gt;# Text file with the API key&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;register_google&lt;/span&gt;(key &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; api)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ggmap&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;get_map&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Australia&amp;#34;&lt;/span&gt;, zoom &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;4&lt;/span&gt;, color &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;bw&amp;#34;&lt;/span&gt;)) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;geom_tile&lt;/span&gt;(data &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; soil_moisture, 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;              &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;aes&lt;/span&gt;(x &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; longitude, y &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; latitude, fill &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; value), alpha &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0.5&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;scale_fill_gradient&lt;/span&gt;(low &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;chocolate4&amp;#34;&lt;/span&gt;, high &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;dodgerblue&amp;#34;&lt;/span&gt;, name &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;NULL&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;labs&lt;/span&gt;(title &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Relative Total rootzone soil moisture (0-100 cm)&amp;#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;         subtitle &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;format&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;as.Date&lt;/span&gt;(plot_date), &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;%d %B %Y&amp;#34;&lt;/span&gt;),
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;         caption &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Source: Australian Landscape Water Balance AWRA-L Model&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;theme_void&lt;/span&gt;()&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/hydroinformatics/soil-moisture-google-maps.jpg&#34; alt=&#34;Relative soil moisture in Australia, May 2025&#34; title=&#34;Relative soil moisture in Australia, May 2025&#34; width=&#34;500&#34;/&gt;
&lt;figcaption&gt;
Relative soil moisture in Australia, May 2025.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-4&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-4&#34;&gt;
Analysing the data
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-4&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;For my analysis, I am interested in the time series of moisture data for a specific point on the map. The previous code slices the data for all location at a specific month.&lt;/p&gt;
&lt;p&gt;
We can also pierce the data vertically for a particular coordinate to create a time series. The coordinate needs to be rounded to  the nearest 0.05 decimal degrees to match the source data.&lt;/p&gt;
&lt;p&gt;
This time series was used to investigate the relationship between sewer main blockages and deep soil data, which can be a topic for a future post.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/hydroinformatics/soil-moisture-time-series.png&#34; alt=&#34;Soil moisture time series for Bendigo&#34; title=&#34;Soil moisture time series for Bendigo&#34;/&gt;
&lt;figcaption&gt;
Soil moisture time series for Bendigo.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Time series&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  location &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;round&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;geocode&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Bendigo, Australia&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0.05&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0.05&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  soil_moisture_time &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;tibble&lt;/span&gt;(month,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                               moisture &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; moisture&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;[as.character&lt;/span&gt;(location&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;lon), 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                                                   &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;as.character&lt;/span&gt;(location&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;lat), ]) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;filter&lt;/span&gt;(month &lt;span style=&#34;color:#333&#34;&gt;&amp;gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;2000-01-01&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ggplot&lt;/span&gt;(soil_moisture_time) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;geom_line&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;aes&lt;/span&gt;(x &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; month, y &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; moisture)) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;labs&lt;/span&gt;(title &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Relative Total rootzone soil moisture (0-100 cm)&amp;#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;         subtitle &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;paste&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;as.character&lt;/span&gt;(location), collapse &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;, &amp;#34;&lt;/span&gt;),
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;         caption &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Source: Australian Landscape Water Balance AWRA-L Model&amp;#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;         x &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;NULL&lt;/span&gt;, y &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Relative Soil Moisture&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;theme_minimal&lt;/span&gt;(base_size &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;16&lt;/span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;

  &lt;div class=&#34;box&#34;&gt;
    &lt;div class=&#34;media&#34;&gt;
      &lt;figure class=&#34;media-left&#34;&gt;
        &lt;p class=&#34;image is-128x128&#34;&gt;
          &lt;img src=&#34;https://lucidmanager.org/images/books/2023_ds4wu.jpg&#34; alt = &#34;Data Science for Water Utilities&#34; title = &#34;Data Science for Water Utilities&#34;&gt;
        &lt;/p&gt;
      &lt;/figure&gt;
      &lt;div class=&#34;media-content&#34;&gt;
        &lt;div class=&#34;content&#34;&gt;
          &lt;p class=&#34;is-size-5 has-text-weight-bold&#34;&gt;Data Science for Water Utilities&lt;/p&gt;
          &lt;p class=&#34;mb-2&#34;&gt;Data Science for Water Utilities published by CRC Press is an applied, practical guide that shows water professionals how to use data science to solve urban water management problems using the R language for statistical computing.&lt;/p&gt;
          &lt;div class=&#34;buttons&#34;&gt;
            &lt;a class=&#34;button is-info&#34; href=&#34;https://routledge.com/9781032354545&#34; target=&#34;_blank&#34;&gt;
              &lt;span class=&#34;icon&#34;&gt;
                &lt;i class=&#34;fas fa-shopping-cart&#34;&gt;&lt;/i&gt;
              &lt;/span&gt;
              &lt;span&gt;Routledge&lt;/span&gt;
            &lt;/a&gt;
            &lt;a class=&#34;button is-info&#34; href=&#34;https://www.amazon.com/Data-Science-Water-Utilities-Chapman/dp/1032354550&#34; target=&#34;_blank&#34;&gt;
              &lt;span class=&#34;icon&#34;&gt;
                &lt;i class=&#34;fas fa-shopping-cart&#34;&gt;&lt;/i&gt;
              &lt;/span&gt;
              &lt;span&gt;Amazon&lt;/span&gt;
            &lt;/a&gt;
          &lt;/div&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://www.r-bloggers.com/&#34; target=&#34;_blank&#34; title=&#34;Proudly associated with R-Bloggers&#34;&gt;
  &lt;button class=&#34;button is-link is-medium&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-r-project&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span&gt;As seen on R Bloggers&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;

      </description>
    </item>
    
    
    
    <item>
      <title>Pacific Island Hopping using R and the iGraph package</title>
      <link>https://lucidmanager.org/data-science/pacific-island-hopping/</link>
      <pubDate>Thu, 31 Aug 2017 10:00:56 +0000</pubDate>
      <author>Peter Prevos</author>
      <guid>https://lucidmanager.org/data-science/pacific-island-hopping/</guid>
      <description>
	
&lt;p&gt;
Last month I enjoyed a relaxing holiday in the &lt;a href=&#34;https://horizonofreason.com/culture/vanuatu-sand-drawings-sandroing/&#34;&gt;tropical paradise of Vanuatu&lt;/a&gt;. One rainy day I contemplated how to go island hopping across the Pacific ocean visiting as many island nations as possible. The Pacific ocean is a massive body of water between, Asia and the Americas, which covers almost half the surface of the earth. The southern Pacific is strewn with island nations from Australia to Chile. In this post, I describe how to use R to plan your next Pacific island
hopping journey.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/geography/pacific-ocean.png&#34; alt=&#34;The Pacific Ocean&#34; title=&#34;The Pacific Ocean&#34;/&gt;
&lt;figcaption&gt;
The Pacific Ocean with UN Geoscheme regions (source: Oceania_ISO_3166-1.svg: User:Tintazulderivative work: Cruickshanks / &lt;a href=&#34;https://creativecommons.org/licenses/by-sa/3.0&#34;&gt;CC BY-SA&lt;/a&gt;).
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;div id=&#34;outline-container-headline-1&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-1&#34;&gt;
Geocoding the airports
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-1&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;My first step was to create a list of flight connections between each of the island nations in the Pacific ocean. I am not aware of a publicly available data set of international flights so unfortunately, I created the list manually by collecting route maps on &lt;em&gt;Pinterest&lt;/em&gt; (if you do know of a fomal data set with international flights, then please leave a comment).&lt;/p&gt;
&lt;p&gt;
My manual research resulted in a list of international flights from or to island airports. This list might not be complete, but it is a start. My &lt;a href=&#34;https://www.pinterest.com.au/HorizonOfReason/pacific-island-hopping/&#34;&gt;Pinterest board with Pacific island airline route maps&lt;/a&gt; was the information source for this list.&lt;/p&gt;
&lt;p&gt;
The first code section reads the list of airline routes and uses the ggmap package to extract their coordinates from Google maps. You will need a Google API to enable the &lt;a href=&#34;https://lucidmanager.org/data-science/geocoding-with-ggmap/&#34;&gt;geocoding function&lt;/a&gt;. The data frame with airport coordinates is saved for future reference to avoid repeatedly pinging Google for the same information.&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://github.com/pprevos/geography/&#34; target=&#34;_blank&#34;
   title=&#34;Download geography from GitHub&#34;
   alt=&#34;Download geography from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;geography&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(ggmap)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(ggplot2)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(ggrepel)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(geosphere)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(tidyverse)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(stringr)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Google maps api&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  api &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;readLines&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;google.api&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#888&#34;&gt;# Text file with the API key&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;register_google&lt;/span&gt;(key &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; api)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Read flight list and airport list&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  flights &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;read_csv&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;pacific/pacific-flights.csv&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  f &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;pacific/pacific-airports.csv&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;file.exists&lt;/span&gt;(f)) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    airports &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;read.csv&lt;/span&gt;(f)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  } &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;else&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    airports &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;data.frame&lt;/span&gt;(airport &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;NA&lt;/span&gt;, lat &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;NA&lt;/span&gt;, lon &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;NA&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Lookup coordinates for new airports&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  all_airports &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;unique&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;paste&lt;/span&gt;(flights&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;From, flights&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;From.Country, sep &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;, &amp;#34;&lt;/span&gt;),
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                           &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;paste&lt;/span&gt;(flights&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;To, flights&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;To.Country, sep &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;, &amp;#34;&lt;/span&gt;)))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  new_airports &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; all_airports[&lt;span style=&#34;color:#333&#34;&gt;!&lt;/span&gt;(all_airports &lt;span style=&#34;color:#333&#34;&gt;%in%&lt;/span&gt; airports&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;airport)]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;while&lt;/span&gt; (&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;length&lt;/span&gt;(new_airports) &lt;span style=&#34;color:#333&#34;&gt;!=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    coords &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;geocode&lt;/span&gt;(new_airports)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    temp_airports &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;data.frame&lt;/span&gt;(airport &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; new_airports, coords)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    airports &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;rbind&lt;/span&gt;(airports, temp_airports) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;filter&lt;/span&gt;(&lt;span style=&#34;color:#333&#34;&gt;!&lt;/span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;is.na&lt;/span&gt;(lat), &lt;span style=&#34;color:#333&#34;&gt;!&lt;/span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;is.na&lt;/span&gt;(lon))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    new_airports &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; all_airports[&lt;span style=&#34;color:#333&#34;&gt;!&lt;/span&gt;(all_airports &lt;span style=&#34;color:#333&#34;&gt;%in%&lt;/span&gt; airports&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;airport)]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Remove countries and save&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  airports&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;airport &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;str_remove_all&lt;/span&gt;(airports&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;airport, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;,.*&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;write_csv&lt;/span&gt;(airports, f)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Add coordinates to flight list&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  flights &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;merge&lt;/span&gt;(flights, airports, by.x &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;From&amp;#34;&lt;/span&gt;, by.y &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;airport&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  flights &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;merge&lt;/span&gt;(flights, airports, by.x &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;To&amp;#34;&lt;/span&gt;, by.y &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;airport&amp;#34;&lt;/span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-2&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-2&#34;&gt;
Create the map
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-2&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;To create a map, I modified the code to create &lt;a href=&#34;https://lucidmanager.org/data-science/create-air-travel-route-maps/&#34;&gt;flight maps&lt;/a&gt; I published in an earlier article. This code had to be changed to centre the map on the Pacific.&lt;/p&gt;
&lt;p&gt;
Mapping the Pacific ocean is problematic because the -180 and +180 degree meridians meet around the dateline. Longitudes west of the antemeridian are positive, while longitudes east are negative. The &lt;code class=&#34;verbatim&#34;&gt;world2&lt;/code&gt; data set in the borders function of the ggplot2 package is centred on the Pacific ocean. To enable plotting on this map, all negative longitudes are made positive by adding 360 degrees to them and &lt;a href=&#34;https://lucidmanager.org/data-science/mapping-antipodes/&#34;&gt;defining&lt;/a&gt; the antipode.&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Pacific centric&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  flights&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;lon.x[flights&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;lon.x &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; flights&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;lon.x[flights&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;lon.x &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;] &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;360&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  flights&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;lon.y[flights&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;lon.y &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; flights&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;lon.y[flights&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;lon.y &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;] &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;360&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  airports&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;lon[airports&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;lon &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; airports&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;lon[airports&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;lon &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;] &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;360&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Plot flight routes&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  worldmap &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;borders&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;world2&amp;#34;&lt;/span&gt;, fill &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;grey&amp;#34;&lt;/span&gt;, col &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;NA&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ggplot&lt;/span&gt;() &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; worldmap &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;geom_point&lt;/span&gt;(data &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; airports, &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;aes&lt;/span&gt;(x &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; lon, y &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; lat), col &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;#970027&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;geom_text_repel&lt;/span&gt;(data &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; airports, &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;aes&lt;/span&gt;(x &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; lon, y &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; lat, label &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; airport), col &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;black&amp;#34;&lt;/span&gt;, size &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                    segment.color &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;NA&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;geom_curve&lt;/span&gt;(data &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; flights, &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;aes&lt;/span&gt;(x &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; lon.x, y &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; lat.x, xend &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; lon.y, yend &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; lat.y, col &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; Airline), size &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;               curvature &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;.2&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;xlim&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;90&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;300&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ylim&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;-50&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;50&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;theme_void&lt;/span&gt;() &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;labs&lt;/span&gt;(title &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Pacific Island Hopping&amp;#34;&lt;/span&gt;, subtitle &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;lucidmanager.org&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ggsave&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;pacific-flights.png&amp;#34;&lt;/span&gt;, width &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;6&lt;/span&gt;, height &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;4&lt;/span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/geography/pacific-flights.png&#34; alt=&#34;Pacific island hopping flight route map&#34; title=&#34;Pacific island hopping flight route map&#34;/&gt;
&lt;figcaption&gt;
Pacific island hopping - flight route map.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-3&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-3&#34;&gt;
Pacific Island Hopping
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-3&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;This visualisation is aesthetic and full of context, but it is not the best visualisation to solve the travel problem. This map can also be expressed as a graph with nodes (airports) and edges (routes). Once the map is represented mathematically, we can generate travel routes and begin our Pacific Island hopping.&lt;/p&gt;
&lt;p&gt;
The igraph package converts the flight list to a graph that can be analysed and plotted. The &lt;code class=&#34;verbatim&#34;&gt;shortest_path&lt;/code&gt; function can then be used to plan routes. If I would want to travel from Auckland to Saipan in the Northern Mariana Islands, I have to go through Port Vila, Honiara, Port Moresby, Chuuk, Guam and then to Saipan. I am pretty sure there are quicker ways to get there, but this would be an exciting journey through the Pacific.&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(igraph)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  g &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;graph_from_edgelist&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;as.matrix&lt;/span&gt;(flights[,&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;]), directed &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;FALSE&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;png&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;pacific-network.png&amp;#34;&lt;/span&gt;, width &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;600&lt;/span&gt;, height &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;600&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;par&lt;/span&gt;(mar &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;rep&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;4&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;plot&lt;/span&gt;(g, layout &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; layout.fruchterman.reingold,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;       vertex.size &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;       vertex.label.cex &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1.2&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;       edge.width &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;dev.off&lt;/span&gt;()
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;shortest_paths&lt;/span&gt;(g, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Auckland&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Saipan&amp;#34;&lt;/span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/geography/pacific-network.png&#34; alt=&#34;Pacific island hopping - network model&#34; title=&#34;Pacific island hopping - network model&#34;/&gt;
&lt;figcaption&gt;
Pacific island hopping - network model.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
This diagram looks a lot like a &lt;a href=&#34;http://www.thenonist.com/index.php/thenonist/permalink/stick_charts/&#34;&gt;Polynesian stick chart&lt;/a&gt;. These are maps made from sticks bound together with strings. These maps allowed the per-colonial Polynesians to navigate between their islands. The maps are not mathematical projections of the earth, like our first map, but a logical model.&lt;/p&gt;
&lt;p&gt;
Stick charts indicate the local swell patterns that allowed the ancient mariners to find their way across the vast distances in the pacific. All we have to do is hop on a plane and we get to wherever we want withing a few hours.&lt;/p&gt;
&lt;p&gt;
Happy island hopping!&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://www.r-bloggers.com/&#34; target=&#34;_blank&#34; title=&#34;Proudly associated with R-Bloggers&#34;&gt;
  &lt;button class=&#34;button is-link is-medium&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-r-project&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span&gt;As seen on R Bloggers&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;

      </description>
    </item>
    
    
    
    <item>
      <title>Project Euler 30: Digit Fifth Powers</title>
      <link>https://lucidmanager.org/data-science/project-euler-30/</link>
      <pubDate>Thu, 24 Aug 2017 00:00:00 +0000</pubDate>
      <author>Peter Prevos</author>
      <guid>https://lucidmanager.org/data-science/project-euler-30/</guid>
      <description>
	
&lt;p&gt;
Project Euler 30 is another number-crunching problem that deals with numbers to the power of five. Two other Euler problems dealt with raising numbers to a power. The &lt;a href=&#34;https://lucidmanager.org/data-science/project-euler-29/&#34;&gt;previous problem&lt;/a&gt; looked at permutations of powers, and &lt;a href=&#34;https://lucidmanager.org/data-science/project-euler-16/&#34;&gt;problem 16&lt;/a&gt; asks for the sum of the digits of \(2^{1000}\).&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-1&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-1&#34;&gt;
Project Euler 30 Definition
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-1&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;Surprisingly there are only three numbers that can be written as the sum of fourth powers of their digits:&lt;/p&gt;
&lt;p&gt;
$$1634 = 1^4 + 6^4 + 3^4 + 4^4$$&lt;/p&gt;
&lt;p&gt;
$$8208 = 8^4 + 2^4 + 0^4 + 8^4$$&lt;/p&gt;
&lt;p&gt;
$$9474 = 9^4 + 4^4 + 7^4 + 4^4$$&lt;/p&gt;
&lt;p&gt;
As \(1 = 1^4\) is not a sum, it is not included.&lt;/p&gt;
&lt;p&gt;
The sum of these numbers is \(1634 + 8208 + 9474 = 19316\). Find the sum of all the numbers that can be written as the sum of the fifth powers of their digits.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-2&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-2&#34;&gt;
Proposed Solution
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-2&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The problem asks for a brute-force solution, but we have a halting problem. How far do we need to go before we can be sure there are no sums of fifth power digits? Do we need to search until infinity, which means we can never solve the problem? We can limit the solution space because the solution will have lower and upper limits. An upper bound is a number with \(log 9^5\) digits.&lt;/p&gt;
&lt;p&gt;
In base ten, the highest digit is 9 and \(9^5=59049\), which has five digits. This implies that the highest possible number, which equals the sum of the fifth power of its digits is 5 times \(9^5=295245\). The lowest possible number is 2 times \(2^5 = 64\). All numbers with this property thus must range between these two values.&lt;/p&gt;
&lt;p&gt;
The code to solve this project first declares the exponent and determines the lower and upper boundaries of the solution space. Change the variable n to try other exponents. The loop cycles through the digits of each number and tests whether the sum of the fifth powers equals the number. &lt;/p&gt;
&lt;p&gt;
The digit sum of a number \(x\) in base 10 is given by:&lt;/p&gt;
&lt;p&gt;
$$\sum_{n=0}^{\lfloor \log_{10} x\rfloor} \frac{1}{10^n}(x \bmod 10^{n + 1} - x \bmod 10^n)$$&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://github.com/pprevos/ProjectEuler/&#34; target=&#34;_blank&#34;
   title=&#34;Download ProjectEuler from GitHub&#34;
   alt=&#34;Download ProjectEuler from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;ProjectEuler&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Project Euler 30: Digit Fifth Powers&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  m &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;5&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  highest &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;round&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;log10&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;9&lt;/span&gt;^m)) &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;9&lt;/span&gt;^m
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  answer &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;for&lt;/span&gt; (x &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;in&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;highest) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      n &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;log10&lt;/span&gt;(x)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      power.sum &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sum&lt;/span&gt;(((&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt;^n) &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; (x &lt;span style=&#34;color:#333&#34;&gt;%%&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;^&lt;/span&gt;(n &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; x &lt;span style=&#34;color:#333&#34;&gt;%%&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt;^n))^m)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (power.sum &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; x) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;print&lt;/span&gt;(x)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          answer &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; answer &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; x
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;print&lt;/span&gt;(answer)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-3&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-3&#34;&gt;
Digit sums
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-3&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The mathematics in this problem is about the digit sum, which is the sum of all digits in the relevant number base.&lt;/p&gt;
&lt;p&gt;
Computer scientists use digit sums in cryptography and data validation. You can find the digital sum base-10 numbers in &lt;a href=&#34;http://oeis.org/A007953&#34;&gt;sequence A007953&lt;/a&gt; of the Online Encyclopedia of Integer Sequences. Checksum algorithms use digit sums as a quality assurance method. For example, the final digit in the ten-digit International Standard Book Numbers is a check digit. You can compute the check digit by multiplying each digit by its position in the ISBN number (alternating by 3 and 1), and taking the sum of these products. The check digit is 0 when the modules 10 of this modulus is zero, otherwise it is 10 minus the remainder. In R code, determining the check digit for a 13-digit ISBN number works as follows:&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Check ISB number&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  check_isbn13 &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;function&lt;/span&gt;(isbn) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      isbn &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;gsub&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34; |-&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;&amp;#34;&lt;/span&gt;, isbn)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      check_sum &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;for&lt;/span&gt; (i &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;in&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;12&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          m &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ifelse&lt;/span&gt;(i &lt;span style=&#34;color:#333&#34;&gt;%%&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;3&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          check_sum &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; check_sum &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; m &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;as.numeric&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;substr&lt;/span&gt;(isbn, i, i))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      check_sum &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ifelse&lt;/span&gt;(check_sum &lt;span style=&#34;color:#333&#34;&gt;%%&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; check_sum &lt;span style=&#34;color:#333&#34;&gt;%%&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      check_sum &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;substr&lt;/span&gt;(isbn, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;13&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;13&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;check_isbn13&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;978-0-9875669-4-2&amp;#34;&lt;/span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;a href = &#34;https://www.r-bloggers.com/&#34; target=&#34;_blank&#34; title=&#34;Proudly associated with R-Bloggers&#34;&gt;
  &lt;button class=&#34;button is-link is-medium&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-r-project&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span&gt;As seen on R Bloggers&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/div&gt;
&lt;/div&gt;

      </description>
    </item>
    
    
    
    <item>
      <title>Visualise Water Consumption with a Geographic Bubble Chart</title>
      <link>https://lucidmanager.org/data-science/geographic-bubble-chart/</link>
      <pubDate>Thu, 17 Aug 2017 00:00:00 +0000</pubDate>
      <author>Peter Prevos</author>
      <guid>https://lucidmanager.org/data-science/geographic-bubble-chart/</guid>
      <description>
	
&lt;p&gt;
A geographic bubble chart is a straightforward method to visualise quantitative information with a geospatial relationship. Last week, I was in Vietnam helping the Phú Thọ Water Supply Joint Stock Company with their data science. They asked me to create a map of a sample of their water consumption data. In this post, I share this little ditty to explain how to plot a bubble chart over a map using the ggmap package. &lt;/p&gt;
&lt;p&gt;
In this post, I share this little ditty to explain how to plot a bubble chart over a map using the ggmap package. Thanks to Ms Quy and Mr Tuyen of Phu Tho Water for their permission to use this data. Other posts on this blog detail how to analyse water consumption from &lt;a href=&#34;https://lucidmanager.org/data-science/analyse-digital-water-meter-data/&#34;&gt;digital metering data&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;
This map visualises water consumption in the targeted area of Việt Trì. The larger the bubble, the larger the consumption. It is no surprise that two commercial customers used the most water.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/hydroinformatics/vietwater.jpg&#34; alt=&#34;Viet Water conference 2018&#34; title=&#34;Viet Water conference 2018&#34;/&gt;
&lt;figcaption&gt;
Viet Water Conference 2018.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;div id=&#34;outline-container-headline-1&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-1&#34;&gt;
Load and Explore the Data
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-1&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The sample data contains a list of just over 100 readings from water meters in the city of Việt Trì, Vietnam, along with their geospatial locations. This data uses the World Geodetic System of 1984 (WGS84), compatible with Google Maps and similar systems. &lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://github.com/pprevos/r4h2o/&#34; target=&#34;_blank&#34;
   title=&#34;Download r4h2o from GitHub&#34;
   alt=&#34;Download r4h2o from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;r4h2o&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;# Water consumption bubble chart&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  water &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;read.csv&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;data/viet-tri-consumption.csv&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  water&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;Consumption &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; water&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;read_new &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; water&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;read_old
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Summarise the data&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;summary&lt;/span&gt;(water&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;Consumption)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
Consumption per connection ranges from 0 to 529 cubic metres, with an average of 23.45 cubic metres.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-2&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-2&#34;&gt;
Visualise the data with a geographic bubble chart
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-2&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;With the &lt;a href=&#34;https://r-graph-gallery.com/324-map-background-with-the-ggmap-library.html&#34;&gt;ggmap&lt;/a&gt; extension of the ggplot package, we can visualise any spatial data set on a map. The only condition is that the spatial coordinates are in the WGS84 datum. The ggmap package adds a geographical layer to ggplot by providing a Google Maps or OpenStreetMap canvas. The first step is to download the map canvas. You need to know the centre coordinates and the zoom factor to do this. Determining the perfect zoon factor requires some trial and error. The ggmap package provides for various map types, which are described in detail in the &lt;a href=&#34;https://cran.r-project.org/web/packages/ggmap/ggmap.pdf&#34;&gt;documentation&lt;/a&gt;. You will need a &lt;a href=&#34;https://lucidmanager.org/data-science/geocoding-with-ggmap/&#34;&gt;Google Maps API&lt;/a&gt; to enable this functionality.&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Plot on Google Maps&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(ggmap)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  api &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;readLines&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;data/google-maps.api&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#888&#34;&gt;# Text file with the API key&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;register_google&lt;/span&gt;(key &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; api)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  centre &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;mean&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;range&lt;/span&gt;(water&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;lon)), &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;mean&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;range&lt;/span&gt;(water&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;lat)))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  viettri &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;get_map&lt;/span&gt;(centre, zoom &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;17&lt;/span&gt;, maptype &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;hybrid&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  g &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ggmap&lt;/span&gt;(viettri)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
The ggmap package follows the same conventions as ggplot. We first call the map layer and then add any required geom. The point geom creates a nice bubble chart when combined with the &lt;code class=&#34;verbatim&#34;&gt;scale_size_area option&lt;/code&gt;. This option scales the points to a maximum size to ensure they are easily visible. The transparency (alpha) minimises the problem of overplotting. This last code snippet plots the map with water consumption. &lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/hydroinformatics/viet-tri-consumption.jpg&#34; alt=&#34;Water consumption in  Việt Trì&#34; title=&#34;Water consumption in  Việt Trì.&#34;/&gt;
&lt;figcaption&gt;
Water consumption in  Việt Trì.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;# Add data to ggmap  &lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  g &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;geom_point&lt;/span&gt;(data &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; water, &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;aes&lt;/span&gt;(x &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; lon, y &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; lat, size &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; Consumption),
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                 shape &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;21&lt;/span&gt;, colour &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;dodgerblue4&amp;#34;&lt;/span&gt;, fill &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;dodgerblue&amp;#34;&lt;/span&gt;, 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                 alpha &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;.8&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;scale_size_area&lt;/span&gt;(max_size &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;30&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#888&#34;&gt;# Size of the biggest point&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ggtitle&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Việt Trì sự tiêu thụ nước&amp;#34;&lt;/span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;

  &lt;div class=&#34;box&#34;&gt;
    &lt;div class=&#34;media&#34;&gt;
      &lt;figure class=&#34;media-left&#34;&gt;
        &lt;p class=&#34;image is-128x128&#34;&gt;
          &lt;img src=&#34;https://lucidmanager.org/images/books/2023_ds4wu.jpg&#34; alt = &#34;Data Science for Water Utilities&#34; title = &#34;Data Science for Water Utilities&#34;&gt;
        &lt;/p&gt;
      &lt;/figure&gt;
      &lt;div class=&#34;media-content&#34;&gt;
        &lt;div class=&#34;content&#34;&gt;
          &lt;p class=&#34;is-size-5 has-text-weight-bold&#34;&gt;Data Science for Water Utilities&lt;/p&gt;
          &lt;p class=&#34;mb-2&#34;&gt;Data Science for Water Utilities published by CRC Press is an applied, practical guide that shows water professionals how to use data science to solve urban water management problems using the R language for statistical computing.&lt;/p&gt;
          &lt;div class=&#34;buttons&#34;&gt;
            &lt;a class=&#34;button is-info&#34; href=&#34;https://routledge.com/9781032354545&#34; target=&#34;_blank&#34;&gt;
              &lt;span class=&#34;icon&#34;&gt;
                &lt;i class=&#34;fas fa-shopping-cart&#34;&gt;&lt;/i&gt;
              &lt;/span&gt;
              &lt;span&gt;Routledge&lt;/span&gt;
            &lt;/a&gt;
            &lt;a class=&#34;button is-info&#34; href=&#34;https://www.amazon.com/Data-Science-Water-Utilities-Chapman/dp/1032354550&#34; target=&#34;_blank&#34;&gt;
              &lt;span class=&#34;icon&#34;&gt;
                &lt;i class=&#34;fas fa-shopping-cart&#34;&gt;&lt;/i&gt;
              &lt;/span&gt;
              &lt;span&gt;Amazon&lt;/span&gt;
            &lt;/a&gt;
          &lt;/div&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://www.r-bloggers.com/&#34; target=&#34;_blank&#34; title=&#34;Proudly associated with R-Bloggers&#34;&gt;
  &lt;button class=&#34;button is-link is-medium&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-r-project&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span&gt;As seen on R Bloggers&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;

      </description>
    </item>
    
    
    
    <item>
      <title>Project Euler 29: Distinct Powers</title>
      <link>https://lucidmanager.org/data-science/project-euler-29/</link>
      <pubDate>Thu, 03 Aug 2017 00:00:00 +0000</pubDate>
      <author>Peter Prevos</author>
      <guid>https://lucidmanager.org/data-science/project-euler-29/</guid>
      <description>
	
&lt;p&gt;
Project Euler 29 is another permutation problem that is quite easy to solve using brute force. The now defunct MathBlog site by Kristian Edlund has a nice solution using only pen and paper.&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-1&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-1&#34;&gt;
Euler 29 Definition
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-1&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;Consider all integer combinations of: $a^b$ for $2 \leq a \leq 5$ and $b \leq b \leq 5$ .&lt;/p&gt;
&lt;p&gt;
$$2^2=4, \quad 2^3 = 8, \quad 2^4 = 16, \quad 2^5 = 32$$&lt;/p&gt;
&lt;p&gt;
$$3^2 = 9,\quad 3^3 = 27, \quad 3^4 = 81,\quad 3^5 = 243$$&lt;/p&gt;
&lt;p&gt;
$$4^2 = 16,\quad 4^3 = 64, \quad 4^4 = 256, \quad 4^5 = 1024$$&lt;/p&gt;
&lt;p&gt;
$$5^2 = 25,\quad 5^3 = 125, \quad 5^4 = 625,\quad 5^5 = 3125$$&lt;/p&gt;
&lt;p&gt;
If they are then placed in numerical order, with any repeats removed, we get the following sequence of 15 distinct terms:&lt;/p&gt;
&lt;p&gt;
$$4, \ 8, \ 9, \ 16, \ 25, \ 27, \ 32, \ 64, \ 81, \ 125, \ 243, \ 256,\ 625, \ 1024, \ 3125$$&lt;/p&gt;
&lt;p&gt;
How many &lt;a href=&#34;https://projecteuler.net/problem=29&#34;&gt;distinct terms&lt;/a&gt; are in the sequence generated by $a^b$ for $2 \leq a \leq 100$ and $2 \leq b \leq 100$ ?&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-2&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-2&#34;&gt;
Proposed Solution
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-2&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;This code calculates all powers from $2^2$ to $2^{100}$ and determines the number of unique values. There is no need to use Multiple Precision Arithmetic because we are only interested in their uniqueness and not the precise value.&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://github.com/pprevos/ProjectEuler/&#34; target=&#34;_blank&#34;
   title=&#34;Download ProjectEuler from GitHub&#34;
   alt=&#34;Download ProjectEuler from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;ProjectEuler&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Project Euler 29: Distinct Powers&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  target &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;100&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  terms &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;vector&lt;/span&gt;()
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  i &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;for&lt;/span&gt; (a &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;in&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;target) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;for&lt;/span&gt; (b &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;in&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;target) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      terms[i] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; a^b
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      i &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; i &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  answer &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;length&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;unique&lt;/span&gt;(terms))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;print&lt;/span&gt;(answer)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
&lt;a href = &#34;https://www.r-bloggers.com/&#34; target=&#34;_blank&#34; title=&#34;Proudly associated with R-Bloggers&#34;&gt;
  &lt;button class=&#34;button is-link is-medium&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-r-project&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span&gt;As seen on R Bloggers&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;

      </description>
    </item>
    
    
    
    <item>
      <title>How Virtual Tags have transformed SCADA data analysis</title>
      <link>https://lucidmanager.org/data-science/virtual-tags/</link>
      <pubDate>Thu, 27 Jul 2017 00:00:00 +0000</pubDate>
      <author>Peter Prevos</author>
      <guid>https://lucidmanager.org/data-science/virtual-tags/</guid>
      <description>
	
&lt;p&gt;
Yesterday, I delivered the International Keynote at the &lt;a href=&#34;https://www.conferenz.co.nz/events/asset-data-insights-conference&#34;&gt;Asset Data &amp;amp; Insights Conference&lt;/a&gt; in Auckland, New Zealand (the place where R was initially developed). My talk was about creating value from CADA data using a method I developed with my colleagues called &amp;#34;Virtual Tags.&amp;#34; My address started with my views on data science strategy, which I also presented to the &lt;a href=&#34;https://lucidmanager.org/data-science/strategic-data-science/&#34;&gt;R User Group&lt;/a&gt; in Melbourne. In this post, I would like to explain what Virtual Tags are and how they can be used to improve the value of &lt;a href=&#34;https://www.researchgate.net/publication/326533741_Health-Based_Targets_Performance_Reporting_Using_Virtual_SCADA_Tags_to_Facilitate_Data_Aalysis&#34;&gt;SCADA data&lt;/a&gt;.&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-1&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-1&#34;&gt;
SCADA Systems at Water Treatment Plants
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-1&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;Water treatment plants are mostly fully automated, using analysers and the SCADA system to communicate this data. For those of you not familiar with water treatment plants, the video below gives a cute summary of the process.&lt;/p&gt;
&lt;figure&gt;
&lt;div style=&#34;position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden;&#34;&gt;
      &lt;iframe allow=&#34;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share; fullscreen&#34; loading=&#34;eager&#34; referrerpolicy=&#34;strict-origin-when-cross-origin&#34; src=&#34;https://www.youtube.com/embed/rj44AkHmVCo?autoplay=0&amp;amp;controls=1&amp;amp;end=0&amp;amp;loop=0&amp;amp;mute=0&amp;amp;start=0&#34; style=&#34;position: absolute; top: 0; left: 0; width: 100%; height: 100%; border:0;&#34; title=&#34;YouTube video&#34;&gt;&lt;/iframe&gt;
    &lt;/div&gt;

&lt;figcaption&gt;
Water Treatment — SCADA Plant IQ
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
Water treatment plants rely on sensors to track a broad range of parameters. These instruments capture data around the clock, supporting real-time operational decisions. When everything runs smoothly, all values stay within a precise band. Typically, these values are archived by the SCADA system for a year, then purged to manage storage needs.&lt;/p&gt;
&lt;p&gt;
Turbidity measurements—essentially a gauge of water clarity—help assess filtration effectiveness at water treatment plants. Below, you’ll find a code snippet simulating five hours of turbidity readings from one such instrument. Most of the time, these water quality values follow a log-normal distribution and cluster tightly together, making them ideal for precise monitoring.&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://github.com/pprevos/r4h2o/&#34; target=&#34;_blank&#34;
   title=&#34;Download r4h2o from GitHub&#34;
   alt=&#34;Download r4h2o from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;r4h2o&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;# Virtual tag simulation&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;# Generate turbidity measurements&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;set.seed&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1234&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  n &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;300&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  wtp &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;data.frame&lt;/span&gt;(timestamp &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;seq.POSIXt&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ISOdate&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1910&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;), length.out &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; n, by &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;60&lt;/span&gt;),
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                    turbidity &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;rlnorm&lt;/span&gt;(n, &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;log&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;.1&lt;/span&gt;), &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;.01&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(ggplot2)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  p1 &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ggplot&lt;/span&gt;(wtp) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;aes&lt;/span&gt;(x &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; timestamp, y &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; turbidity) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;geom_line&lt;/span&gt;(colour &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;grey&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ylim&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0.09&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0.11&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;theme_bw&lt;/span&gt;(base_size &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;labs&lt;/span&gt;(title &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Turbidity simulation&amp;#34;&lt;/span&gt;, x &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Timestamp&amp;#34;&lt;/span&gt;, y &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Turbidity&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  p1&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/hydroinformatics/turbidity-simulation.png&#34; alt=&#34;Turbidity simulation&#34; title=&#34;Turbidity simulation&#34;/&gt;
&lt;figcaption&gt;
Turbidity simulation.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-2&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-2&#34;&gt;
SCADA Historian
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-2&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The data generated by the SCADA system is used to make operational decisions. The data is created and structured to make decisions in the present, not to solve problems in the future. SCADA Historian systems archive this information for future analysis. Historian systems only store new values when the new reading is more or less than a certain percentage of the previous one. This method saves storage space without sacrificing much accuracy.&lt;/p&gt;
&lt;p&gt;
Imagine an instrument reads 0.20, and the limit is 5%: you’ll see new values recorded only when they fall below 0.19 or above 0.21. Each subsequent value gets stored only when it deviates by 5% from the last stored point. The following code snippet brings this process to life, simulating it using earlier turbidity readings. Notice: this Historian captures and stores only the data points shown in black.&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;# Historise using dead banding&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  threshold &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0.03&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  h &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt; &lt;span style=&#34;color:#888&#34;&gt;# First historised point&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  wtp&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;historised &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;FALSE&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  wtp&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;historised&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;[c&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;, n)] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;TRUE&lt;/span&gt; &lt;span style=&#34;color:#888&#34;&gt;# Testing for delta &amp;gt; threshold&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;for&lt;/span&gt; (i &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;in&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;nrow&lt;/span&gt;(wtp)) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    delta &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; wtp&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;turbidity[i] &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt; wtp&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;turbidity[h] 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (delta &lt;span style=&#34;color:#333&#34;&gt;&amp;gt;&lt;/span&gt; (&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; threshold) &lt;span style=&#34;color:#333&#34;&gt;|&lt;/span&gt; delta &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;&lt;/span&gt; (&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; threshold)) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      wtp&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;historised[i] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;TRUE&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      h &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; i
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  historian &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;subset&lt;/span&gt;(wtp, historised)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  p2 &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; p1 &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;geom_point&lt;/span&gt;(data &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; historian, 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;               &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;aes&lt;/span&gt;(x &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; timestamp, y &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; turbidity), 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;               size &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;3&lt;/span&gt;, alpha &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;.5&lt;/span&gt;, color &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;blue&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;labs&lt;/span&gt;(title &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Historised data&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  p2&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/hydroinformatics/turbidity-historised.png&#34; alt=&#34;Historised simulated turbidity data&#34; title=&#34;Historised simulated turbidity data&#34;/&gt;
&lt;figcaption&gt;
Historised simulated turbidity data.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-3&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-3&#34;&gt;
Virtual Tags
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-3&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;This standard method for generating and storing SCADA data works well for operational systems but not for post hoc analysis. The data in Historian are unequally spaced, making them harder to analyse. Using constant interpolation, the Virtual Tag approach expands these unequal time series into an equally spaced time series.&lt;/p&gt;
&lt;p&gt;
The &lt;code class=&#34;verbatim&#34;&gt;vt()&lt;/code&gt; function undertakes the constant interpolation using the &lt;a href=&#34;https://www.rdocumentation.org/packages/stats/versions/3.6.2/topics/approxfun&#34;&gt;approx function&lt;/a&gt;. The function &lt;code class=&#34;verbatim&#34;&gt;vt&lt;/code&gt; is applied to all the &lt;code class=&#34;verbatim&#34;&gt;DateTime&lt;/code&gt; values using the historised data points. The red line shows how the value is constant until it jumps by more than 5%. This example demonstrates that we have a steady process with some minor spikes, which is the expected outcome of this simulation.&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;# Virtual Tags extrapolation&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  vt &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;function&lt;/span&gt;(t) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;approx&lt;/span&gt;(historian&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;timestamp, historian&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;turbidity, xout &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; t, method &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;constant&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  turbidity &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;lapply&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;as.data.frame&lt;/span&gt;(wtp&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;timestamp), vt)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  wtp&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;virtual_tag &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; turbidity[[1]]&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;y
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  p3 &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; p2 &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;geom_line&lt;/span&gt;(data &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; wtp,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;              &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;aes&lt;/span&gt;(x &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; timestamp, y &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; virtual_tag), colour &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;red&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ggtitle&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Virtual Tags&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  p3&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/hydroinformatics/turbidity-virtual-tags.png&#34; alt=&#34;Virtual tags for the simulated data&#34; title=&#34;Virtual tags for the simulated data&#34;/&gt;
&lt;figcaption&gt;
Virtual tags for the simulated data.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
The next step in Virtual Tags is to combine tags from different data points for richer insights. For instance, imagine you&amp;#39;re curious about turbidity readings only when the filter is running. By combining this data with information such as the filter&amp;#39;s valve status and flow, you can gain a clearer picture of system performance.&lt;/p&gt;
&lt;p&gt;
Although this approach may seem cumbersome at first glance, it makes analysing data from SCADA Historian much more straightforward. With Virtual Tags, you can easily access a catalogue of analytical processes that would otherwise be challenging to perform. This system also provides valuable context for your SCADA information by linking tags to one another and to the operational processes they reflect. Curious to learn more? Download the &lt;a href=&#34;http://colibanwater.github.io/HBT/&#34;&gt;technical manual&lt;/a&gt; for an in-depth look at Virtual Tags and their implementation in SQL.&lt;/p&gt;
&lt;p&gt;
Interested in solving water challenges with R code? You might enjoy the book Data Science for Water Utilities, which goes deeper into practical techniques for the field.&lt;/p&gt;
&lt;p&gt;

  &lt;div class=&#34;box&#34;&gt;
    &lt;div class=&#34;media&#34;&gt;
      &lt;figure class=&#34;media-left&#34;&gt;
        &lt;p class=&#34;image is-128x128&#34;&gt;
          &lt;img src=&#34;https://lucidmanager.org/images/books/2023_ds4wu.jpg&#34; alt = &#34;Data Science for Water Utilities&#34; title = &#34;Data Science for Water Utilities&#34;&gt;
        &lt;/p&gt;
      &lt;/figure&gt;
      &lt;div class=&#34;media-content&#34;&gt;
        &lt;div class=&#34;content&#34;&gt;
          &lt;p class=&#34;is-size-5 has-text-weight-bold&#34;&gt;Data Science for Water Utilities&lt;/p&gt;
          &lt;p class=&#34;mb-2&#34;&gt;Data Science for Water Utilities published by CRC Press is an applied, practical guide that shows water professionals how to use data science to solve urban water management problems using the R language for statistical computing.&lt;/p&gt;
          &lt;div class=&#34;buttons&#34;&gt;
            &lt;a class=&#34;button is-info&#34; href=&#34;https://routledge.com/9781032354545&#34; target=&#34;_blank&#34;&gt;
              &lt;span class=&#34;icon&#34;&gt;
                &lt;i class=&#34;fas fa-shopping-cart&#34;&gt;&lt;/i&gt;
              &lt;/span&gt;
              &lt;span&gt;Routledge&lt;/span&gt;
            &lt;/a&gt;
            &lt;a class=&#34;button is-info&#34; href=&#34;https://www.amazon.com/Data-Science-Water-Utilities-Chapman/dp/1032354550&#34; target=&#34;_blank&#34;&gt;
              &lt;span class=&#34;icon&#34;&gt;
                &lt;i class=&#34;fas fa-shopping-cart&#34;&gt;&lt;/i&gt;
              &lt;/span&gt;
              &lt;span&gt;Amazon&lt;/span&gt;
            &lt;/a&gt;
          &lt;/div&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://www.r-bloggers.com/&#34; target=&#34;_blank&#34; title=&#34;Proudly associated with R-Bloggers&#34;&gt;
  &lt;button class=&#34;button is-link is-medium&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-r-project&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span&gt;As seen on R Bloggers&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;

      </description>
    </item>
    
    
    
    <item>
      <title>Project Euler 28: Number Spiral Diagonals</title>
      <link>https://lucidmanager.org/data-science/project-euler-28/</link>
      <pubDate>Thu, 20 Jul 2017 00:00:00 +0000</pubDate>
      <author>Peter Prevos</author>
      <guid>https://lucidmanager.org/data-science/project-euler-28/</guid>
      <description>
	
&lt;p&gt;
Project Euler 28 takes us to the world of the Ulam Spiral. This is a spiral that contains sequential positive integers in a square spiral, marking the prime numbers. Stanislaw Ulam discovered that a lot of primes are located along the diagonals. These diagonals can be described as polynomials. The Ulam Spiral is thus a method to generate quadratic primes (&lt;a href=&#34;https://lucidmanager.org/data-science/project-euler-27/&#34;&gt;Euler Problem 27&lt;/a&gt;).&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://upload.wikimedia.org/wikipedia/commons/thumb/3/3c/Ulam_spiral_howto_primes_only.svg/507px-Ulam_spiral_howto_primes_only.svg.png&#34; alt=&#34;Ulam Spiral&#34; title=&#34;Ulam Spiral&#34; width=&#34;200&#34;/&gt;
&lt;figcaption&gt;
Ulam spiral (Wikimedia).
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;div id=&#34;outline-container-headline-1&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-1&#34;&gt;
Project Euler 28 Definition
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-1&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;Starting with the number 1 and moving to the right in a clockwise direction, a 5 by 5 spiral is formed as follows:&lt;/p&gt;
&lt;div class=&#34;center-block&#34; style=&#34;text-align: center; margin-left: auto; margin-right: auto;&#34;&gt;
&lt;div class=&#34;src src-text&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-text&#34; data-lang=&#34;text&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;21  22  23  24  25
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;20   7   8   9  10
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;19   6   1   2  11
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;18   5   4   3  12
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;17  16  15  14  13&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
It can be verified that the sum of the numbers on the diagonals is 101.&lt;/p&gt;
&lt;p&gt;
What is the &lt;a href=&#34;https://projecteuler.net/problem=28&#34;&gt;sum of the numbers on the diagonals&lt;/a&gt; in a 1001 by 1001 spiral formed in the same way?&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-2&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-2&#34;&gt;
Proposed Solution
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-2&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;To solve this problem, we do not need to create a matrix. This code calculates the values of the corners of a matrix with size \(n\). The lowest number in the matrix with size \(n\) is \(n(n-3)+4\). The numbers increase by \(n-1\) . The code steps through all matrices from size 3 to 1001. The solution uses only the uneven sized matrices because these have a centre. The answer to the problem is the sum of all numbers.&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://github.com/pprevos/ProjectEuler/&#34; target=&#34;_blank&#34;
   title=&#34;Download ProjectEuler from GitHub&#34;
   alt=&#34;Download ProjectEuler from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;ProjectEuler&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Project Euler 28: Number Spiral Diagonals&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  size &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1001&lt;/span&gt; &lt;span style=&#34;color:#888&#34;&gt;# Size of matrix&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  answer &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt; &lt;span style=&#34;color:#888&#34;&gt;# Starting number&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Define corners of subsequent matrices&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;for&lt;/span&gt; (n &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;in&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;seq&lt;/span&gt;(from &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;3&lt;/span&gt;, to &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; size, by &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;)) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      corners &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;seq&lt;/span&gt;(from &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; n &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; (n &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;3&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;3&lt;/span&gt;, by &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; n &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;, length.out &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;4&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      answer &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; answer &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sum&lt;/span&gt;(corners)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;print&lt;/span&gt;(answer)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-3&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-3&#34;&gt;
Plotting the Ulam Spiral
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-3&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;We can go beyond Euler Problem 28 and play with the mathematics. This code snippet plots all the prime numbers in the Ulam Spiral. Watch the Numberphile video for an explanation of the patterns that appear along the diagonals.&lt;/p&gt;
&lt;p&gt;
The code creates a matrix of the required size and fills it with the Ulam Spiral. The system then identifies all primes using the &lt;code class=&#34;verbatim&#34;&gt;is.prime()&lt;/code&gt; function from &lt;a href=&#34;https://lucidmanager.org/data-science/project-euler-7/&#34;&gt;Euler Problem 7&lt;/a&gt;, as visualised on the top of this article.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/project-euler/problem-028.png&#34; alt=&#34;Ulam Spiral prime numbers.&#34; title=&#34;Ulam Spiral prime numbers.&#34;/&gt;
&lt;figcaption&gt;
Ulam Spiral prime numbers.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;figure&gt;
&lt;div style=&#34;position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden;&#34;&gt;
      &lt;iframe allow=&#34;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share; fullscreen&#34; loading=&#34;eager&#34; referrerpolicy=&#34;strict-origin-when-cross-origin&#34; src=&#34;https://www.youtube.com/embed/iFuR97YcSLM?autoplay=0&amp;amp;controls=1&amp;amp;end=0&amp;amp;loop=0&amp;amp;mute=0&amp;amp;start=0&#34; style=&#34;position: absolute; top: 0; left: 0; width: 100%; height: 100%; border:0;&#34; title=&#34;YouTube video&#34;&gt;&lt;/iframe&gt;
    &lt;/div&gt;

&lt;figcaption&gt;
Prime Spirals - Numberphile.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Prime Spirals&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;source&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;problem007.R&amp;#34;&lt;/span&gt;, verbose &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;FALSE&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  size &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;201&lt;/span&gt; &lt;span style=&#34;color:#888&#34;&gt;# Size of matrix&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  ulam &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;matrix&lt;/span&gt;(ncol &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; size, nrow &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; size)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  mid &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;floor&lt;/span&gt;(size &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  ulam[mid, mid] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;for&lt;/span&gt; (n &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;in&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;seq&lt;/span&gt;(from &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;3&lt;/span&gt;, to &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; size, by &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;)) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      numbers &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; (n &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; (n &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;4&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;5&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt; ((n &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; ((n &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;4&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;4&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      d &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; mid &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;floor&lt;/span&gt;(n &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      l &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;length&lt;/span&gt;(numbers)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      ulam[d, d&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;(d &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; n &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;)] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; numbers&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;[&lt;/span&gt;(l &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; n &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;)&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;l]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      ulam[d &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; n &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;, (d &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; n &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;)&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;d] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; numbers&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;[&lt;/span&gt;(n &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;)&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;(n &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; n)]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      ulam&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;[&lt;/span&gt;(d &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;)&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;(d &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; n &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;), d] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; numbers&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;[&lt;/span&gt;(l &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; n)&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;(l &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; n &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;3&lt;/span&gt;)]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      ulam&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;[&lt;/span&gt;(d &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;)&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;(d &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; n &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;), d &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; n &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; numbers[1&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;(n &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;)]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  ulam.primes &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;apply&lt;/span&gt;(ulam, &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;), is.prime)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(ggplot2)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(reshape2)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  ulam.primes &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;melt&lt;/span&gt;(ulam.primes)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ggplot&lt;/span&gt;(ulam.primes, &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;aes&lt;/span&gt;(x &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; Var1, y &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; Var2, fill &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; value)) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;geom_tile&lt;/span&gt;() &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;scale_fill_manual&lt;/span&gt;(values &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;white&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;black&amp;#34;&lt;/span&gt;)) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;guides&lt;/span&gt;(fill &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;FALSE&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;theme_void&lt;/span&gt;()
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ggsave&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;images/problem-028.png&amp;#34;&lt;/span&gt;, widt &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;6&lt;/span&gt;, height &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;4&lt;/span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;a href = &#34;https://www.r-bloggers.com/&#34; target=&#34;_blank&#34; title=&#34;Proudly associated with R-Bloggers&#34;&gt;
  &lt;button class=&#34;button is-link is-medium&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-r-project&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span&gt;As seen on R Bloggers&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/div&gt;
&lt;/div&gt;

      </description>
    </item>
    
    
    
    <item>
      <title>Using the iGraph package to Analyse the Enron Corpus</title>
      <link>https://lucidmanager.org/data-science/analyse-enron-corpus/</link>
      <pubDate>Thu, 13 Jul 2017 10:00:01 +0000</pubDate>
      <author>Peter Prevos</author>
      <guid>https://lucidmanager.org/data-science/analyse-enron-corpus/</guid>
      <description>
	
&lt;p&gt;
The Enron scandal is one of the most famous corporate governance failures in the history of capitalism. The case itself is interesting for its own sake, but in this post, I am more interested in one of the data sets that the subsequent investigation has provided. The code below show s how to analyse the Enron corpus using the R language.&lt;/p&gt;
&lt;p&gt;
This blog post analyses an extensive collection of e-mails from former Enron employees. The Enron corpus is analysed using network analysis tools provided by the &lt;a href=&#34;https://igraph.org/r/&#34;&gt;iGraph package&lt;/a&gt;. Network analysis is a versatile technique that can be used to add value to a lot of different data sets, including the complex corporate relationships of &lt;a href=&#34;https://lucidmanager.org/data-science/trumpworld-analysis/&#34;&gt;Donald Trump&lt;/a&gt;.&lt;/p&gt;
&lt;figure&gt;
&lt;div style=&#34;position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden;&#34;&gt;
      &lt;iframe allow=&#34;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share; fullscreen&#34; loading=&#34;eager&#34; referrerpolicy=&#34;strict-origin-when-cross-origin&#34; src=&#34;https://www.youtube.com/embed/jrEf8uabe7E?autoplay=0&amp;amp;controls=1&amp;amp;end=0&amp;amp;loop=0&amp;amp;mute=0&amp;amp;start=0&#34; style=&#34;position: absolute; top: 0; left: 0; width: 100%; height: 100%; border:0;&#34; title=&#34;YouTube video&#34;&gt;&lt;/iframe&gt;
    &lt;/div&gt;

&lt;figcaption&gt;
The Enron Scandal Explained in One Minute
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;div id=&#34;outline-container-headline-1&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-1&#34;&gt;
The Enron Corpus
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-1&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The Federal Energy Regulatory Commission used an extensive collection of emails from Enron employees as part of their inquiries. The Enron corpus is one of the only publicly available collections of emails available for research. This dataset also provides a fascinating playground for citizen data scientists.&lt;/p&gt;
&lt;p&gt;
The set has privacy issues as it contains messages from living people. When analysing this data set, we need to keep in mind that the majority of former Enron employees were innocent people who lost their jobs due to the greed of their overlords. The code in this post only analyses the e-mail headers, ignoring the content.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://fm.cnbc.com/applications/cnbc.com/resources/img/editorial/2014/10/13/102082546-675237.530x298.jpg&#34; alt=&#34;Laid-off Enron employees outside Enron headquarters as the company collapsed in 2001&#34; title=&#34;Laid-off Enron employees outside Enron headquarters as the company collapsed in 2001&#34;/&gt;
&lt;figcaption&gt;
Laid-off Enron employees outside Enron headquarters as the company collapsed in 2001 (Source cnbc.com).
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
The Enron Corpus is an extensive database of half a million emails generated by more than 100 Enron employees. You can download the corpus from the &lt;a href=&#34;https://www.cs.cmu.edu/~./enron/&#34;&gt;Carnegie Mellon School of Computer Science&lt;/a&gt;. The first code snippet downloads the 7 May 2015 version of the dataset (about 423Mb, tarred and gzipped) and untars it to your working directory. &lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://github.com/pprevos/digital-humanities/&#34; target=&#34;_blank&#34;
   title=&#34;Download digital-humanities from GitHub&#34;
   alt=&#34;Download digital-humanities from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;digital-humanities&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (&lt;span style=&#34;color:#333&#34;&gt;!&lt;/span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;file.exists&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;enron_mail_20150507.tgz&amp;#34;&lt;/span&gt;)) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;download.file&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;https://www.cs.cmu.edu/~./enron/enron_mail_20150507.tar.gz&amp;#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                     destfile &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;enron-mail-20150507.tgz&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;file.exists&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;enron-mail-20150507.tgz&amp;#34;&lt;/span&gt;)) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;untar&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;enron-mail-20150507.tgz&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-2&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-2&#34;&gt;
Preparing the Enron email
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-2&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The main folder is &lt;code class=&#34;verbatim&#34;&gt;maildir&lt;/code&gt;, which holds all the personal accounts. Our first task is to create a list of available emails. This code results in 517,401 e-mail files with 44,859 emails in the inboxes of users. The E-mail corpus consists of nested folders per user with each e-mail as a text file.&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  emails &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;list.files&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;maildir/&amp;#34;&lt;/span&gt;, full.names &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;TRUE&lt;/span&gt;, recursive &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;TRUE&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  emails &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; emails&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;[grep&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;/inbox&amp;#34;&lt;/span&gt;, emails)]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;length&lt;/span&gt;(emails)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
The bulk of the code creates a list of emails between Enron employees. The code performs a lot of string manipulations to extract the information from the text files. The content of the e-mails is ignored, the code only retrieves the sender and the receiver. The analysis is limited to e-mails between Enron employees. The result of this code is a data frame with the usernames of the sender and receiver for each email. The data frame contains 2779 emails that meet these criteria. &lt;/p&gt;
&lt;p&gt;
The code below looks extracts all internal emails in the Inox folders. It then retrieves a lis of usernames from the e-mails. The data is cleaned by removing users without sent e-mails, and emails which users sent to themselves.&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(tibble)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  inboxes &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;tibble&lt;/span&gt;(
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      from &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;apply&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;as.data.frame&lt;/span&gt;(emails), &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;, 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                 &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;function&lt;/span&gt;(x){&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;readLines&lt;/span&gt;(x, warn &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;FALSE&lt;/span&gt;)[3]}),
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      to &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; emails,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      stringsAsFactors &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;FALSE&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  )
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Keep only enron.com and strip all but username&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(stringr) &lt;span style=&#34;color:#888&#34;&gt;# String manipulation&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  inboxes &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; inboxes&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;[grepl&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;@enron.com&amp;#34;&lt;/span&gt;, inboxes&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;from),]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  inboxes&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;from &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;str_remove_all&lt;/span&gt;(inboxes&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;from, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;From: |@enron.com&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  inboxes&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;to &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;str_remove_all&lt;/span&gt;(inboxes&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;to, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;maildir//|/inbox/[0-9]*.&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Create list of usernames&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  users &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;data.frame&lt;/span&gt;(user &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;paste0&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;maildir/&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;unique&lt;/span&gt;(inboxes&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;to)))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Remove those without sent mails&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  sent &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;apply&lt;/span&gt;(users, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;, &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;function&lt;/span&gt;(x) &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sum&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;grepl&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;sent&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;dir&lt;/span&gt;(x))))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  users &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;subset&lt;/span&gt;(users, sent &lt;span style=&#34;color:#333&#34;&gt;!=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Replace username with e-mail name&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  users&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;mailname &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;NA&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  i &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;-1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;for&lt;/span&gt; (i &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;in&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;nrow&lt;/span&gt;(users)){
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      sentmail &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;dir&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;paste0&lt;/span&gt;(users&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;user[i], &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;/sent_items/&amp;#34;&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      name &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;readLines&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;paste0&lt;/span&gt;(users&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;user[i], &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;/sent_items/&amp;#34;&lt;/span&gt;, sentmail[1]), warn &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#007020&#34;&gt;F&lt;/span&gt;)[3]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      name &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;str_remove_all&lt;/span&gt;(name, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;From: |@enron.com&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      users&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;mailname[i] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; name
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  users&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;user &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;str_remove&lt;/span&gt;(users&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;user, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;maildir/&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  inboxes &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;merge&lt;/span&gt;(inboxes, by.x &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;to&amp;#34;&lt;/span&gt;, users, by.y &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;user&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  inboxes &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;tibble&lt;/span&gt;(from &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; inboxes&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;from,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                    to &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; inboxes&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;mailname)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Only e-mails between users inside the corpus&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  inboxes &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; inboxes[inboxes&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;from &lt;span style=&#34;color:#333&#34;&gt;%in%&lt;/span&gt; inboxes&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;to,]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Remove no.address&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  inboxes &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;subset&lt;/span&gt;(inboxes, from &lt;span style=&#34;color:#333&#34;&gt;!=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;no.address&amp;#34;&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;&amp;amp;&lt;/span&gt; to &lt;span style=&#34;color:#333&#34;&gt;!=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;no.address&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Remove emails to self&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  inboxes &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;subset&lt;/span&gt;(inboxes, inboxes&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;from &lt;span style=&#34;color:#333&#34;&gt;!=&lt;/span&gt; inboxes&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;to)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  inboxes&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-3&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-3&#34;&gt;
Analyse the Enron Corpus
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-3&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The last code snippet defines a &lt;a href=&#34;https://en.wikipedia.org/wiki/Graph_(discrete_mathematics)&#34;&gt;graph&lt;/a&gt; from the table of e-mails. Each employee is a node in the network, and each e-mail is an edge (line). The iGraph package is a powerful tool to analyse networks. The &lt;code class=&#34;verbatim&#34;&gt;graph_from_edgelist&lt;/code&gt; function creates a network object that can be analysed using the iGraph package. The graph is directed because the information flows from the sender to the receiver.&lt;/p&gt;
&lt;p&gt;
In the next step, the Spingglass algorithm finds &lt;a href=&#34;https://en.wikipedia.org/wiki/Community_structure&#34;&gt;community structure&lt;/a&gt; within the data. A community is a group of nodes that are more connected with each other than with any other node.&lt;/p&gt;
&lt;p&gt;
The last step visualises the network. The diagram is drawn using the Fruchterman-Reingold algorithm, which places the most connected nodes at the centre of the picture. The background colours in the diagram indicate the eight communities.&lt;/p&gt;
&lt;p&gt;
The graph tells us a lot about the group of employees in the Enron corpus and how they relate to each other. Each of the communities represents a tightly connected group of employees that mainly e-mail each other. Any connections between communities are shown in red. When the &lt;code class=&#34;verbatim&#34;&gt;vertex.label = NA&lt;/code&gt; line is removed, the usernames are displayed at each node.&lt;/p&gt;
&lt;p&gt;
We can see groups that never email each other, lonely hangers-on and tightly knit cliques within Enron. In the centre of the graph, we see a few individuals who are connectors between groups because they send a lot of emails to people outside their community. On the fringes of the graph are the hangers-on who only once or twice emailed somebody in the corpus but were still included in the investigation.&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(igraph)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  g &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;graph_from_edgelist&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;as.matrix&lt;/span&gt;(inboxes), directed &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;TRUE&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  coms &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;spinglass.community&lt;/span&gt;(g)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;par&lt;/span&gt;(mar &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;,&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;,&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;,&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;plot&lt;/span&gt;(coms, g,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      vertex.label&lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;NA&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      layout &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; layout.fruchterman.reingold,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      vertex.size &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;3&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  )&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/digital-humanities/enron-email-network.jpg&#34; alt=&#34;Enron email corpus network with communities&#34; title=&#34;Enron email corpus network with communities&#34;/&gt;
&lt;figcaption&gt;
Enron email corpus network with communities.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
This analysis provides only a quick glimpse into the knowledge contained in the Enron email corpus. An extensive range of tools is available to analyse such networks. An interesting exercise would be to overlap this network with the organisation chart to see the relationships between teams. Have fun playing with this fantastic, but somewhat sensitive, data set!&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://www.r-bloggers.com/&#34; target=&#34;_blank&#34; title=&#34;Proudly associated with R-Bloggers&#34;&gt;
  &lt;button class=&#34;button is-link is-medium&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-r-project&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span&gt;As seen on R Bloggers&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;

      </description>
    </item>
    
    
    
    <item>
      <title>Project Euler 27: Quadratic Primes</title>
      <link>https://lucidmanager.org/data-science/project-euler-27/</link>
      <pubDate>Thu, 06 Jul 2017 00:00:00 +0000</pubDate>
      <author>Peter Prevos</author>
      <guid>https://lucidmanager.org/data-science/project-euler-27/</guid>
      <description>
	
&lt;p&gt;
Prime numbers are the engine of the Internet economy. There is no algorithm that can generate these numbers which makes them ideal for as encryption keys. This impossibility has not stopped mathematicians from trying to find formulas to generate prime numbers. Project Euler 27 deals with quadratic formulas that can be used to generate sets of prime numbers.&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-1&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-1&#34;&gt;
Project Euler 27 Definition
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-1&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;Euler discovered the remarkable quadratic formula: \(n^2+n+41\)&lt;/p&gt;
&lt;p&gt;
It turns out that the formula will produce 40 primes for the consecutive integer values \(0 \leq n \leq 39\). However, when \(n=40\), \(40^2+40+41=40(40+1)+41\) is divisible by 41, and certainly when \(n=41\), \(41^2+41+41\) is clearly divisible by 41.&lt;/p&gt;
&lt;p&gt;
The incredible formula \(n^2-79n+1601\) produces 80 primes for the consecutive values \(0 \leq n \leq 79\). The product of the coefficients, -79 and 1601, is -126479.&lt;/p&gt;
&lt;p&gt;
Considering quadratics of the form: &lt;/p&gt;
&lt;p&gt;
\(n^2 + an + b\), where \(|a| &amp;lt; 1000\) and \(|b| \leq 1000\).&lt;/p&gt;
&lt;p&gt;
\(|n|\) is the absolute value of \(n\), e.g. \(|11|=11\) and \(|-4|=4\).&lt;/p&gt;
&lt;p&gt;
Find the &lt;a href=&#34;https://projecteuler.net/problem=27&#34;&gt;product of the coefficients&lt;/a&gt;, \(a\) and \(b\), for the quadratic expression that produces the maximum number of primes for consecutive values of \(n\), starting with \(n=0\).&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-2&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-2&#34;&gt;
Proposed Solution
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-2&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The only way to solve this problem is through brute force and search for all possible combinations for \(a\) and \(b\). Without reducing the search space, that would mean testing four million options. We need to reduce the solution space to achieve a manageable running speed.&lt;/p&gt;
&lt;p&gt;
Because the outcome of the equation must be prime for \(n = 0\), \(b\) also has to be prime. We can use the prime sieve from &lt;a href=&#34;https://lucidmanager.org/data-science/project-euler-3/&#34;&gt;Euler Problem 3&lt;/a&gt; to use only prime numbers from 2 to 1000, which reduces it from 2000 to 168 permutations. &lt;/p&gt;
&lt;p&gt;
When we insert \(n = 1\), it follows that \(a\) has to be an even number. If \(1+a+b\) has to be prime and \(b\) is a prime number, then \(a\) can only be an odd number. However, when \(b = 2\), \(a\), also has to be even.&lt;/p&gt;
&lt;p&gt;
We can use these deliberations to write some code to solve this problem. We cycle through \(a\) and \(b\) and increment a counter every time we find a prime number. The &lt;code class=&#34;verbatim&#34;&gt;is.prime()&lt;/code&gt; function from &lt;a href=&#34;https://lucidmanager.org/data-science/project-euler-7/&#34;&gt;Project Euler 7&lt;/a&gt; helps to identify whether a number is prime.&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://github.com/pprevos/ProjectEuler/&#34; target=&#34;_blank&#34;
   title=&#34;Download ProjectEuler from GitHub&#34;
   alt=&#34;Download ProjectEuler from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;ProjectEuler&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Project Euler 27: Quadratic Primes&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;source&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;problem007.R&amp;#34;&lt;/span&gt;, verbose &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;FALSE&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  seq.a &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;seq&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;-999&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1001&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;) &lt;span style=&#34;color:#888&#34;&gt;# a has to be odd&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  seq.b &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; (&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;esieve&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1000&lt;/span&gt;)) &lt;span style=&#34;color:#888&#34;&gt;# b has to be prime&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  max.n &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;for&lt;/span&gt; (a &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;in&lt;/span&gt; seq.a) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;for&lt;/span&gt; (b &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;in&lt;/span&gt; seq.b) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      n &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt; &lt;span style=&#34;color:#888&#34;&gt;# Find sequence of primes for a and b&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;while&lt;/span&gt; (&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;is.prime&lt;/span&gt;(n^2 &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; a &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; n &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; b)) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        n &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; n &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#888&#34;&gt;## Store maximum values&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (n &lt;span style=&#34;color:#333&#34;&gt;&amp;gt;&lt;/span&gt; max.n) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        max.n &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; n
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        max.a &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; a
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        max.b &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; b
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  answer &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; max.a &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; max.b
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;print&lt;/span&gt;(answer)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Test the answer&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  n &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;(max.count &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sapply&lt;/span&gt;(n^2 &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; max.a &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; n &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; max.b, is.prime)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
&lt;a href = &#34;https://www.r-bloggers.com/&#34; target=&#34;_blank&#34; title=&#34;Proudly associated with R-Bloggers&#34;&gt;
  &lt;button class=&#34;button is-link is-medium&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-r-project&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span&gt;As seen on R Bloggers&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;

      </description>
    </item>
    
    
    
    <item>
      <title>Project Euler 26: Reciprocal Cycles</title>
      <link>https://lucidmanager.org/data-science/project-euler-26/</link>
      <pubDate>Thu, 29 Jun 2017 00:00:00 +0000</pubDate>
      <author>Peter Prevos</author>
      <guid>https://lucidmanager.org/data-science/project-euler-26/</guid>
      <description>
	
&lt;p&gt;
Project Euler 26 delves into a topic many kids in primary school dread: fractions. A few years ago, a fraction broke the internet. What happens when you divide 1 by 998001?&lt;/p&gt;
&lt;p&gt;
$$\frac{1}{998001} = 0.000001002003004005006007008009010011012013014015 \ldots$$&lt;/p&gt;
&lt;p&gt;
What is unique about this fraction is that it lists every three-decimal number except for 998. Look carefully at the sequence to see that is 000, 001, 0002, 003, 004, 005 and so on. After it has reached 999, the sequence continues from the start. This fraction thus has 2997 recurring decimals. James Grime from Numberphile explains this mathematical oddity with his usual enthusiasm.&lt;/p&gt;
&lt;figure&gt;
&lt;div style=&#34;position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden;&#34;&gt;
      &lt;iframe allow=&#34;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share; fullscreen&#34; loading=&#34;eager&#34; referrerpolicy=&#34;strict-origin-when-cross-origin&#34; src=&#34;https://www.youtube.com/embed/daro6K6mym8?autoplay=0&amp;amp;controls=1&amp;amp;end=0&amp;amp;loop=0&amp;amp;mute=0&amp;amp;start=0&#34; style=&#34;position: absolute; top: 0; left: 0; width: 100%; height: 100%; border:0;&#34; title=&#34;YouTube video&#34;&gt;&lt;/iframe&gt;
    &lt;/div&gt;

&lt;figcaption&gt;
998,001 and its Mysterious Recurring Decimals - Numberphile.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
The decimal fraction of 1/998001 is a recurring decimal. These are decimal numbers with periodic digits (repeating its values at regular intervals). &lt;/p&gt;
&lt;p&gt;
&lt;a href=&#34;https://lucidmanager.org/data-science/project-euler-033/&#34;&gt;Project Euler 33&lt;/a&gt; is also about fractions and asks us to analyse recurring decimals (reciprocal cycles) and Ford Circles.&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-1&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-1&#34;&gt;
Project Euler 26 Definition
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-1&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;A unit fraction contains 1 in the numerator. The decimal representation of the unit fractions with denominators 2 to 10 are given:&lt;/p&gt;
&lt;p&gt;
1/2 = 0.5&lt;/p&gt;
&lt;p&gt;
1/3 = 0.(3) &lt;/p&gt;
&lt;p&gt;
1/4 = 0.25 &lt;/p&gt;
&lt;p&gt;
1/5 = 0.2 &lt;/p&gt;
&lt;p&gt;
1/6 = 0.1(6) &lt;/p&gt;
&lt;p&gt;
1/7 = 0.(142857) &lt;/p&gt;
&lt;p&gt;
1/8 = 0.125 &lt;/p&gt;
&lt;p&gt;
1/9 = 0.(1) &lt;/p&gt;
&lt;p&gt;
1/10 = 0.1 &lt;/p&gt;
&lt;p&gt;
Where 0.1(6) means 0.166666… and has a 1-digit recurring cycle. It can be seen that 1/7 has a 6-digit recurring cycle. Find the value of d &amp;lt; 1000 for which 1/d contains the &lt;a href=&#34;https://projecteuler.net/problem=26&#34;&gt;longest recurring cycle&lt;/a&gt; in its decimal fraction part.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-2&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-2&#34;&gt;
Proposed Solution
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-2&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;&lt;a href=&#34;https://oeis.org/A051626&#34;&gt;Sequence A051626&lt;/a&gt; in the On-Line Encyclopaedia of Integer Sequences describes the length of the recurring numbers in 1/n. To solve Euler Problem 26, we need to generate the first 1000 numbers of this sequence and find out which number has the longest recurring cycle.&lt;/p&gt;
&lt;p&gt;
R can only display up to 22 decimals by using &lt;code class=&#34;verbatim&#34;&gt;options(digits=22)&lt;/code&gt;. The base R capability is unsuitable for solving this problem, so I wrote some code to perform long division the old-fashioned way.&lt;/p&gt;
&lt;p&gt;
The &lt;code class=&#34;verbatim&#34;&gt;recur()&lt;/code&gt; function divides 1 by any arbitrary integer. The code continues until the decimal terminates, for example 1/4 = 0.25, or when a recurring pattern emerges, e.g. 1/7 = 0.(142857).&lt;/p&gt;
&lt;p&gt;
The function has two arguments: &lt;code class=&#34;verbatim&#34;&gt;n&lt;/code&gt; is the input number. The output argument determines the outcome of the function: “=len=” for the length of the recurring decimals. Any other value shows the result of the calculation. The output of the function is a string. Using the European notation, the recurring part of the decimals is shown between brackets, e.g. 1/14 = 0.0(714285).&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://github.com/pprevos/ProjectEuler/&#34; target=&#34;_blank&#34;
   title=&#34;Download ProjectEuler from GitHub&#34;
   alt=&#34;Download ProjectEuler from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;ProjectEuler&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Project Euler 26: Reciprocal Cycles&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  recur &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;function&lt;/span&gt;(x, output &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;&amp;#34;&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#888&#34;&gt;# Prepare variable&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (x &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;) &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;return&lt;/span&gt;(&lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;NaN&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (x &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;) &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;return&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      x &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;floor&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;abs&lt;/span&gt;(x))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#888&#34;&gt;# Initiate vectors to store decimals and remainders&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      dec &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;vector&lt;/span&gt;()
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      rem &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;vector&lt;/span&gt;()
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#888&#34;&gt;# Initiate values&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      i &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      r &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      rem &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; r
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#888&#34;&gt;# Long division&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;repeat&lt;/span&gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          dec[i] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;floor&lt;/span&gt;(r &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt; x)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          r &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; (r &lt;span style=&#34;color:#333&#34;&gt;%%&lt;/span&gt; x)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          &lt;span style=&#34;color:#888&#34;&gt;# Test wether the number is terminating or repeating&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (r &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;|&lt;/span&gt; r &lt;span style=&#34;color:#333&#34;&gt;%in%&lt;/span&gt; rem) &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;break&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          rem[i &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; r
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          i &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; i &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#888&#34;&gt;# Determine number of recurring digits&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      rep &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ifelse&lt;/span&gt;(r &lt;span style=&#34;color:#333&#34;&gt;!=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;, &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;length&lt;/span&gt;(rem) &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;which&lt;/span&gt;(r &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; rem) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#888&#34;&gt;# Output&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (output &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;len&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;return&lt;/span&gt;(rep)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;else&lt;/span&gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (rep &lt;span style=&#34;color:#333&#34;&gt;!=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;              &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (rep &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;length&lt;/span&gt;(dec)) 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                  l &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;(&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;              &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;else&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                  l &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(dec[1&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;length&lt;/span&gt;(dec) &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; rep)], &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;(&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;              dec &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(l, dec&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;[&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;length&lt;/span&gt;(dec) &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; rep &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;)&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;length&lt;/span&gt;(dec)], &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;)&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;return&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;paste0&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;0.&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;paste0&lt;/span&gt;(dec, collapse &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;&amp;#34;&lt;/span&gt;, sep &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;&amp;#34;&lt;/span&gt;)))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  A051626 &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sapply&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1000&lt;/span&gt;, recur, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;len&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  answer &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;which.max&lt;/span&gt;(A051626)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;print&lt;/span&gt;(answer)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;recur&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;998001&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;len&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;recur&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;998001&lt;/span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;a href = &#34;https://www.r-bloggers.com/&#34; target=&#34;_blank&#34; title=&#34;Proudly associated with R-Bloggers&#34;&gt;
  &lt;button class=&#34;button is-link is-medium&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-r-project&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span&gt;As seen on R Bloggers&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/div&gt;
&lt;/div&gt;

      </description>
    </item>
    
    
    
    <item>
      <title>Project Euler 25: Fibonacci number with 1000 digits</title>
      <link>https://lucidmanager.org/data-science/project-euler-25/</link>
      <pubDate>Thu, 22 Jun 2017 00:00:00 +0000</pubDate>
      <author>Peter Prevos</author>
      <guid>https://lucidmanager.org/data-science/project-euler-25/</guid>
      <description>
	
&lt;p&gt;
Project Euler 25 asks us back to the Fibonacci numbers. This famous number sequence describes many natural processes, such as the patterns in this beautiful sunflower. The seeds inside the sunflower grow in a spiral pattern, and the number of spirals tends to be a Fibonacci number. This pattern is the most efficient way to pack the centre with as many seeds as possible.&lt;/p&gt;
&lt;p&gt;
Famous mathematician and father of computer science Alan Turing was fascinated by this problem until his death. In 2016, the Royal Society published a journal article that builds on his work in the Open Science journal. They collected sunflowers from &lt;a href=&#34;http://dx.doi.org/10.1098/rsos.160091&#34;&gt;citizen scientists&lt;/a&gt; from around the world and confirmed that for the majority of sunflowers, the number of spirals is a Fibonacci number. They did, however, also discover other patterns. Euler Problem 25 is a bit less poetic as it only asks to generate a huge Fibonacci number.&lt;/p&gt;
&lt;figure&gt;
&lt;div style=&#34;position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden;&#34;&gt;
      &lt;iframe allow=&#34;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share; fullscreen&#34; loading=&#34;eager&#34; referrerpolicy=&#34;strict-origin-when-cross-origin&#34; src=&#34;https://www.youtube.com/embed/DRjFV_DETKQ?autoplay=0&amp;amp;controls=1&amp;amp;end=0&amp;amp;loop=0&amp;amp;mute=0&amp;amp;start=0&#34; style=&#34;position: absolute; top: 0; left: 0; width: 100%; height: 100%; border:0;&#34; title=&#34;YouTube video&#34;&gt;&lt;/iframe&gt;
    &lt;/div&gt;

&lt;figcaption&gt;
Sunflowers and Fibonacci - Numberphile.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;div id=&#34;outline-container-headline-1&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-1&#34;&gt;
Project Euler 25 Definition
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-1&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The Fibonacci sequence is defined by a recurrence relation:&lt;/p&gt;
&lt;p&gt;
$$F_n = F_{n-1} + F_{n-2} \quad | \quad F_1 = 1, F_2 = 1$$&lt;/p&gt;
&lt;p&gt;
The 12&lt;sup&gt;th&lt;/sup&gt; term, \(F_{12}=144\), is the first term to contain three digits.&lt;/p&gt;
&lt;p&gt;
What is the index of the first term in the Fibonacci sequence to &lt;a href=&#34;https://projecteuler.net/problem=25&#34;&gt;contain 1000 digits&lt;/a&gt;?&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-2&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-2&#34;&gt;
Large Integers in R
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-2&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;By default, numbers with more than 7 digits are shown in scientific notation in R, which reduces the accuracy of the calculation. You can change the precision of large integers with the &lt;code class=&#34;verbatim&#34;&gt;options()&lt;/code&gt; function, but R struggles with integers with more than 22 digits. This example illustrates this issue:&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;factorial&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;24&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;[1] &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;6.204484e+23&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;options&lt;/span&gt;(digits &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;22&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;factorial&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;24&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;[1] &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;620448401733239409999872&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Finding a number with 1000 digits is a problem in R with using a special function or package. This article explains how to solve this problem using the &lt;a href=&#34;https://cran.r-project.org/web/packages/gmp/index.html&#34;&gt;GMP package&lt;/a&gt; and in base R code.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-3&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-3&#34;&gt;
Project Euler 25 Solution in R
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-3&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;We have already seen various ways to generate Fibonacci numbers in R using a recursive function, a while loop and the GMP library in &lt;a href=&#34;https://lucidmanager.org/data-science/project-euler-2/&#34;&gt;Euler Problem 2&lt;/a&gt;.&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-4&#34; class=&#34;outline-3&#34;&gt;
&lt;h3 id=&#34;headline-4&#34;&gt;
Arbitrary-Precision Arithmetic
&lt;/h3&gt;
&lt;div id=&#34;outline-text-headline-4&#34; class=&#34;outline-text-3&#34;&gt;
&lt;p&gt;The first solution uses the GMP package to manage large integers. This package implements the GNU Multiple Precision Arithmetic Library for working with huge numbers. This package also contains a function to generate Fibonacci numbers. This solution cycles through the Fibonacci sequence until it finds a number with 1000 digits.&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://github.com/pprevos/ProjectEuler/&#34; target=&#34;_blank&#34;
   title=&#34;Download ProjectEuler from GitHub&#34;
   alt=&#34;Download ProjectEuler from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;ProjectEuler&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt; &lt;span style=&#34;color:#888&#34;&gt;## Project Euler 25: Fibonacci number with 1000 digits&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt; &lt;span style=&#34;color:#888&#34;&gt;## GNU Multiple Precision Arithmetic Library&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(gmp) 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  answer &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  fib &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;while&lt;/span&gt; (&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;nchar&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;as.character&lt;/span&gt;(fib)) &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1000&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      fib &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;fibnum&lt;/span&gt;(answer) &lt;span style=&#34;color:#888&#34;&gt;# Determine next Fibonacci number&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      answer &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; answer &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;print&lt;/span&gt;(answer)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-5&#34; class=&#34;outline-3&#34;&gt;
&lt;h3 id=&#34;headline-5&#34;&gt;
R-base code
&lt;/h3&gt;
&lt;div id=&#34;outline-text-headline-5&#34; class=&#34;outline-text-3&#34;&gt;
&lt;p&gt;This is a very fast solution but my aim is to solve the first 100 Project Euler problems using only base-R code. The &lt;code class=&#34;verbatim&#34;&gt;big.add()&lt;/code&gt; function I developed to solve &lt;a href=&#34;https://lucidmanager.org/data-science/project-euler-13/&#34;&gt;Euler Problem 13&lt;/a&gt; uses only basic R to add large numbers. This function multiplies large numbers in the same way you would do on a piece of paper. This code to solve Euler Problem 25 is much slower than the GMP library but it was fun to develop.&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Base-R Solution&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  fib &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt; &lt;span style=&#34;color:#888&#34;&gt;# First Fibonaci number&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  cur &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt; &lt;span style=&#34;color:#888&#34;&gt;# Current number in sequence&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  pre &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt; &lt;span style=&#34;color:#888&#34;&gt;# Previous number in sequence&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  answer &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;while&lt;/span&gt; (&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;nchar&lt;/span&gt;(fib) &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1000&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      fib &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;big.add&lt;/span&gt;(cur, pre) &lt;span style=&#34;color:#888&#34;&gt;# Determine next Fibonacci number&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      pre &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; cur
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      cur &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; fib
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      answer &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; answer &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;print&lt;/span&gt;(answer)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;a href = &#34;https://www.r-bloggers.com/&#34; target=&#34;_blank&#34; title=&#34;Proudly associated with R-Bloggers&#34;&gt;
  &lt;button class=&#34;button is-link is-medium&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-r-project&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span&gt;As seen on R Bloggers&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

      </description>
    </item>
    
    
    
    <item>
      <title>Project Euler 24: Lexicographic Permutations</title>
      <link>https://lucidmanager.org/data-science/project-euler-24/</link>
      <pubDate>Thu, 15 Jun 2017 00:00:00 +0000</pubDate>
      <author>Peter Prevos</author>
      <guid>https://lucidmanager.org/data-science/project-euler-24/</guid>
      <description>
	
&lt;p&gt;
Project Euler 24 asks to develop lexicographic permutations. These are ordered arrangements of objects in lexicographic order. Tushar Roy of Coding Made Simple has shared a great introduction on how to generate lexicographic permutations.&lt;/p&gt;
&lt;figure&gt;
&lt;div style=&#34;position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden;&#34;&gt;
      &lt;iframe allow=&#34;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share; fullscreen&#34; loading=&#34;eager&#34; referrerpolicy=&#34;strict-origin-when-cross-origin&#34; src=&#34;https://www.youtube.com/embed/nYFd7VHKyWQ?autoplay=0&amp;amp;controls=1&amp;amp;end=0&amp;amp;loop=0&amp;amp;mute=0&amp;amp;start=0&#34; style=&#34;position: absolute; top: 0; left: 0; width: 100%; height: 100%; border:0;&#34; title=&#34;YouTube video&#34;&gt;&lt;/iframe&gt;
    &lt;/div&gt;

&lt;figcaption&gt;
String Permutation Algorithm.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;div id=&#34;outline-container-headline-1&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-1&#34;&gt;
Project Euler 24 Definition
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-1&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;A permutation is an ordered arrangement of objects. For example, 3124 is one possible permutation of the digits 1, 2, 3 and 4. If all of the permutations are listed numerically or alphabetically, we call it lexicographic order. The lexicographic permutations of 0, 1 and 2 are:&lt;/p&gt;
&lt;p&gt;
$$012 \quad 021 \quad 102 \quad 120 \quad 201 \quad 210$$&lt;/p&gt;
&lt;p&gt;
What is the &lt;a href=&#34;https://projecteuler.net/problem=24&#34;&gt;millionth lexicographic permutation&lt;/a&gt; of the digits 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9?&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-2&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-2&#34;&gt;
Proposed Solution
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-2&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The digits 0 to 9 have $10! = 362880$  permutations (including combinations that start with 0). Most of these permutations are, however, not in lexicographic order. A brute-force way to solve the problem is to determine the next lexicographic permutation of a number string and repeat this one million times.
This code takes the following steps:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Find largest index $i$ such that$a_{i-1} &amp;lt; a_i$.&lt;/li&gt;
&lt;li&gt;If no such index exists, then this is already the last permutation.&lt;/li&gt;
&lt;li&gt;Find largest index $j$ such that $j \geq i$ and $a_j &amp;gt; a_{i-1}$.&lt;/li&gt;
&lt;li&gt;Swap $a_j$  and $a_{i-1}$.&lt;/li&gt;
&lt;li&gt;Reverse the suffix starting at $a_i$.&lt;/li&gt;
&lt;/ul&gt;
&lt;a href = &#34;https://github.com/pprevos/ProjectEuler/&#34; target=&#34;_blank&#34;
   title=&#34;Download ProjectEuler from GitHub&#34;
   alt=&#34;Download ProjectEuler from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;ProjectEuler&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;


&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Project Euler 24: Lexicographic permutations&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  nextPerm &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;function&lt;/span&gt;(a) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#888&#34;&gt;# Find longest non-increasing suffix&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      i &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;length&lt;/span&gt;(a)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;while&lt;/span&gt; (i &lt;span style=&#34;color:#333&#34;&gt;&amp;gt;&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;&amp;amp;&amp;amp;&lt;/span&gt; a[i &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;] &lt;span style=&#34;color:#333&#34;&gt;&amp;gt;=&lt;/span&gt; a[i])
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          i &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; i &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#888&#34;&gt;# i is the head index of the suffix&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#888&#34;&gt;# Are we at the last permutation?&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (i &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;) &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;return&lt;/span&gt; (&lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;NA&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#888&#34;&gt;# a[i - 1] is the pivot&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#888&#34;&gt;# Frind rightmost element that exceeds the pivot&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      j &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;length&lt;/span&gt;(a)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;while&lt;/span&gt; (a[j] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;=&lt;/span&gt; a[i &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;]) 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          j &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; j &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#888&#34;&gt;# Swap pivot with j&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      temp &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; a[i &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      a[i &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; a[j]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      a[j] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; temp
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#888&#34;&gt;# Reverse the suffix&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      a[i&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;length&lt;/span&gt;(a)] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;rev&lt;/span&gt;(a[i&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;length&lt;/span&gt;(a)])
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;return&lt;/span&gt;(a)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  numbers &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;9&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;for&lt;/span&gt; (i &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;in&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1E6&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;)) numbers &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;nextPerm&lt;/span&gt;(numbers)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  answer &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;paste&lt;/span&gt;(numbers, collapse &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;print&lt;/span&gt;(answer)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-3&#34; class=&#34;outline-3&#34;&gt;
&lt;h3 id=&#34;headline-3&#34;&gt;
Combinatorics
&lt;/h3&gt;
&lt;div id=&#34;outline-text-headline-3&#34; class=&#34;outline-text-3&#34;&gt;
&lt;p&gt;A more efficient solution is to use combinatorics, thanks to the now defunct &lt;em&gt;MathBlog&lt;/em&gt;. The last nine digits can be ordered in 9! = 362880 ways. So the first 9! permutations start with a 0. By extending this thought, it follows that the millionth permutation must start with a 2.&lt;/p&gt;
&lt;p&gt;
$$\lfloor (1000000 - 1) / 9 ! \rfloor  = 2$$&lt;/p&gt;
&lt;p&gt;
From this rule, it follows that the 725761&lt;sup&gt;st&lt;/sup&gt; permutation is 2,013,456,789. We now need 274,239 more lexicographic permutations:&lt;/p&gt;
&lt;p&gt;
$$(1000000 - 1) - (2 \times 9!) = 274239$$&lt;/p&gt;
&lt;p&gt;
We can repeat this logic to find the next digit. The last 8 digits can be ordered in 40320 ways. The second digit is the 6th digit in the remaining numbers, which is 7 (2013456789).&lt;/p&gt;
&lt;p&gt;
$$\lfloor 274239 / 8 ! \rfloor  = 6$$&lt;/p&gt;
&lt;p&gt;
$$274239 - (6 \times 7 !) = 32319$$&lt;/p&gt;
&lt;p&gt;
This process is repeated until all digits have been used.&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  numbers &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;9&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  n &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;length&lt;/span&gt;(numbers)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  answer &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;vector&lt;/span&gt;(length &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  remain &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1E6&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;for&lt;/span&gt; (i &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;in&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;n) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      j &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;floor&lt;/span&gt;(remain &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;factorial&lt;/span&gt;(n &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; i))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      answer[i] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; numbers[j &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      remain &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; remain &lt;span style=&#34;color:#333&#34;&gt;%%&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;factorial&lt;/span&gt;(n &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; i)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      numbers &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; numbers[&lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt;(j &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;)]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  answer &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;paste&lt;/span&gt;(answer, collapse &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;print&lt;/span&gt;(answer)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
&lt;a href = &#34;https://www.r-bloggers.com/&#34; target=&#34;_blank&#34; title=&#34;Proudly associated with R-Bloggers&#34;&gt;
  &lt;button class=&#34;button is-link is-medium&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-r-project&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span&gt;As seen on R Bloggers&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

      </description>
    </item>
    
    
    
    <item>
      <title>Tic Tac Toe Simulation: The Intelligent Minimax Algorithm</title>
      <link>https://lucidmanager.org/data-science/tic-tac-toe-war-games/</link>
      <pubDate>Thu, 08 Jun 2017 00:00:00 +0000</pubDate>
      <author>Peter Prevos</author>
      <guid>https://lucidmanager.org/data-science/tic-tac-toe-war-games/</guid>
      <description>
	
&lt;p&gt;
One of my favourite movies is &lt;em&gt;WarGames&lt;/em&gt;. One of the most memorable scenes is where the protagonist tries to teach the out-of-control AI that a nuclear war is unwinnable with a Tic Tac Toe simulation.&lt;/p&gt;
&lt;p&gt;
Tic Tac Toe might be a futile children&amp;#39;s game but it can also teach us about artificial intelligence. Tic Tac Toe, or Noughts and Crosses, is a &lt;a href=&#34;https://en.wikipedia.org/wiki/Zero-sum_game&#34;&gt;zero-sum game&lt;/a&gt; with perfect information. Both players have full information about each other. When nobody makes a mistake, the game always ends in a draw.&lt;/p&gt;
&lt;p&gt;
This article describes how to self-playing game of Tic Tac Toe, complete with a graphical interface, in the R language. This article is part of a series about &lt;a href=&#34;https://lucidmanager.org/tags/r-games/&#34;&gt;computer games&lt;/a&gt; in the R Language.&lt;/p&gt;
&lt;p&gt;
The first two parts of the article describe how to create the user interface and add a human player. The second part delves into creating computer players. My ultimate aim is to recreate this iconic scene in the &lt;em&gt;&lt;a href=&#34;https://www.youtube.com/watch?v=F7qOV8xonfY&#34;&gt;WarGames&lt;/a&gt;&lt;/em&gt; movie.&lt;/p&gt;
&lt;figure&gt;
&lt;div style=&#34;position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden;&#34;&gt;
      &lt;iframe allow=&#34;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share; fullscreen&#34; loading=&#34;eager&#34; referrerpolicy=&#34;strict-origin-when-cross-origin&#34; src=&#34;https://www.youtube.com/embed/F7qOV8xonfY?autoplay=0&amp;amp;controls=1&amp;amp;end=0&amp;amp;loop=0&amp;amp;mute=0&amp;amp;start=0&#34; style=&#34;position: absolute; top: 0; left: 0; width: 100%; height: 100%; border:0;&#34; title=&#34;YouTube video&#34;&gt;&lt;/iframe&gt;
    &lt;/div&gt;

&lt;figcaption&gt;
Tic Tac Toe With Joshua (1983).
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
&lt;a href = &#34;https://github.com/pprevos/RGames/&#34; target=&#34;_blank&#34;
   title=&#34;Download RGames from GitHub&#34;
   alt=&#34;Download RGames from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;RGames&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-1&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-1&#34;&gt;
The Game Board
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-1&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;This first code snippet draws the Tic Tac Toe simulation board. The variable &lt;code class=&#34;verbatim&#34;&gt;xo&lt;/code&gt; holds the identity of the pieces and the vector &lt;code class=&#34;verbatim&#34;&gt;board&lt;/code&gt; holds the current game. The game board is a vector of length nine consisting of either -1 (X), 0 (empty field) or 1 (O). The vector indices correspond with locations on the game board:&lt;/p&gt;
&lt;div class=&#34;center-block&#34; style=&#34;text-align: center; margin-left: auto; margin-right: auto;&#34;&gt;
&lt;div class=&#34;src src-text&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-text&#34; data-lang=&#34;text&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;1 2 3
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;4 5 6
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;7 8 9&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
The second part of the code checks for three in a row and draws the corresponding line.&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#888&#34;&gt;## Tic Tac Toe Simulation in the R language&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#888&#34;&gt;## Peter Prevos&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#888&#34;&gt;## lucidmanager.org&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  draw.board &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;function&lt;/span&gt;(game) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      xo &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;X&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34; &amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;O&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#888&#34;&gt;# Symbols&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;par&lt;/span&gt;(mar &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;rep&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;,&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;4&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;plot.new&lt;/span&gt;()
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;plot.window&lt;/span&gt;(xlim &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;,&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;30&lt;/span&gt;), ylim &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;,&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;30&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;abline&lt;/span&gt;(h &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;20&lt;/span&gt;), col &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;darkgrey&amp;#34;&lt;/span&gt;, lwd &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;4&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;abline&lt;/span&gt;(v &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;20&lt;/span&gt;), col &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;darkgrey&amp;#34;&lt;/span&gt;, lwd &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;4&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;text&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;rep&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;5&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;15&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;25&lt;/span&gt;), &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;3&lt;/span&gt;), &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;rep&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;25&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;3&lt;/span&gt;), &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;rep&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;15&lt;/span&gt;,&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;3&lt;/span&gt;), &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;rep&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;5&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;3&lt;/span&gt;)), 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;           xo[game &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;], cex &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;4&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#888&#34;&gt;# Identify location of any three in a row&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      square &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;matrix&lt;/span&gt;(game, nrow &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;3&lt;/span&gt;, byrow &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;TRUE&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      hor &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;abs&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;rowSums&lt;/span&gt;(square))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;any&lt;/span&gt;(hor &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;3&lt;/span&gt;)) 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          hor &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; (&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;4&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;which&lt;/span&gt;(hor &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;3&lt;/span&gt;)) &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;5&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;else&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          hor &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      ver &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;abs&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;colSums&lt;/span&gt;(square))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;any&lt;/span&gt;(ver &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;3&lt;/span&gt;)) 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          ver &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;which&lt;/span&gt;(ver &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;3&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;5&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;else&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          ver &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      diag1 &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sum&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;diag&lt;/span&gt;(square))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      diag2 &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sum&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;diag&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;t&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;apply&lt;/span&gt;(square, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;, rev)))) 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#888&#34;&gt;# Draw winning lines &lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;all&lt;/span&gt;(hor &lt;span style=&#34;color:#333&#34;&gt;&amp;gt;&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;for&lt;/span&gt; (i &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;in&lt;/span&gt; hor)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;              &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;lines&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;30&lt;/span&gt;), &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;rep&lt;/span&gt;(i, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;), lwd &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt;, col&lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;red&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;all&lt;/span&gt;(ver &lt;span style=&#34;color:#333&#34;&gt;&amp;gt;&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;for&lt;/span&gt; (i &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;in&lt;/span&gt; ver)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;              &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;lines&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;rep&lt;/span&gt;(i, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;), &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;30&lt;/span&gt;), lwd &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt;, col&lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;red&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;abs&lt;/span&gt;(diag1) &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;3&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;lines&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;28&lt;/span&gt;), &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;28&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;), lwd &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt;, col &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;red&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;abs&lt;/span&gt;(diag2) &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;3&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;lines&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;28&lt;/span&gt;), &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;28&lt;/span&gt;), lwd &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt;, col &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;red&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-2&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-2&#34;&gt;
Human Player
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-2&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;This second code snippet lets a human player move by clicking anywhere on the graphic display using the &lt;a href=&#34;https://www.rdocumentation.org/packages/graphics/versions/3.6.2/topics/locator&#34;&gt;locator&lt;/a&gt; function. The click location is converted to a number to denote the position on the board. The entered field is only accepted if it has not yet been used (the &lt;code class=&#34;verbatim&#34;&gt;empty&lt;/code&gt; variable contains the available fields).&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;move.human &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;function&lt;/span&gt;(game) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;text&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;4&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Click on screen to move&amp;#34;&lt;/span&gt;, col &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;grey&amp;#34;&lt;/span&gt;, cex&lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;.7&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    empty &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;which&lt;/span&gt;(game &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    move &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;while&lt;/span&gt; (&lt;span style=&#34;color:#333&#34;&gt;!&lt;/span&gt;move &lt;span style=&#34;color:#333&#34;&gt;%in%&lt;/span&gt; empty) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        coords &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;locator&lt;/span&gt;(n &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;) &lt;span style=&#34;color:#888&#34;&gt;# add lines&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        coords&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;x &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;floor&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;abs&lt;/span&gt;(coords&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;x) &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        coords&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;y &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;floor&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;abs&lt;/span&gt;(coords&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;y) &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        move &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; coords&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;x &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;3&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; (&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;3&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; coords&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;y)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;return&lt;/span&gt;(move)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-3&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-3&#34;&gt;
Computer Player
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-3&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The computer uses a modified &lt;a href=&#34;https://en.wikipedia.org/wiki/Minimax&#34;&gt;Minimax Algorithm&lt;/a&gt; to determine its next move. This article from the &lt;a href=&#34;https://neverstopbuilding.com/minimax&#34;&gt;Never Stop Building&lt;/a&gt; blog explains this method in great detail.&lt;/p&gt;
&lt;p&gt;
If the computer moves first, the algorithm takes a while because of the large number of permutations. There
are &lt;a href=&#34;http://www.se16.info/hgb/tictactoe.htm&#34;&gt;255,168 possible legal games&lt;/a&gt; in Tic Tac Toe, 46,080 (18%) of which end in a draw.&lt;/p&gt;
&lt;p&gt;
The code for this part is contributed by a reader (see comments below).  I originally made a mistake in the recurring function and Alberto was so kind to contribute a correct version.&lt;/p&gt;
&lt;p&gt;
The &lt;em&gt;ganador&lt;/em&gt; (Spanish for winning) function assesses the board condition by assigning -10 or + 10 for a winning game and 0 for any other situation.&lt;/p&gt;
&lt;p&gt;
The minimax function returns a list with the move and its valuation through the ganador function. The function calls itself until it has filled the board and retains the best scoring move using the minimax method. Random variables are added to avoid the computer always playing the same move in the same situation.&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#888&#34;&gt;## Minimax code by Alberto C.&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;ganador &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;function&lt;/span&gt;(juego, player) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    game &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;matrix&lt;/span&gt;(juego, nrow &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;3&lt;/span&gt;, byrow &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#007020&#34;&gt;T&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    hor &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;rowSums&lt;/span&gt;(game)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    ver &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;colSums&lt;/span&gt;(game)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    diag &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sum&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;diag&lt;/span&gt;(game)), &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sum&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;diag&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;apply&lt;/span&gt;(game, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;, rev))))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;-3&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;%in%&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(hor, ver, diag))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;return&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;-10&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;3&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;%in%&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(hor, ver, diag))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;return&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;else&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;return&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;minimax &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;function&lt;/span&gt;(juego, player) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    free &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;which&lt;/span&gt;(juego &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;length&lt;/span&gt;(free) &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        juego[free] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; player
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;return&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;list&lt;/span&gt;(move &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; free, U &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ganador&lt;/span&gt;(juego, player)))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    poss.results &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;rep&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;9&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;for&lt;/span&gt;(i &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;in&lt;/span&gt; free) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        game &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; juego
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        game[i] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; player
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        poss.results[i] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ganador&lt;/span&gt;(game, player)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    mm &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ifelse&lt;/span&gt;(player &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;-1&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;which.min&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;which.max&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;any&lt;/span&gt;(poss.results &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; (player &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt;))) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        move &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;do.call&lt;/span&gt;(mm, &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;list&lt;/span&gt;(poss.results))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;return&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;list&lt;/span&gt;(move &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; move, U &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; poss.results[move]))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;for&lt;/span&gt;(i &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;in&lt;/span&gt; free) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        game &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; juego
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        game[i] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; player
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        poss.results[i] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;minimax&lt;/span&gt;(game, &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt;player)&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;U
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    random &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;runif&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;9&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0.1&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    poss.results[&lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt;free] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;100&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt;player
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    poss.results &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; poss.results &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; (player &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; random)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    move &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;do.call&lt;/span&gt;(mm, &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;list&lt;/span&gt;(poss.results))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;return&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;list&lt;/span&gt;(move &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; move, U &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; poss.results[move]))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-4&#34; class=&#34;outline-3&#34;&gt;
&lt;h3 id=&#34;headline-4&#34;&gt;
Play Tic Tac Toe
&lt;/h3&gt;
&lt;div id=&#34;outline-text-headline-4&#34; class=&#34;outline-text-3&#34;&gt;
&lt;p&gt;The last bit of code defines a function to play the game. Each layer can be either a human or a computer. The function loops while there are still empty spots on the board and no winner is declared.&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;tic.tac.toe &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;function&lt;/span&gt;(player1 &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;human&amp;#34;&lt;/span&gt;, player2 &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;computer&amp;#34;&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    game &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;rep&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;9&lt;/span&gt;) &lt;span style=&#34;color:#888&#34;&gt;# Empty board&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    winner &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt; &lt;span style=&#34;color:#888&#34;&gt;# Define winner&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    player &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt; &lt;span style=&#34;color:#888&#34;&gt;# First player&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    players &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(player1, player2)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;draw.board&lt;/span&gt;(game)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;while&lt;/span&gt; (&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;%in%&lt;/span&gt; game &lt;span style=&#34;color:#333&#34;&gt;&amp;amp;&lt;/span&gt; winner &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;) { &lt;span style=&#34;color:#888&#34;&gt;# Keep playing until win or full board&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (players&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;[&lt;/span&gt;(player &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;3&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;%%&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;3&lt;/span&gt;] &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;human&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#888&#34;&gt;# Human player&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            move &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;move.human&lt;/span&gt;(game)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;else&lt;/span&gt; { &lt;span style=&#34;color:#888&#34;&gt;# Computer player&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            move &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;minimax&lt;/span&gt;(game, player)[[1]]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        game[move] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; player &lt;span style=&#34;color:#888&#34;&gt;# Change board&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;draw.board&lt;/span&gt;(game)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        winner &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ganador&lt;/span&gt;(game, player)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        player &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt;player &lt;span style=&#34;color:#888&#34;&gt;# Change player&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (winner &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;text&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;15&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;15&lt;/span&gt; , &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;DRAW&amp;#34;&lt;/span&gt;, col &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;red&amp;#34;&lt;/span&gt;, cex &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;tic.tac.toe&lt;/span&gt;()&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-5&#34; class=&#34;outline-3&#34;&gt;
&lt;h3 id=&#34;headline-5&#34;&gt;
Wargames Tic Tac Toe Simulation
&lt;/h3&gt;
&lt;div id=&#34;outline-text-headline-5&#34; class=&#34;outline-text-3&#34;&gt;
&lt;p&gt;Now all elements are in place to recreate the simulation in the &lt;em&gt;Wargames&lt;/em&gt; movie. The animation package helps to create an animated gif where the computer keeps playing itself. All games end in a draw.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/rgames/wargames.gif&#34; alt=&#34;Wargames Tic Tac Toe simulation&#34; title=&#34;Wargames Tic Tac Toe simulation&#34;/&gt;
&lt;figcaption&gt;
Wargames Tic Tac Toe simulation.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
&lt;a href = &#34;https://www.r-bloggers.com/&#34; target=&#34;_blank&#34; title=&#34;Proudly associated with R-Bloggers&#34;&gt;
  &lt;button class=&#34;button is-link is-medium&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-r-project&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span&gt;As seen on R Bloggers&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

      </description>
    </item>
    
    
    
    <item>
      <title>Project Euler 23: Non-Abundant Sums</title>
      <link>https://lucidmanager.org/data-science/project-euler-23/</link>
      <pubDate>Thu, 01 Jun 2017 00:00:00 +0000</pubDate>
      <author>Peter Prevos</author>
      <guid>https://lucidmanager.org/data-science/project-euler-23/</guid>
      <description>
	
&lt;p&gt;
Project Euler 23 asks us to solve a problem with abundant or excessive numbers. These are numbers for which the sum of its proper divisors is higher than the number itself.&lt;/p&gt;
&lt;p&gt;
Twelve is an abundant number because the sum of its proper divisors (the aliquot sum) is larger than 12: (1 + 2 + 3 + 4 + 6 = 16). All highly composite numbers or anti-primes greater than six are abundant numbers. These numbers have so many divisors that they are the opposite of primes.&lt;/p&gt;
&lt;figure&gt;
&lt;div style=&#34;position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden;&#34;&gt;
      &lt;iframe allow=&#34;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share; fullscreen&#34; loading=&#34;eager&#34; referrerpolicy=&#34;strict-origin-when-cross-origin&#34; src=&#34;https://www.youtube.com/embed/2JM2oImb9Qg?autoplay=0&amp;amp;controls=1&amp;amp;end=0&amp;amp;loop=0&amp;amp;mute=0&amp;amp;start=0&#34; style=&#34;position: absolute; top: 0; left: 0; width: 100%; height: 100%; border:0;&#34; title=&#34;YouTube video&#34;&gt;&lt;/iframe&gt;
    &lt;/div&gt;

&lt;figcaption&gt;
5040 and other Anti-Prime Numbers - Numberphile.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;div id=&#34;outline-container-headline-1&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-1&#34;&gt;
Project Euler 23 Definition
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-1&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;A perfect number is a number for which the sum of its proper divisors is exactly equal to the number. For example, the sum of the proper divisors of 28 would be 1 + 2 + 4 + 7 + 14 = 28, which means that 28 is a perfect number. A number n is called deficient if the sum of its proper divisors is less than n and it is called abundant if this sum exceeds n.&lt;/p&gt;
&lt;p&gt;
As 12 is the smallest abundant number, 1 + 2 + 3 + 4 + 6 = 16, the lowest number that can be written as the sum of two abundant numbers is 24. By mathematical analysis, it can be shown that all integers greater than 28123 can be written as the sum of two abundant numbers. However, this upper limit cannot be reduced any further by analysis, even though it is known that the highest number that cannot be expressed as the sum of two abundant numbers is less than this limit.&lt;/p&gt;
&lt;p&gt;
Find the &lt;a href=&#34;https://projecteuler.net/problem=23&#34;&gt;sum of all the positive integers&lt;/a&gt; which cannot be written as the sum of two abundant numbers.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-2&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-2&#34;&gt;
Proposed Solution
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-2&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;This solution recycles the divisors function that determines the proper divisors for a number, introduced for &lt;a href=&#34;https://lucidmanager.org/data-science/project-euler-21/&#34;&gt;Euler Problem 21&lt;/a&gt;. The first code snippet creates the sequence of all abundant numbers up to 28123 (&lt;a href=&#34;https://oeis.org/A005101&#34;&gt;sequence A005101&lt;/a&gt; in the OEIS). An abundant number is one where its aliquot sum (sum of proper divisors) is larger than itself.&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://github.com/pprevos/ProjectEuler/&#34; target=&#34;_blank&#34;
   title=&#34;Download ProjectEuler from GitHub&#34;
   alt=&#34;Download ProjectEuler from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;ProjectEuler&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Project Euler 23: Non-Abundant Sums&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;source&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;project-euler-021.R&amp;#34;&lt;/span&gt;, verbose &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;FALSE&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  abundant &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;function&lt;/span&gt;(x){
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      abundant &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;vector&lt;/span&gt;()
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      a &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;for&lt;/span&gt; (n &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;in&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;x) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          sigma &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sum&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;divisors&lt;/span&gt;(n))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (sigma &lt;span style=&#34;color:#333&#34;&gt;&amp;gt;&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; n) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;              abundant[a] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; n
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;              a &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; a &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;return&lt;/span&gt;(abundant)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The solution to this problem is provided in &lt;a href=&#34;http://oeis.org/A048242&#34;&gt;sequence A048242&lt;/a&gt; of the Online Encyclopedia of Integer Sequences. This website states that the highest number in this sequence is 20161, not 28123, as stated in the problem definition.&lt;/p&gt;
&lt;p&gt;
The second section of code creates a list of all potential numbers that are not the sum of two abundant numbers. The next bit of code sieves any sum of two abundant numbers from the list. You find the answer by adding the remaining numbers in the sequence.&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  abundant &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;abundant&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;28123&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  A048242 &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;20161&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;for&lt;/span&gt; (i &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;in&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;length&lt;/span&gt;(abundant)) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;for&lt;/span&gt; (j &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;in&lt;/span&gt; i&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;length&lt;/span&gt;(abundant)) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (abundant[i] &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; abundant[j] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;20161&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;              A048242[abundant[i] &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; abundant[j]] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;NA&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  A048242 &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; A048242[&lt;span style=&#34;color:#333&#34;&gt;!&lt;/span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;is.na&lt;/span&gt;(A048242)]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  answer &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sum&lt;/span&gt;(A048242)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;print&lt;/span&gt;(answer)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;a href = &#34;https://www.r-bloggers.com/&#34; target=&#34;_blank&#34; title=&#34;Proudly associated with R-Bloggers&#34;&gt;
  &lt;button class=&#34;button is-link is-medium&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-r-project&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span&gt;As seen on R Bloggers&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/div&gt;
&lt;/div&gt;

      </description>
    </item>
    
    
    
    <item>
      <title>Project Euler 22: Names Scores</title>
      <link>https://lucidmanager.org/data-science/project-euler-22/</link>
      <pubDate>Thu, 25 May 2017 00:00:00 +0000</pubDate>
      <author>Peter Prevos</author>
      <guid>https://lucidmanager.org/data-science/project-euler-22/</guid>
      <description>
	
&lt;p&gt;
Project Euler 22 is another trivial task that takes us to the realm of ASCII codes. ASCII is a method to convert symbols into numbers, initially invented for telegraphs. &lt;/p&gt;
&lt;p&gt;
Back in the 8-bit days, &lt;a href=&#34;https://www.asciiart.eu/&#34;&gt;ASCII art&lt;/a&gt; was a method to create images without using lots of memory. Each image consists of a collection of text characters that give the illusion of an image. &lt;/p&gt;
&lt;figure&gt;
&lt;pre class=&#34;example&#34;&gt;
        ,-&amp;#34;&amp;#34;&amp;#34;&amp;#34;&amp;#34;&amp;#34;-.        
     /\j__/\  (  \`--.    
hjw  \`@_@&amp;#39;/  _)  &amp;gt;--.`.  
    _{.:Y:_}_{{_,&amp;#39;    ) ) 
   {_}`-^{_} ```     (_/ .
&lt;/pre&gt;
&lt;figcaption&gt;
Art by Hayley Jane Wakenshaw.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
Euler problem 22 is, unfortunately, a bit less poetic. This problem is similar to &lt;a href=&#34;https://lucidmanager.org/data-science/project-euler-17/&#34;&gt;Euler problem 17&lt;/a&gt; that asks to count the length of words expressing numbers.&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-1&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-1&#34;&gt;
Project Euler 22 Definition
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-1&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;Using &lt;code class=&#34;verbatim&#34;&gt;names.txt&lt;/code&gt;, a 46K text file containing over five-thousand first names, begin by sorting it into alphabetical order. Then working out the alphabetic value for each name, multiply this value by its alphabetical position in the list to obtain a name score.&lt;/p&gt;
&lt;p&gt;
For example, when the list is sorted into alphabetical order, COLIN, which is worth 3 + 15 + 12 + 9 + 14 = 53, is the 938&lt;sup&gt;th&lt;/sup&gt; name in the list. So, COLIN would obtain a score of 938 × 53 = 49714.&lt;/p&gt;
&lt;p&gt;
What is the &lt;a href=&#34;https://projecteuler.net/problem=22&#34;&gt;total of all the name scores&lt;/a&gt; in the file?&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-2&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-2&#34;&gt;
Proposed Solution
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-2&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;This code reads and cleans the file and sorts the names alphabetically. The charToRaw function determines the numerical value of each character in each name. This output of this function is the hex ASCII code for each character. The letter A is number 65, so we subtract 64 from each value to get the sum total.&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://github.com/pprevos/ProjectEuler/&#34; target=&#34;_blank&#34;
   title=&#34;Download ProjectEuler from GitHub&#34;
   alt=&#34;Download ProjectEuler from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;ProjectEuler&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Project Euler 22: Names Scores&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  names &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;readLines&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;https://projecteuler.net/project/resources/p022_names.txt&amp;#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                     warn &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;FALSE&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  names &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;unlist&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;strsplit&lt;/span&gt;(names, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;,&amp;#34;&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  names &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;gsub&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;[[:punct:]]&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;&amp;#34;&lt;/span&gt;, names)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  names &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sort&lt;/span&gt;(names)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;# Total Name scores&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  answer &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;for&lt;/span&gt; (i &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;in&lt;/span&gt; names) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      value &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sum&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sapply&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;unlist&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;strsplit&lt;/span&gt;(i, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;&amp;#34;&lt;/span&gt;)),
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                   &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;function&lt;/span&gt;(x) &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;as.numeric&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;charToRaw&lt;/span&gt;(x)) &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;64&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      value &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; value &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;which&lt;/span&gt;(names&lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt;i)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      answer &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; answer &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; value
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;print&lt;/span&gt;(answer)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Modern computers in a globalised world don&amp;#39;t use ASCII because it cannot encode the tens of thousands of characters that are needed for the world&amp;#39;s languages. The video below explains the Unicode system to encode the world&amp;#39;s symbols.&lt;/p&gt;
&lt;figure&gt;
&lt;p&gt;&lt;div style=&#34;position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden;&#34;&gt;
      &lt;iframe allow=&#34;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share; fullscreen&#34; loading=&#34;eager&#34; referrerpolicy=&#34;strict-origin-when-cross-origin&#34; src=&#34;https://www.youtube.com/embed/MijmeoH9LT4?autoplay=0&amp;amp;controls=1&amp;amp;end=0&amp;amp;loop=0&amp;amp;mute=0&amp;amp;start=0&#34; style=&#34;position: absolute; top: 0; left: 0; width: 100%; height: 100%; border:0;&#34; title=&#34;YouTube video&#34;&gt;&lt;/iframe&gt;
    &lt;/div&gt;

&lt;a href = &#34;https://www.r-bloggers.com/&#34; target=&#34;_blank&#34; title=&#34;Proudly associated with R-Bloggers&#34;&gt;
  &lt;button class=&#34;button is-link is-medium&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-r-project&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span&gt;As seen on R Bloggers&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;
&lt;/p&gt;
&lt;figcaption&gt;
Characters, Symbols and the Unicode Miracle - Computerphile.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;/div&gt;
&lt;/div&gt;

      </description>
    </item>
    
    
    
    <item>
      <title>Project Euler 21: Amicable Numbers</title>
      <link>https://lucidmanager.org/data-science/project-euler-21/</link>
      <pubDate>Thu, 18 May 2017 00:00:00 +0000</pubDate>
      <author>Peter Prevos</author>
      <guid>https://lucidmanager.org/data-science/project-euler-21/</guid>
      <description>
	
&lt;p&gt;
Project Euler 21 takes us to the realm of amicable numbers. &lt;a href=&#34;https://oeis.org/A259180&#34;&gt;Sequence A259180&lt;/a&gt; in the OEIS lists all amicable, or friendly numbers. These are the most romantic numbers in all of maths. Amicable numbers &lt;/p&gt;
&lt;p&gt;
Recreational mathematicians love to find numbers with special properties. Other such special numbers are &amp;#39;perfect numbers&amp;#39;, which equal the sum of its proper divisors. Mathematicians have also defined sociable numbers and betrothed numbers, which are similar to amicable numbers. But perhaps these are for another Euler problem. &lt;a href=&#34;https://lucidmanager.org/data-science/project-euler-23/&#34;&gt;Euler Problem 23&lt;/a&gt; is about finding abundant numbers, which are numbers for which the sum of its proper divisors is higher than the number itself.&lt;/p&gt;
&lt;p&gt;
Amicable numbers were known to the Pythagoreans, who credited them with many mystical properties. Before we had access to computers, finding amicable numbers was a task that required a lot of patience. No algorithm can systematically generate all amicable numbers, and until 1946 only 390 pairs were known. Medieval Muslim mathematicians developed several formulas to create amicable numbers, but the only way to be complete is by using brute force.&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-1&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-1&#34;&gt;
Project Euler 21 Definition
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-1&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;Let \(d(n)\) be defined as the sum of proper divisors of \(n\) (numbers less than \(n\) which divide evenly into \(n\)). If \(d(a) = b\) and \(d(b) = a\), where \(a \neq b\), then \(a\) and \(b\) are an amicable pair and each of \(a\) and \(b\) are called amicable numbers.&lt;/p&gt;
&lt;p&gt;
For example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110; therefore \(d(220) = 284\). The proper divisors of 284 are 1, 2, 4, 71 and 142; so, \(d(284) = 220\).&lt;/p&gt;
&lt;p&gt;
Evaluate the &lt;a href=&#34;https://projecteuler.net/problem=21&#34;&gt;sum of all the amicable numbers&lt;/a&gt; under 10000.&lt;/p&gt;
&lt;figure&gt;
&lt;div style=&#34;position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden;&#34;&gt;
      &lt;iframe allow=&#34;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share; fullscreen&#34; loading=&#34;eager&#34; referrerpolicy=&#34;strict-origin-when-cross-origin&#34; src=&#34;https://www.youtube.com/embed/fUSZBVYZdKY?autoplay=0&amp;amp;controls=1&amp;amp;end=0&amp;amp;loop=0&amp;amp;mute=0&amp;amp;start=0&#34; style=&#34;position: absolute; top: 0; left: 0; width: 100%; height: 100%; border:0;&#34; title=&#34;YouTube video&#34;&gt;&lt;/iframe&gt;
    &lt;/div&gt;

&lt;figcaption&gt;
220 and 284 (Amicable Numbers) - Numberphile.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-2&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-2&#34;&gt;
Proposed Solution
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-2&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The first part of the code provides for a function to list all proper divisors for a given integer &lt;code class=&#34;verbatim&#34;&gt;x&lt;/code&gt;. &lt;/p&gt;
&lt;p&gt;
The loop determines the divisors for numbers 220 to 10,000, calculates their sum and then checks if these numbers are amicable. &lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://github.com/pprevos/ProjectEuler/&#34; target=&#34;_blank&#34;
   title=&#34;Download ProjectEuler from GitHub&#34;
   alt=&#34;Download ProjectEuler from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;ProjectEuler&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Project Euler 21: Amicable Numbers&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  divisors &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;function&lt;/span&gt;(x) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    divisors &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;vector&lt;/span&gt;()
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    d &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;for&lt;/span&gt; (i &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;in&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;floor&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sqrt&lt;/span&gt;(x))) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (x &lt;span style=&#34;color:#333&#34;&gt;%%&lt;/span&gt; i &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        divisors[d] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; i
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (i &lt;span style=&#34;color:#333&#34;&gt;!=&lt;/span&gt; x&lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt;i) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          d &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; d &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          divisors[d] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; x &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt; i
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        d &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; d &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;return&lt;/span&gt;(divisors)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  answer &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  n &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;220&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;while&lt;/span&gt; (n &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10000&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    div.sum &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sum&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;divisors&lt;/span&gt;(n)) &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; n
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (n &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sum&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;divisors&lt;/span&gt;(div.sum)) &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; div.sum &lt;span style=&#34;color:#333&#34;&gt;&amp;amp;&lt;/span&gt; n &lt;span style=&#34;color:#333&#34;&gt;!=&lt;/span&gt; div.sum) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      answer &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; answer &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; n
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;print&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;paste0&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;(&amp;#34;&lt;/span&gt;, n, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;, &amp;#34;&lt;/span&gt;, div.sum, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;)&amp;#34;&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    } 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    n &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; n &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;print&lt;/span&gt;(answer)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;a href = &#34;https://www.r-bloggers.com/&#34; target=&#34;_blank&#34; title=&#34;Proudly associated with R-Bloggers&#34;&gt;
  &lt;button class=&#34;button is-link is-medium&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-r-project&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span&gt;As seen on R Bloggers&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/div&gt;
&lt;/div&gt;

      </description>
    </item>
    
    
    
    <item>
      <title>Project Euler 20: Factorial Digit Sum</title>
      <link>https://lucidmanager.org/data-science/project-euler-20/</link>
      <pubDate>Thu, 11 May 2017 00:00:00 +0000</pubDate>
      <author>Peter Prevos</author>
      <guid>https://lucidmanager.org/data-science/project-euler-20/</guid>
      <description>
	
&lt;p&gt;
Project Euler 20 is the third problem that requires special consideration for working with huge integers. In this problem, we look at factorials. These numbers are useful in combinatorics if, for example, you like to know in how many ways you can arrange a deck of cards.&lt;/p&gt;
&lt;p&gt;
The problem with computing factorials is that they are mostly vast numbers, beyond the generic capabilities of computers to process. For example, a humble pack of playing cards can be shuffled in \(52! = 80658175170943878571660636856403766975289505440883277824000000000000\) different configurations.&lt;/p&gt;
&lt;p&gt;
The largest number traditional handheld calculators can factorise is the mythical number 69. &lt;/p&gt;
&lt;figure&gt;
&lt;div style=&#34;position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden;&#34;&gt;
      &lt;iframe allow=&#34;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share; fullscreen&#34; loading=&#34;eager&#34; referrerpolicy=&#34;strict-origin-when-cross-origin&#34; src=&#34;https://www.youtube.com/embed/kw6l_uTakRA?autoplay=0&amp;amp;controls=1&amp;amp;end=0&amp;amp;loop=0&amp;amp;mute=0&amp;amp;start=0&#34; style=&#34;position: absolute; top: 0; left: 0; width: 100%; height: 100%; border:0;&#34; title=&#34;YouTube video&#34;&gt;&lt;/iframe&gt;
    &lt;/div&gt;

&lt;figcaption&gt;
69! - Numberphile.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;div id=&#34;outline-container-headline-1&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-1&#34;&gt;
Project Euler 20 Definition
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-1&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;\(n!\) means \(n \times (n − 1) \times \ldots \times 3 \times 2 \times 1\)&lt;/p&gt;
&lt;p&gt;
For example, \(10! = 10 \times 9 \times \ldots \times 2 \times 1 = 3628800\),
and the sum of the digits in the number \(10!\) is \(3 + 6 + 2 + 8 + 8 + 0 + 0 = 27\).&lt;/p&gt;
&lt;p&gt;
Find the &lt;a href=&#34;https://projecteuler.net/problem=20&#34;&gt;sum of the digits in the number 100!&lt;/a&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-2&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-2&#34;&gt;
Proposed Solution
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-2&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The factorial of the number 100 contains 158 digits, which is a lot more digits than a 64-bit operating system can accurately produce. Using the standard function: &lt;code class=&#34;verbatim&#34;&gt;factorial(100) = 9.332622e+157&lt;/code&gt;. Without using a specialised algorithm, we cannot determine the sum of all digits. We need to deploy arbitrary-precision arithmetic to solve this problem.&lt;/p&gt;
&lt;p&gt;
This problem can be solved using a specialised R package and using only base-R code. Many computer languages, including R, have special libraries to deal with such large numbers. The &lt;a href=&#34;https://cran.r-project.org/web/packages/gmp/index.html&#34;&gt;GMP package&lt;/a&gt; renders this problem almost trivial.&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://github.com/pprevos/ProjectEuler/&#34; target=&#34;_blank&#34;
   title=&#34;Download ProjectEuler from GitHub&#34;
   alt=&#34;Download ProjectEuler from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;ProjectEuler&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Project Euler 20: Factorial Digit Sum&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(gmp)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  digits &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;factorialZ&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;100&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  digits &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;as.character&lt;/span&gt;(digits)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  answer &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sum&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;as.numeric&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;unlist&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;strsplit&lt;/span&gt;(digits, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;&amp;#34;&lt;/span&gt;))))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;print&lt;/span&gt;(answer)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-3&#34; class=&#34;outline-3&#34;&gt;
&lt;h3 id=&#34;headline-3&#34;&gt;
Using base-R code
&lt;/h3&gt;
&lt;div id=&#34;outline-text-headline-3&#34; class=&#34;outline-text-3&#34;&gt;
&lt;p&gt;The problem becomes more interesting when only using basic R code. I developed the big.add function to solve Euler Problem 13 through the addition of very large integers. We can extend this function to also calculate factorials. A factorial can be replaced by a series of additions, for example:&lt;/p&gt;
&lt;p&gt;
$$3! = 1 \times 2 \times 3 = (((1+1) + (1+1)) + (1+1))$$&lt;/p&gt;
&lt;p&gt;
This can be mimicked in R using the Reduce function. This function reduces a vector to a single value by recursively calling a function. &lt;code class=&#34;verbatim&#34;&gt;Reduce(&amp;#34;+&amp;#34;, rep(4, 5))&lt;/code&gt; is the same as:&lt;/p&gt;
&lt;p&gt;
$$4 \times 5 = ((((4 + 4) + 4) + 4) + 4) = 20$$&lt;/p&gt;
&lt;p&gt;
Using a loop, we can use the Reduce function to calculate a factorial, using only additions:&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#888&#34;&gt;## Base-R Code&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;fact &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;x &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;100&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;for&lt;/span&gt; (i &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;in&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;x) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    fact &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;Reduce&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;+&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;rep&lt;/span&gt;(fact, i))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;print&lt;/span&gt;(fact)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
The &lt;code class=&#34;verbatim&#34;&gt;big.factorial()&lt;/code&gt; function below implements this idea by combining the &lt;code class=&#34;verbatim&#34;&gt;big.add()&lt;/code&gt; and &lt;code class=&#34;verbatim&#34;&gt;Reduce()&lt;/code&gt; functions to calculate large integer factorials. The function returns a value of 1, for factorial of 0 or 1. This function does not calculate the Gamma-function for fractions. For all other values, it goes through a loop from 2 to the requested factorial. The temporary values are stored in the bf variable. The code loops through the factorials by using the result of the previous Reduce call into the current one.&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;source&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;problem013.R&amp;#34;&lt;/span&gt;, echo &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;FALSE&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  big.factorial &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;function&lt;/span&gt;(x) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      x &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;floor&lt;/span&gt;(x)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      bf &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (x &lt;span style=&#34;color:#333&#34;&gt;&amp;gt;&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;for&lt;/span&gt; (i &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;in&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;x) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;              bf &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;Reduce&lt;/span&gt;(big.add, &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;rep&lt;/span&gt;(bf,i))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;return&lt;/span&gt; (bf)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  digits &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;big.factorial&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;100&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  answer &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sum&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;as.numeric&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;unlist&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;strsplit&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;as.character&lt;/span&gt;(digits), &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;&amp;#34;&lt;/span&gt;))))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;print&lt;/span&gt;(answer)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;a href = &#34;https://www.r-bloggers.com/&#34; target=&#34;_blank&#34; title=&#34;Proudly associated with R-Bloggers&#34;&gt;
  &lt;button class=&#34;button is-link is-medium&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-r-project&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span&gt;As seen on R Bloggers&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

      </description>
    </item>
    
    
    
    <item>
      <title>Project Euler 19: Counting Sundays</title>
      <link>https://lucidmanager.org/data-science/project-euler-19/</link>
      <pubDate>Thu, 04 May 2017 00:00:00 +0000</pubDate>
      <author>Peter Prevos</author>
      <guid>https://lucidmanager.org/data-science/project-euler-19/</guid>
      <description>
	
&lt;p&gt;
Project Euler 19 is so trivial it is almost not worth writing an article about. One exciting aspect of this problem is the naming of weekdays and deciding which day the week starts. This issue is more complicated than it sounds because data science is, in essence, not about data but about people.&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-1&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-1&#34;&gt;
Project Euler 19 Definition
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-1&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;You are given the following information, but you may prefer to do some research for yourself.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;1 January 1900 was a Monday.&lt;/li&gt;
&lt;li&gt;Thirty days has September, April, June and November.&lt;/li&gt;
&lt;li&gt;All the rest have thirty-one,&lt;/li&gt;
&lt;li&gt;Saving February alone, Which has twenty-eight, rain or shine. And on leap years, twenty-nine.&lt;/li&gt;
&lt;li&gt;A leap year occurs on any year evenly divisible by 4, but not on a century unless it is divisible by 400.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;a href=&#34;https://projecteuler.net/problem=19&#34;&gt;How many Sundays&lt;/a&gt; fell on the first of the month during the twentieth century (1 January 1901 to 31 December 2000)?&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-2&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-2&#34;&gt;
Proposed Solution
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-2&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The problem can be quickly solved with R base code and a tiny bit faster when using the lubridate package.&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://github.com/pprevos/ProjectEuler/&#34; target=&#34;_blank&#34;
   title=&#34;Download ProjectEuler from GitHub&#34;
   alt=&#34;Download ProjectEuler from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;ProjectEuler&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Project Euler 19: Counting Sundays&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Base-R&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  dates &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;seq.Date&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;as.Date&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;1901-01-01&amp;#34;&lt;/span&gt;), &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;as.Date&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;2000-12-31&amp;#34;&lt;/span&gt;), &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;days&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  days &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;rep&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;7&lt;/span&gt;, length.out &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;length&lt;/span&gt;(dates))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  answer &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sum&lt;/span&gt;(days&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;[substr&lt;/span&gt;(dates, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;9&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;01&amp;#34;&lt;/span&gt;] &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;print&lt;/span&gt;(answer)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Using Lubridate&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(lubridate)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  dates &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;seq.Date&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;as.Date&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;1901-01-01&amp;#34;&lt;/span&gt;), &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;as.Date&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;2000-12-31&amp;#34;&lt;/span&gt;), &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;months&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  answer &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sum&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;wday&lt;/span&gt;(dates) &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;print&lt;/span&gt;(answer)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
To draw out this post a little bit further, I wrote some code to solve the problem without using any of the calendar functions in R.&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Calculate from scratch&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  week.day &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  answer &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;for&lt;/span&gt; (y &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;in&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1901&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2000&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;for&lt;/span&gt; (m &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;in&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;12&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          max.day &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;31&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (m &lt;span style=&#34;color:#333&#34;&gt;%in%&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;4&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;6&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;9&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;11&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;              max.day &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;30&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          &lt;span style=&#34;color:#888&#34;&gt;# Leap years&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (m &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;              &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (y &lt;span style=&#34;color:#333&#34;&gt;%%&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;4&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;&amp;amp;&lt;/span&gt; y &lt;span style=&#34;color:#333&#34;&gt;%%&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;100&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;!=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;|&lt;/span&gt; y &lt;span style=&#34;color:#333&#34;&gt;%%&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;400&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;) 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                  max.day &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;29&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;              &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;else&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                  max.day &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;28&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;for&lt;/span&gt; (d &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;in&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;max.day) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;              week.day &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; week.day &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;              &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (week.day &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;8&lt;/span&gt;) week.day &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;              &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (week.day &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;&amp;amp;&lt;/span&gt; d &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;              answer &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; answer &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;print&lt;/span&gt;(answer)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-3&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-3&#34;&gt;
Which day does the week start?
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-3&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The only aspect remotely interesting about this problem is the conversion from weekdays to numbers. In R, Monday is considered day one, which makes sense in the Christian context of Western culture. Saturday and Sunday are the weekends, the two last days of the week so they are day 6 and 7. According to international standard ISO 8601, Monday is the first day of the week. Although this is the international standard, several countries, including the United States and Canada, consider Sunday to be the first day of the week.&lt;/p&gt;
&lt;p&gt;
The international standard is biased towards Christianity. The Christian or Western world marks Sunday as their day of rest and worship. Muslims refer to Friday as their day of rest and prayer. The Jewish calendar counts Saturday—the Sabbath—as the day of rest and worship. This idea is also shared by Seventh-Day Adventists.&lt;/p&gt;
&lt;p&gt;
This Euler problem shows that data science is not only about data: it is always how people &lt;a href=&#34;https://lucidmanager.org/data-science/qualitative-data-science/&#34;&gt;interpret the world&lt;/a&gt;.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/project-euler/problem-019.png&#34; alt=&#34;First day of the week around the world.&#34; title=&#34;First day of the week around the world.&#34; width=&#34;600&#34;/&gt;
&lt;figcaption&gt;
First day of the week around the world.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## First day of the week&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(tidyverse)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  firstday &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;read_csv&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;data/first_day.csv&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;map_data&lt;/span&gt;(map &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;world&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;full_join&lt;/span&gt;(firstday) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ggplot&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;aes&lt;/span&gt;(long, lat, group &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; group, fill &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; firstday)) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;geom_polygon&lt;/span&gt;() &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;theme_void&lt;/span&gt;() &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;coord_fixed&lt;/span&gt;() &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;labs&lt;/span&gt;(title &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;First day of the week&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ggsave&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;images/problem-019.png&amp;#34;&lt;/span&gt;, width &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;6&lt;/span&gt;, height &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;4&lt;/span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;figure&gt;
&lt;div style=&#34;position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden;&#34;&gt;
      &lt;iframe allow=&#34;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share; fullscreen&#34; loading=&#34;eager&#34; referrerpolicy=&#34;strict-origin-when-cross-origin&#34; src=&#34;https://www.youtube.com/embed/JEyuQd-zMeg?autoplay=0&amp;amp;controls=1&amp;amp;end=0&amp;amp;loop=0&amp;amp;mute=0&amp;amp;start=0&#34; style=&#34;position: absolute; top: 0; left: 0; width: 100%; height: 100%; border:0;&#34; title=&#34;YouTube video&#34;&gt;&lt;/iframe&gt;
    &lt;/div&gt;

&lt;figcaption&gt;
Where do the Days of the Week Get Their Names?
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
&lt;a href = &#34;https://www.r-bloggers.com/&#34; target=&#34;_blank&#34; title=&#34;Proudly associated with R-Bloggers&#34;&gt;
  &lt;button class=&#34;button is-link is-medium&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-r-project&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span&gt;As seen on R Bloggers&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;

      </description>
    </item>
    
    
    
    <item>
      <title>R-Cade Games: Simulating the Legendary Pong Game in R</title>
      <link>https://lucidmanager.org/data-science/pong/</link>
      <pubDate>Thu, 20 Apr 2017 00:00:00 +0000</pubDate>
      <author>Peter Prevos</author>
      <guid>https://lucidmanager.org/data-science/pong/</guid>
      <description>
	
&lt;p&gt;
Pong is one of the earliest &lt;a href=&#34;https://en.wikipedia.org/wiki/Arcade_game&#34;&gt;arcade games&lt;/a&gt; on the market, first released in 1972. From the day I first saw this miracle box, I wanted to know more about computers. There was something magical about being able to influence what happens on the television screen, even though the graphics left everything to t he imagintaion.&lt;/p&gt;
&lt;figure&gt;
&lt;div style=&#34;position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden;&#34;&gt;
      &lt;iframe allow=&#34;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share; fullscreen&#34; loading=&#34;eager&#34; referrerpolicy=&#34;strict-origin-when-cross-origin&#34; src=&#34;https://www.youtube.com/embed/fiShX2pTz9A?autoplay=0&amp;amp;controls=1&amp;amp;end=0&amp;amp;loop=0&amp;amp;mute=0&amp;amp;start=0&#34; style=&#34;position: absolute; top: 0; left: 0; width: 100%; height: 100%; border:0;&#34; title=&#34;YouTube video&#34;&gt;&lt;/iframe&gt;
    &lt;/div&gt;

&lt;figcaption&gt;
Original Atari PONG (1972) arcade machine gameplay video.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
I learnt how to write code from the 1983 book &lt;a href=&#34;https://archive.org/details/Dr_Wackos_Guide_to_Creating_Arcade_Games&#34;&gt;Dr. C. Wacko&amp;#39;s Miracle Guide to Designing and Programming your own Atari Computer Arcade Games&lt;/a&gt;. This book explains in a very clear and humorous way how to write computer games in Atari basic. I devoured this book and spent many hours developing silly games. This article is an ode to Dr Wacko, a computer geek&amp;#39;s midlife-crisis and an attempt to replicate the software I developed thirty years ago.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/rgames/dr-wacko.jpg&#34; alt=&#34;Dr. C. Wacko&amp;#39;s Miracle Guide to Designing and Programming your own Atari Computer Arcade Games&#34; title=&#34;Dr. C. Wacko&amp;#39;s Miracle Guide to Designing and Programming your own Atari Computer Arcade Games&#34;/&gt;
&lt;figcaption&gt;
Dr. C. Wacko&amp;#39;s Miracle Guide to Designing and Programming your own Atari Computer Arcade Games
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
I showed in a other posts that R can be used for &lt;a href=&#34;https://lucidmanager.org/data-science/tic-tac-toe-war-games/&#34;&gt;board games&lt;/a&gt; or to play a &lt;a href=&#34;https://lucidmanager.org/data-science/text-adventure/&#34;&gt;text adventure&lt;/a&gt;. The question is whether we create arcade games in R. My challenge is to recreate the look and feel of 1980s arcade games, or &lt;em&gt;R-Cade&lt;/em&gt; games, using R? The code shown below simulates the legendary game of pong.&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-1&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-1&#34;&gt;
Playing Pong in R
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-1&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The code is based on the Wacko&amp;#39;s &lt;em&gt;Boing Program&lt;/em&gt; in the above-mentioned book. The R code is fully commented and speaks for itself. Please note that the animation is very clunky when you run it in &lt;a href=&#34;https://support.rstudio.com/hc/en-us/articles/200488548-Problem-with-Plots-or-Graphics-Device&#34;&gt;RStudio&lt;/a&gt;. Only the native R Terminal displays the animation correctly. &lt;/p&gt;
&lt;p&gt;
This version plays automatically with a randomiser routine to render the gameplay imperfect. The skill parameter indicates how good the computer player is between 0 and 1.&lt;/p&gt;
&lt;p&gt;
The &lt;a href=&#34;https://cran.r-project.org/web/packages/beepr/index.html&#34;&gt;beepr&lt;/a&gt; library by Rasmus Bååth provides some much-needed sound.&lt;/p&gt;
&lt;p&gt;
The main issue with this code is that I have not been able to ad a human Pong player. I am not aware of a way to use continuous input in the R language. Perhaps somebody can help me perfect this little ditty. I love to know how to read real-time USB input to control the game, so we get a step closer to the first R-Cade game.&lt;/p&gt;
&lt;figure&gt;
&lt;div style=&#34;position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden;&#34;&gt;
      &lt;iframe allow=&#34;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share; fullscreen&#34; loading=&#34;eager&#34; referrerpolicy=&#34;strict-origin-when-cross-origin&#34; src=&#34;https://www.youtube.com/embed/A6ytTe8KcZ0?autoplay=0&amp;amp;controls=1&amp;amp;end=0&amp;amp;loop=0&amp;amp;mute=0&amp;amp;start=0&#34; style=&#34;position: absolute; top: 0; left: 0; width: 100%; height: 100%; border:0;&#34; title=&#34;YouTube video&#34;&gt;&lt;/iframe&gt;
    &lt;/div&gt;

&lt;figcaption&gt;
R language Pong simulation.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
&lt;a href = &#34;https://github.com/pprevos/RGames/&#34; target=&#34;_blank&#34;
   title=&#34;Download RGames from GitHub&#34;
   alt=&#34;Download RGames from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;RGames&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Simulating the Legendary Pong Game in R&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Sound library&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(beepr) 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Game parameters&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  skill &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0.87&lt;/span&gt; &lt;span style=&#34;color:#888&#34;&gt;# Skill (0-1)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  score &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  high.score &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Define playing field&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;x11&lt;/span&gt;()
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;par&lt;/span&gt;(mar &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;rep&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;,&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;4&lt;/span&gt;), bg &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;black&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;plot.new&lt;/span&gt;()
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;plot.window&lt;/span&gt;(xlim &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;,&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;30&lt;/span&gt;), ylim &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;,&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;30&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;lines&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;30&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;30&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;), &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;30&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;30&lt;/span&gt;), type &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;l&amp;#34;&lt;/span&gt;, lwd &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;5&lt;/span&gt;, col &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;white&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Playing field boundaries (depends on cex)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  xmin &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0.5&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  xmax &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;29.4&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  ymin &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0.5&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  ymax &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;29.4&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## initial position&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  x &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sample&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;5&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;25&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  y &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sample&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;5&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;25&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;points&lt;/span&gt;(x, y, pch &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;15&lt;/span&gt;, col &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;white&amp;#34;&lt;/span&gt;, cex &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Paddle control&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  psize &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;4&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  ypaddle &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; y
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Set direction&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  dx &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;runif&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;.5&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  dy &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;runif&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;.5&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Game play&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;while&lt;/span&gt; (x &lt;span style=&#34;color:#333&#34;&gt;&amp;gt;&lt;/span&gt; xmin &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      sound &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt; &lt;span style=&#34;color:#888&#34;&gt;# Silence&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;Sys.sleep&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;.05&lt;/span&gt;) &lt;span style=&#34;color:#888&#34;&gt;# Pause screen&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;points&lt;/span&gt;(x, y, pch &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;15&lt;/span&gt;, col &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;black&amp;#34;&lt;/span&gt;, cex &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;) &lt;span style=&#34;color:#888&#34;&gt;# Erase ball&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#888&#34;&gt;# Move ball&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      x &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; x &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; dx
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      y &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; y &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; dy
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#888&#34;&gt;# Collision detection&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (x &lt;span style=&#34;color:#333&#34;&gt;&amp;gt;&lt;/span&gt; xmax) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          dx &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt;dx &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;runif&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;.9&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1.1&lt;/span&gt;) &lt;span style=&#34;color:#888&#34;&gt;# Bounce&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (x &lt;span style=&#34;color:#333&#34;&gt;&amp;gt;&lt;/span&gt; xmin) x &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; xmax &lt;span style=&#34;color:#888&#34;&gt;# Boundary&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          sound &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt; &lt;span style=&#34;color:#888&#34;&gt;# Set sound&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (y &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;&lt;/span&gt; ymin &lt;span style=&#34;color:#333&#34;&gt;|&lt;/span&gt; y &lt;span style=&#34;color:#333&#34;&gt;&amp;gt;&lt;/span&gt; ymax) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (y &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;&lt;/span&gt; ymin) y &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; ymin
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (y &lt;span style=&#34;color:#333&#34;&gt;&amp;gt;&lt;/span&gt; ymax) y &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; ymax
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          dy &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt;dy &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;runif&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;.9&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1.1&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          sound &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#888&#34;&gt;# Caught by paddle?&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (x &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;&lt;/span&gt; xmin &lt;span style=&#34;color:#333&#34;&gt;&amp;amp;&lt;/span&gt; (y &lt;span style=&#34;color:#333&#34;&gt;&amp;gt;&lt;/span&gt; ypaddle &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; (psize &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;)) &lt;span style=&#34;color:#333&#34;&gt;&amp;amp;&lt;/span&gt; y &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;&lt;/span&gt; ypaddle &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; (psize &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;)) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (x &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;&lt;/span&gt; xmin) x &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; xmin
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          dx &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt;dx &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;runif&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;.9&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1.1&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          sound &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          score &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; score &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#888&#34;&gt;# Draw ball&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;points&lt;/span&gt;(x, y, pch &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;15&lt;/span&gt;, col &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;white&amp;#34;&lt;/span&gt;, cex &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (sound &lt;span style=&#34;color:#333&#34;&gt;!=&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;) &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;beep&lt;/span&gt;(sound)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#888&#34;&gt;# Move paddle&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;runif&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;&lt;/span&gt; skill) ypaddle &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; ypaddle &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; dy &lt;span style=&#34;color:#888&#34;&gt;# Imperfect follow&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#888&#34;&gt;# Draw paddle&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#888&#34;&gt;# Erase back line&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;lines&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;), &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;30&lt;/span&gt;), type &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;l&amp;#34;&lt;/span&gt;, lwd &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;8&lt;/span&gt;, col &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;black&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#888&#34;&gt;# Keep paddle in window&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (ypaddle &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;&lt;/span&gt; (psize &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;)) ypaddle &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; (psize &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (ypaddle &lt;span style=&#34;color:#333&#34;&gt;&amp;gt;&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;30&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; (psize &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;)) ypaddle &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;30&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; (psize &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#888&#34;&gt;# Draw paddle&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;lines&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;), &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(ypaddle &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; (psize &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;), ypaddle &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; (psize &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;)), type &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;l&amp;#34;&lt;/span&gt;, lwd &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;8&lt;/span&gt;, col &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;white&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;beep&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;8&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;text&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;15&lt;/span&gt;,&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;15&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;GAME OVER&amp;#34;&lt;/span&gt;, cex&lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;5&lt;/span&gt;, col &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;white&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  s &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ifelse&lt;/span&gt;(score &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;s&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;text&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;15&lt;/span&gt;,&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;5&lt;/span&gt;, &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;paste0&lt;/span&gt;(score, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34; Point&amp;#34;&lt;/span&gt;, s), cex&lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;3&lt;/span&gt;, col &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;white&amp;#34;&lt;/span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
&lt;a href = &#34;https://www.r-bloggers.com/&#34; target=&#34;_blank&#34; title=&#34;Proudly associated with R-Bloggers&#34;&gt;
  &lt;button class=&#34;button is-link is-medium&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-r-project&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span&gt;As seen on R Bloggers&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;

      </description>
    </item>
    
    
    
    <item>
      <title>Project Euler 18: Maximum Path Sum</title>
      <link>https://lucidmanager.org/data-science/project-euler-18/</link>
      <pubDate>Mon, 17 Apr 2017 00:00:00 +0000</pubDate>
      <author>Peter Prevos</author>
      <guid>https://lucidmanager.org/data-science/project-euler-18/</guid>
      <description>
	
&lt;p&gt;
Project Euler 18 and 67 are identical, other than that the data in the second version is more extensive than in the first one. In this post, I kill two Eulers with one code. These two problems are logically similar to &lt;a href=&#34;https://lucidmanager.org/data-science/project-euler-15/&#34;&gt;Project Euler 15&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;
These two problems concern binary trees, which is a data structure where each node has two children. A practical example of a binary tree is a pedigree chart, where each person or animal has two parents, four grandparents and so on. In this Project Euler problem, we need to navigate through such a tree.&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-1&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-1&#34;&gt;
Project Euler 18 Definition
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-1&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;By starting at the top of the triangle below and moving to adjacent numbers on the row below, the maximum total from top to bottom is 23.&lt;/p&gt;
&lt;div class=&#34;src src-text&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-text&#34; data-lang=&#34;text&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;   3
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  7 4
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt; 2 4 6
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;8 5 9 3&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
That is, 3 + 7 + 4 + 9 = 23.&lt;/p&gt;
&lt;p&gt;
Find the &lt;a href=&#34;https://projecteuler.net/problem=18&#34;&gt;maximum total&lt;/a&gt; from top to bottom of the triangle below:&lt;/p&gt;
&lt;div class=&#34;src src-text&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-text&#34; data-lang=&#34;text&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                            75
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                          95  64
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                        17  47  82
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                      18  35  87  10
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                    20  04  82  47  65
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                  19  01  23  75  03  34
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                88  02  77  73  07  63  67
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;              99  65  04  28  06  16  70  92
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            41  41  26  56  83  40  80  70  33
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          41  48  72  33  47  32  37  16  94  29
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        53  71  44  65  25  43  91  52  97  51  14
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      70  11  33  28  77  73  17  78  39  68  17  57
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    91  71  52  38  17  14  91  43  58  50  2 7 2 9 48
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  63  66  04  68  89  53  67  30  73  16  69  87  40  31
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;04  62  98  27  23  09  70  98  73  93  38  53  60  04  23&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-2&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-2&#34;&gt;
Solution
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-2&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;This problem seeks a maximum path sum in a binary tree. The brute force method, as indicated in the problem definition, is a very inefficient way to solve this problem. &lt;/p&gt;
&lt;p&gt;
A more efficient method is to define the maximum path layer by layer, starting at the bottom. The maximum sum of 2 + 8 or 2 + 5 is 10, the maximum sum of 4 + 5 or 4 + 9 is 13, and the last maximum sum is 15. These numbers are now placed in the next row. This process cycles until only one number is left. This algorithm solves the sample triangle into four steps:&lt;/p&gt;
&lt;p&gt;
Step 1:&lt;/p&gt;
&lt;div class=&#34;center-block&#34; style=&#34;text-align: center; margin-left: auto; margin-right: auto;&#34;&gt;
&lt;p&gt;3&lt;/p&gt;
&lt;p&gt;
7 4&lt;/p&gt;
&lt;p&gt;
2 4 6&lt;/p&gt;
&lt;p&gt;
8 5 9 3&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;
Step 2:&lt;/p&gt;
&lt;div class=&#34;center-block&#34; style=&#34;text-align: center; margin-left: auto; margin-right: auto;&#34;&gt;
&lt;p&gt;3&lt;/p&gt;
&lt;p&gt;
7 4&lt;/p&gt;
&lt;p&gt;
10 13 15&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;
Step 3:&lt;/p&gt;
&lt;div class=&#34;center-block&#34; style=&#34;text-align: center; margin-left: auto; margin-right: auto;&#34;&gt;
&lt;p&gt;3&lt;/p&gt;
&lt;p&gt;
20 19&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;
Step 4:&lt;/p&gt;
&lt;div class=&#34;center-block&#34; style=&#34;text-align: center; margin-left: auto; margin-right: auto;&#34;&gt;
&lt;p&gt;23&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;
In the code below, the data is a triangle matrix. The variables &lt;code class=&#34;verbatim&#34;&gt;rij&lt;/code&gt; (row) and &lt;code class=&#34;verbatim&#34;&gt;kol&lt;/code&gt; (column) drive the search for the maximum path. The triangle for Euler Problem 18 is manually created, and the triangle for Euler Problem 67 is read from the website.&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://github.com/pprevos/ProjectEuler/&#34; target=&#34;_blank&#34;
   title=&#34;Download ProjectEuler from GitHub&#34;
   alt=&#34;Download ProjectEuler from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;ProjectEuler&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Project Euler 18: Maximum Path Sum&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  path.sum &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;function&lt;/span&gt;(triangle) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;for&lt;/span&gt; (rij &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;in&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;nrow&lt;/span&gt;(triangle)&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;for&lt;/span&gt; (kol &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;in&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ncol&lt;/span&gt;(triangle)&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;-1&lt;/span&gt;)) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;              triangle[rij &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;,kol] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;max&lt;/span&gt;(triangle[rij,kol&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;(kol &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;)]) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; triangle[rij &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;, kol]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          triangle[rij,] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;NA&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;return&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;max&lt;/span&gt;(triangle, na.rm &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;TRUE&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  triangle &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;matrix&lt;/span&gt;(ncol &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;15&lt;/span&gt;, nrow &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;15&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  triangle[1,&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;75&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  triangle[2,&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;95&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;64&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  triangle[3,&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;3&lt;/span&gt;] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;17&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;47&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;82&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  triangle[4,&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;4&lt;/span&gt;] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;18&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;35&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;87&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  triangle[5,&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;5&lt;/span&gt;] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;20&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;04&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;82&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;47&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;65&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  triangle[6,&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;6&lt;/span&gt;] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;19&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;01&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;23&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;75&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;03&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;34&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  triangle[7,&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;7&lt;/span&gt;] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;88&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;02&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;77&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;73&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;07&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;63&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;67&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  triangle[8,&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;8&lt;/span&gt;] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;99&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;65&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;04&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;28&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;06&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;16&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;70&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;92&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  triangle[9,&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;9&lt;/span&gt;] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;41&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;41&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;26&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;56&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;83&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;40&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;80&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;70&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;33&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  triangle[10,&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt;] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;41&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;48&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;72&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;33&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;47&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;32&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;37&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;16&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;94&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;29&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  triangle[11,&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;11&lt;/span&gt;] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;53&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;71&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;44&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;65&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;25&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;43&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;91&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;52&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;97&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;51&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;14&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  triangle[12,&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;12&lt;/span&gt;] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;70&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;11&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;33&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;28&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;77&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;73&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;17&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;78&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;39&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;68&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;17&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;57&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  triangle[13,&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;13&lt;/span&gt;] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;91&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;71&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;52&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;38&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;17&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;14&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;91&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;43&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;58&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;50&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;27&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;29&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;48&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  triangle[14,&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;14&lt;/span&gt;] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;63&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;66&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;04&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;68&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;89&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;53&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;67&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;30&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;73&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;16&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;69&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;87&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;40&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;31&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  triangle[15,&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;15&lt;/span&gt;] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;04&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;62&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;98&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;27&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;23&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;09&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;70&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;98&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;73&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;93&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;38&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;53&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;60&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;04&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;23&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  answer &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;path.sum&lt;/span&gt;(triangle)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;print&lt;/span&gt;(answer)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-3&#34; class=&#34;outline-3&#34;&gt;
&lt;h3 id=&#34;headline-3&#34;&gt;
Project Euler 67
&lt;/h3&gt;
&lt;div id=&#34;outline-text-headline-3&#34; class=&#34;outline-text-3&#34;&gt;
&lt;p&gt;The solution for &lt;a href=&#34;https://projecteuler.net/problem=67&#34;&gt;problem number 67&lt;/a&gt; is exactly the same. The data is read directly from the Project Euler website.&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  triangle.file &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;read.delim&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;https://projecteuler.net/project/resources/p067_triangle.txt&amp;#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                              stringsAsFactors &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;FALSE&lt;/span&gt;, header &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;FALSE&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  triangle.67 &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;matrix&lt;/span&gt;(nrow &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;100&lt;/span&gt;, ncol &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;100&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;for&lt;/span&gt; (i &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;in&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;100&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      triangle.67[i,&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;i] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;as.numeric&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;unlist&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;strsplit&lt;/span&gt;(triangle.file[i,], &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34; &amp;#34;&lt;/span&gt;)))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  answer &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;path.sum&lt;/span&gt;(triangle.67)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;print&lt;/span&gt;(answer)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
This video visualises the quest for the maximum path as a brute-force solution, which takes eleven minutes of hypnotic animation.&lt;/p&gt;
&lt;figure&gt;
&lt;div style=&#34;position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden;&#34;&gt;
      &lt;iframe allow=&#34;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share; fullscreen&#34; loading=&#34;eager&#34; referrerpolicy=&#34;strict-origin-when-cross-origin&#34; src=&#34;https://www.youtube.com/embed/z-AOWBztnpI?autoplay=0&amp;amp;controls=1&amp;amp;end=0&amp;amp;loop=0&amp;amp;mute=0&amp;amp;start=0&#34; style=&#34;position: absolute; top: 0; left: 0; width: 100%; height: 100%; border:0;&#34; title=&#34;YouTube video&#34;&gt;&lt;/iframe&gt;
    &lt;/div&gt;

&lt;figcaption&gt;
Maximum path sum.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
&lt;a href = &#34;https://www.r-bloggers.com/&#34; target=&#34;_blank&#34; title=&#34;Proudly associated with R-Bloggers&#34;&gt;
  &lt;button class=&#34;button is-link is-medium&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-r-project&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span&gt;As seen on R Bloggers&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

      </description>
    </item>
    
    
    
    <item>
      <title>Project Euler 17: Number Letter Counts</title>
      <link>https://lucidmanager.org/data-science/project-euler-17/</link>
      <pubDate>Thu, 23 Mar 2017 00:00:00 +0000</pubDate>
      <author>Peter Prevos</author>
      <guid>https://lucidmanager.org/data-science/project-euler-17/</guid>
      <description>
	
&lt;p&gt;
Project Euler 17 asks to count the letters in numbers written as words. This is a skill we all learnt in primary school mainly useful when writing cheques—to those that still use them. The principle for this problem is similar to &lt;a href=&#34;https://lucidmanager.org/data-science/project-euler-22/&#34;&gt;Project Euler 22&lt;/a&gt;, which analyses given names.&lt;/p&gt;
&lt;p&gt;
Each language has its own rules for writing numbers. My native language Dutch has very different logic to English. Both Dutch and English use compound words after the number twelve.&lt;/p&gt;
&lt;p&gt;
Linguists have theorised this is evidence that early Germanic numbers were duodecimal. The importance of a “dozen” as a counting word and the twelve hours in the clock support this idea. There is even a Dozenal Society that promotes the use of a number system based on 12.&lt;/p&gt;
&lt;p&gt;
The English language changes the rules when reaching the number 21. While we say eight-teen in English, we do no say “one-twenty”. Dutch stays consistent and the last number is always spoken first. For example, 37 in English is “thirty-seven”, while in Dutch it is written as “zevenendertig” (seven and thirty).&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-1&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-1&#34;&gt;
Project Euler 17 Definition
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-1&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total. If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, &lt;a href=&#34;https://projecteuler.net/problem=17&#34;&gt;how many letters would be used&lt;/a&gt;?&lt;/p&gt;
&lt;p&gt;
Do not count spaces or hyphens. For example, 342 (three hundred and forty-two) contains 23 letters, and 115 (one hundred and fifteen) contains 20 letters. The use of “and” when writing out numbers is in compliance with British usage.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-2&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-2&#34;&gt;
Solution
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-2&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The first piece of code provides a function that generates the words for numbers 1 to 999,999. This is more than the problem asks for, but it might be a useful function for another application. The last line concatenates all words and removes the spaces to count the number of letters.&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://github.com/pprevos/ProjectEuler/&#34; target=&#34;_blank&#34;
   title=&#34;Download ProjectEuler from GitHub&#34;
   alt=&#34;Download ProjectEuler from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;ProjectEuler&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Project Euler 17: Number Letter Counts&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  numword.en &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;function&lt;/span&gt;(x) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (x &lt;span style=&#34;color:#333&#34;&gt;&amp;gt;&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;999999&lt;/span&gt;) &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;return&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Error: Oustide my vocabulary&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#888&#34;&gt;# Vocabulary &lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      single &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;one&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;two&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;three&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;four&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;five&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;six&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;seven&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;eight&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;nine&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      teens &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;( &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;ten&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;eleven&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;twelve&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;thirteen&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;fourteen&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;fifteen&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;sixteen&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;seventeen&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;eighteen&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;nineteen&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      tens &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;ten&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;twenty&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;thirty&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;forty&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;fifty&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;sixty&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;seventy&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;eighty&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;ninety&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      numword.10 &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;function&lt;/span&gt; (y) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          a &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; y &lt;span style=&#34;color:#333&#34;&gt;%%&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;100&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (a &lt;span style=&#34;color:#333&#34;&gt;!=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;              and &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ifelse&lt;/span&gt;(y &lt;span style=&#34;color:#333&#34;&gt;&amp;gt;&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;100&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;and&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;              &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (a &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;20&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                  &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;return&lt;/span&gt; (&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(and, &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(single, teens)[a]))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;              &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;else&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                  &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;return&lt;/span&gt; (&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(and, tens&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;[floor&lt;/span&gt;(a &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt;)], single[a &lt;span style=&#34;color:#333&#34;&gt;%%&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt;]))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      numword.100 &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;function&lt;/span&gt; (y) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          a &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; (&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;floor&lt;/span&gt;(y &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;100&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;%%&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;100&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;%%&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (a &lt;span style=&#34;color:#333&#34;&gt;!=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;              &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;return&lt;/span&gt; (&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(single[a], &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;hundred&amp;#34;&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      numword.1000 &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;function&lt;/span&gt;(y) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          a &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; (&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1000&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;floor&lt;/span&gt;(y &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1000&lt;/span&gt;)) &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1000&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (a &lt;span style=&#34;color:#333&#34;&gt;!=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;              &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;return&lt;/span&gt; (&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;numword.100&lt;/span&gt;(a), &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;numword.10&lt;/span&gt;(a), &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;thousand&amp;#34;&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      numword &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;paste&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;numword.1000&lt;/span&gt;(x), &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;numword.100&lt;/span&gt;(x), &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;numword.10&lt;/span&gt;(x)), collapse&lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34; &amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;return&lt;/span&gt; (&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;trimws&lt;/span&gt;(numword))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  answer &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;nchar&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;gsub&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34; &amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;paste0&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sapply&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1000&lt;/span&gt;, numword.en), collapse&lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;&amp;#34;&lt;/span&gt;)))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;print&lt;/span&gt;(answer)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-3&#34; class=&#34;outline-3&#34;&gt;
&lt;h3 id=&#34;headline-3&#34;&gt;
Writing Numbers in Dutch
&lt;/h3&gt;
&lt;div id=&#34;outline-text-headline-3&#34; class=&#34;outline-text-3&#34;&gt;
&lt;p&gt;I went beyond Euler Problem 17 by translating the code to spell numbers in Dutch. Interesting bit of trivia is that it takes 307 fewer characters to spell the numbers 1 to 1000 in Dutch than it does in English. It would be good if other people can submit functions for other languages in the comment section. Perhaps we can create an R package with a multi-lingual function for spelling numbers.&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Nederlandse nummers&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  numword.nl &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;function&lt;/span&gt;(x) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (x &lt;span style=&#34;color:#333&#34;&gt;&amp;gt;&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;999999&lt;/span&gt;) &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;return&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Error: Getal te hoog.&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      single &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;een&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;twee&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;drie&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;vier&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;vijf&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;zes&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;zeven&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;acht&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;negen&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      teens &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;( &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;tien&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;elf&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;twaalf&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;dertien&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;veertien&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;vijftien&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;zestien&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;zeventien&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;achtien&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;negentien&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      tens &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;tien&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;twintig&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;dertig&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;veertig&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;vijftig&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;zestig&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;zeventig&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;tachtig&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;negengtig&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      numword.10 &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;function&lt;/span&gt;(y) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          a &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; y &lt;span style=&#34;color:#333&#34;&gt;%%&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;100&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (a &lt;span style=&#34;color:#333&#34;&gt;!=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;              &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (a &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;20&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                  &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;return&lt;/span&gt; (&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(single, teens)[a])
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;              &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;else&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                  &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;return&lt;/span&gt; (&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(single[a &lt;span style=&#34;color:#333&#34;&gt;%%&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt;], &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;en&amp;#34;&lt;/span&gt;, tens&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;[floor&lt;/span&gt;(a &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt;)]))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      numword.100 &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;function&lt;/span&gt;(y) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          a &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; (&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;floor&lt;/span&gt;(y &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;100&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;%%&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;100&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;%%&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (a &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;              &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;return&lt;/span&gt; (&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;honderd&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (a &lt;span style=&#34;color:#333&#34;&gt;&amp;gt;&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;) 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;              &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;return&lt;/span&gt; (&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(single[a], &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;honderd&amp;#34;&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      numword.1000 &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;function&lt;/span&gt;(y) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          a &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; (&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1000&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;floor&lt;/span&gt;(y &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1000&lt;/span&gt;)) &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1000&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (a &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;              &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;return&lt;/span&gt; (&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;duizend &amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (a &lt;span style=&#34;color:#333&#34;&gt;&amp;gt;&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;              &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;return&lt;/span&gt; (&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;numword.100&lt;/span&gt;(a), &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;numword.10&lt;/span&gt;(a), &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;duizend &amp;#34;&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      numword&lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;paste&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;numword.1000&lt;/span&gt;(x), &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;numword.100&lt;/span&gt;(x), &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;numword.10&lt;/span&gt;(x)), collapse&lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;return&lt;/span&gt; (&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;trimws&lt;/span&gt;(numword))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  antwoord &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;nchar&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;gsub&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34; &amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;paste0&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sapply&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1000&lt;/span&gt;, numword.nl), collapse&lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;&amp;#34;&lt;/span&gt;)))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;print&lt;/span&gt;(antwoord)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;a href = &#34;https://www.r-bloggers.com/&#34; target=&#34;_blank&#34; title=&#34;Proudly associated with R-Bloggers&#34;&gt;
  &lt;button class=&#34;button is-link is-medium&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-r-project&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span&gt;As seen on R Bloggers&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

      </description>
    </item>
    
    
    
    <item>
      <title>Project Euler 16: Power digit sum</title>
      <link>https://lucidmanager.org/data-science/project-euler-16/</link>
      <pubDate>Thu, 16 Mar 2017 00:00:00 +0000</pubDate>
      <author>Peter Prevos</author>
      <guid>https://lucidmanager.org/data-science/project-euler-16/</guid>
      <description>
	
&lt;p&gt;
Project Euler 16 is reminiscent of the &lt;a href=&#34;http://trailblazernetizens.blogspot.com.au/2015/08/the-amazing-chessboard-theory.html&#34;&gt;fable of wheat and chess&lt;/a&gt;. Lahur Sessa invented the game of chess for King Iadava. The king was delighted with the game and asked Lahur to name his reward. Lahur asked the king to place one grain of rice on the first square of a chessboard, two on the next square, four on the third square and so on until the board is filled. &lt;/p&gt;
&lt;p&gt;
The king was happy with his humble request until his mathematicians worked out that it would take millions of tonnes of grain. Assuming there are 25 grains of wheat in a gramme, the last field will contain more than 461,168,602,000 tonnes of grain.&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-1&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-1&#34;&gt;
Project Euler 16 Definition
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-1&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;\(2^{15} = 32768\) and the sum of its digits is \(3 + 2 + 7 + 6 + 8 = 26\). &lt;/p&gt;
&lt;p&gt;
What is the &lt;a href=&#34;https://projecteuler.net/problem=16&#34;&gt;sum of the digits&lt;/a&gt; of the number \(2^{1000}\)?&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-2&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-2&#34;&gt;
Possible Solutions
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-2&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The most straightforward solution uses the GMP package for Multiple Precision Arithmetic to calculate big integers. The &lt;code class=&#34;verbatim&#34;&gt;as.bigz()&lt;/code&gt; function results in a special class of arbitrarily large integer numbers. To add the digits, the results is converted to a string and split into numbers.&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://github.com/pprevos/ProjectEuler/&#34; target=&#34;_blank&#34;
   title=&#34;Download ProjectEuler from GitHub&#34;
   alt=&#34;Download ProjectEuler from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;ProjectEuler&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Project Euler 16: Power digit sum&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(gmp)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  digits &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;as.bigz&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;^1000) &lt;span style=&#34;color:#888&#34;&gt;# Define number&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  answer &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sum&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;as.numeric&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;unlist&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;strsplit&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;as.character&lt;/span&gt;(digits), &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;&amp;#34;&lt;/span&gt;))))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;print&lt;/span&gt;(answer)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
We can also solve this problem in base-R with the &lt;code class=&#34;verbatim&#34;&gt;bigg.add()&lt;/code&gt; used for &lt;a href=&#34;https://lucidmanager.org/data-science/project-euler-13/&#34;&gt;Euler Problem 13&lt;/a&gt;. This function uses basic string operations to add to arbitrarily large numbers. Raising a number to the power of two can also be written as a series of additions:&lt;/p&gt;
&lt;p&gt;
$$2^4 = 2 \times 2 \times 2 \times 2 = ((2+2)+(2+2)) + ((2+2)+(2+2))$$&lt;/p&gt;
&lt;p&gt;
The solution to this problem is to add 2 + 2 then add the outcome of that equation to itself, and so on. Repeat this one thousand times to raise the number two to the power of one thousand.&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Base-R Method&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;source&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;problem013.R&amp;#34;&lt;/span&gt;, echo &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;FALSE&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  pow &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;for&lt;/span&gt; (i &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;in&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1000&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      pow &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;big.add&lt;/span&gt;(pow, pow)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  answer &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sum&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;as.numeric&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;unlist&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;strsplit&lt;/span&gt;(pow, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;&amp;#34;&lt;/span&gt;))))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;print&lt;/span&gt;(answer)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
&lt;a href = &#34;https://www.r-bloggers.com/&#34; target=&#34;_blank&#34; title=&#34;Proudly associated with R-Bloggers&#34;&gt;
  &lt;button class=&#34;button is-link is-medium&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-r-project&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span&gt;As seen on R Bloggers&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;

      </description>
    </item>
    
    
    
    <item>
      <title>Project Euler 15: Lattice Paths</title>
      <link>https://lucidmanager.org/data-science/project-euler-15/</link>
      <pubDate>Thu, 09 Mar 2017 00:00:00 +0000</pubDate>
      <author>Peter Prevos</author>
      <guid>https://lucidmanager.org/data-science/project-euler-15/</guid>
      <description>
	
&lt;p&gt;
Project Euler 15 analyses taxicab geometry. This system replaces the usual distance function with the sum of the absolute differences of their Cartesian coordinates. In other words, the distance a taxi would travel in a grid plan instead of the shortest distance between two points. In chess, the distance between squares on the chessboard for rooks is measured in taxicab distance.&lt;/p&gt;
&lt;p&gt;
The fifteenth Euler problem asks to determine the number of possible routes a taxi can take in a city. This problem is logically similar to &lt;a href=&#34;https://lucidmanager.org/data-science/project-euler-18/&#34;&gt;Euler Problem 18 and 67&lt;/a&gt;.&lt;/p&gt;
&lt;figure&gt;
&lt;div style=&#34;position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden;&#34;&gt;
      &lt;iframe allow=&#34;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share; fullscreen&#34; loading=&#34;eager&#34; referrerpolicy=&#34;strict-origin-when-cross-origin&#34; src=&#34;https://www.youtube.com/embed/Pll5MXk5AMA?autoplay=0&amp;amp;controls=1&amp;amp;end=0&amp;amp;loop=0&amp;amp;mute=0&amp;amp;start=0&#34; style=&#34;position: absolute; top: 0; left: 0; width: 100%; height: 100%; border:0;&#34; title=&#34;YouTube video&#34;&gt;&lt;/iframe&gt;
    &lt;/div&gt;

&lt;figcaption&gt;
Taxicab Geometry.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;div id=&#34;outline-container-headline-1&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-1&#34;&gt;
Project Euler 15 Definition
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-1&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;Starting in the top left corner of a 2 by 2 grid, and only being able to move to the right and down, there are exactly 6 routes to the bottom right corner.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://projecteuler.net/project/images/p015.png&#34; alt=&#34;Project Euler 15: Lattice Paths&#34; title=&#34;Project Euler 15: Lattice Paths&#34; align=&#34;center&#34;/&gt;
&lt;figcaption&gt;
Project Euler 15: Lattice Paths.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
How many such &lt;a href=&#34;https://projecteuler.net/problem=15&#34;&gt;routes are there&lt;/a&gt; through a 20×20 grid?&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-2&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-2&#34;&gt;
proposed Solutions
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-2&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The matrix to solve the problem is one larger than the number of squares in the challenge. Along the edges of the matrix, only one pathway is possible: straight to the right or down. We can calculate the number of possible paths for the other squares by adding the number to the right and below the point under consideration. The code loops through the matrix backwards, adding the number of paths step by step.&lt;/p&gt;
&lt;p&gt;
$$p_{i,j}=p_{i,j-1}+p_{{i+1},j}$$&lt;/p&gt;
&lt;p&gt;
For the two by two lattice the solution space is:&lt;/p&gt;
&lt;div class=&#34;center-block&#34; style=&#34;text-align: center; margin-left: auto; margin-right: auto;&#34;&gt;
&lt;div class=&#34;src src-text&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-text&#34; data-lang=&#34;text&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;6  3  1
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;3  2  1
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;1  1  .&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
The total number of pathways from the upper left corner to the lower right corner is thus 6. This logic can now be applied to a grid of any arbitrary size using the following code. The code defines the lattice and initiates the boundary conditions. The bottom row and the right column are filled with 1 as there is only one solution from these points. The code then calculates the pathways by working backwards through the matrix. The final solution is the number is the first cell.&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://github.com/pprevos/ProjectEuler/&#34; target=&#34;_blank&#34;
   title=&#34;Download ProjectEuler from GitHub&#34;
   alt=&#34;Download ProjectEuler from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;ProjectEuler&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Project Euler 15: Lattice Paths&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;# Define lattice&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  nLattice &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  lattice &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;matrix&lt;/span&gt;(ncol &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; nLattice &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;, nrow &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; nLattice &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;# Boundary conditions&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  lattice[nLattice &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;, &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt;(nLattice &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;)] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  lattice[&lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt;(nLattice &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;), nLattice &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;# Calculate Pathways&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;for&lt;/span&gt; (i &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;in&lt;/span&gt; nLattice&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;for&lt;/span&gt; (j &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;in&lt;/span&gt; nLattice&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          lattice[i,j] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; lattice[i&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;+1&lt;/span&gt;, j] &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; lattice[i, j&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;+1&lt;/span&gt;]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  answer &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; lattice[1,&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;print&lt;/span&gt;(answer)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;a href = &#34;https://www.r-bloggers.com/&#34; target=&#34;_blank&#34; title=&#34;Proudly associated with R-Bloggers&#34;&gt;
  &lt;button class=&#34;button is-link is-medium&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-r-project&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span&gt;As seen on R Bloggers&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/div&gt;
&lt;/div&gt;

      </description>
    </item>
    
    
    
    <item>
      <title>Create Air Travel Route Maps with ggplot2</title>
      <link>https://lucidmanager.org/data-science/create-air-travel-route-maps/</link>
      <pubDate>Tue, 07 Mar 2017 07:24:24 +0000</pubDate>
      <author>Peter Prevos</author>
      <guid>https://lucidmanager.org/data-science/create-air-travel-route-maps/</guid>
      <description>
	
&lt;p&gt;
I have been lucky to fly to a few countries around the world. Like any other bored traveller, I thumb through the airline magazines and look at the &lt;a href=&#34;http://www.airlineroutemaps.com/&#34;&gt;air travel route maps&lt;/a&gt;. These maps are beautifully stylised depictions of the world with gently curved lines between the destinations serviced by the airline. I always wanted such a map for my own travel adventures. This article explains how to create air travel route maps with  ggplot2 in the style of the Emirates Airlines route map.&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-1&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-1&#34;&gt;
Create Air Travel Route Maps using ggplot2
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-1&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The first step was to create a list of all the places I have flown between at least once. Paging through my travel photos and diaries, I managed to create a pretty complete list. The structure of this document is simply a list of all routes (From, To) and every flight only gets counted once. The next step finds the spatial coordinates for each airport by searching Google Maps using the geocode function from the &lt;a href=&#34;https://cran.r-project.org/web/packages/ggmap/index.html&#34;&gt;ggmap&lt;/a&gt; package. You will need a Google API to enable the &lt;a href=&#34;https://lucidmanager.org/data-science/geocoding-with-ggmap/&#34;&gt;geocoding function&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;
In some instances, I had to add the country name to avoid confusion between places. To prevent errors from the Google maps API, I have added a while loop that runs until all destinations have been geocoded. We now we have a data frame of airports with their coordinates and can create air travel route maps. The data frames are merged so that we can create air travel route maps using the &lt;a href=&#34;http://ggplot2.tidyverse.org/reference/geom_segment.html&#34;&gt;curve geom&lt;/a&gt;. The &lt;a href=&#34;http://ggplot2.tidyverse.org/reference/borders.html&#34;&gt;borders&lt;/a&gt; function of ggplot2 creates the map data. The &lt;a href=&#34;https://github.com/slowkow/ggrepel&#34;&gt;ggrepel&lt;/a&gt; package helps to prevent overplotting of text. This code also removes any return flights and splits flights that crossed the date line.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/geography/travel-diary.png&#34; alt=&#34;My personal Air Travel Route Maps in ggplot&#34; title=&#34;My personal Air Travel Route Maps in ggplot&#34;/&gt;
&lt;figcaption&gt;
My personal Air Travel Route Maps in ggplot: A Visual Travel Diary
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
In another article, I have used the same principle to create a route map of flights between islands in the &lt;a href=&#34;https://lucidmanager.org/data-science/pacific-island-hopping/&#34;&gt;Pacific Ocean&lt;/a&gt; using the schedules from several international airlines and to show demonstrate the nonsense of &lt;a href=&#34;https://lucidmanager.org/data-science/flat-earth-mathematics/&#34;&gt;Flat Earth&lt;/a&gt; theories.&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://github.com/pprevos/geography/&#34; target=&#34;_blank&#34;
   title=&#34;Download geography from GitHub&#34;
   alt=&#34;Download geography from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;geography&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Flightpath map&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## https://lucidmanager.org/create-air-travel-route-map&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Init&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(tidyverse)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(ggmap)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(ggrepel)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  api &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;readLines&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;../google-maps.api&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#888&#34;&gt;# Text file with the API key&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;register_google&lt;/span&gt;(key &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; api)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Read flight and airports lists &lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;#flights &amp;lt;- read_csv(&amp;#34;flights.csv&amp;#34;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  flights &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;read_csv&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;flight-map/flights.csv&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  airports_file &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;flight-map/airports.csv&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;file.exists&lt;/span&gt;(airports_file)) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    airports &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;read_csv&lt;/span&gt;(airports_file)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    } &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;else&lt;/span&gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    airports &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;tibble&lt;/span&gt;(airport &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;NA&lt;/span&gt;, lon &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;NA&lt;/span&gt;, lat&lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;NA&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Lookup coordinates&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Some airports need country names to ensure Google finds the correct location&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## The geocoding keeps looping till all coordinates have been found&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  destinations &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;unique&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(flights&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;From, flights&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;To))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  new_destinations &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; destinations[&lt;span style=&#34;color:#333&#34;&gt;!&lt;/span&gt;destinations &lt;span style=&#34;color:#333&#34;&gt;%in%&lt;/span&gt; airports&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;airport]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;while&lt;/span&gt; (&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;length&lt;/span&gt;(new_destinations) &lt;span style=&#34;color:#333&#34;&gt;&amp;gt;&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      new_airports &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;geocode&lt;/span&gt;(new_destinations) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;mutate&lt;/span&gt;(airport &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; new_destinations) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;select&lt;/span&gt;(airport, lon, lat)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      airports &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;rbind&lt;/span&gt;(airports, new_airports) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;filter&lt;/span&gt;(&lt;span style=&#34;color:#333&#34;&gt;!&lt;/span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;is.na&lt;/span&gt;(lon) &lt;span style=&#34;color:#333&#34;&gt;|&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;!&lt;/span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;is.na&lt;/span&gt;(lat))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      new_destinations &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; destinations[&lt;span style=&#34;color:#333&#34;&gt;!&lt;/span&gt;destinations &lt;span style=&#34;color:#333&#34;&gt;%in%&lt;/span&gt; airports&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;airport]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;write_csv&lt;/span&gt;(airports, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;flight-map/airports.csv&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Remove country names&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  airports&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;airport &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;as.character&lt;/span&gt;(airports&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;airport)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  comma &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;regexpr&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;,&amp;#34;&lt;/span&gt;, airports&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;airport)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  airports&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;airport&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;[which&lt;/span&gt;(comma &lt;span style=&#34;color:#333&#34;&gt;&amp;gt;&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;)] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;substr&lt;/span&gt;(airports&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;airport&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;[which&lt;/span&gt;(comma &lt;span style=&#34;color:#333&#34;&gt;&amp;gt;&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;)], &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;, comma[comma &lt;span style=&#34;color:#333&#34;&gt;&amp;gt;&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;] &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Remove return flights&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  d &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;vector&lt;/span&gt;()
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;for&lt;/span&gt; (i &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;in&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;nrow&lt;/span&gt;(flights)) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      d &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;which&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;paste&lt;/span&gt;(flights&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;From, flights&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;To) &lt;span style=&#34;color:#333&#34;&gt;%in%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                 &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;paste&lt;/span&gt;(flights&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;To[i], flights&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;From[i]))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      flights&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;From[d] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;R&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  flights2 &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; flights &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;filter&lt;/span&gt;(From &lt;span style=&#34;color:#333&#34;&gt;!=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;R&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;select&lt;/span&gt;(From, To)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Add coordinates to flight list&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  flights &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;merge&lt;/span&gt;(flights, airports, by.x &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;From&amp;#34;&lt;/span&gt;, by.y &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;airport&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  flights &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;merge&lt;/span&gt;(flights, airports, by.x &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;To&amp;#34;&lt;/span&gt;, by.y &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;airport&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  flights &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; flights &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;select&lt;/span&gt;(From, To, lon.x, lat.x, lon.y, lat.y) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;as_tibble&lt;/span&gt;()
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Split Circumnaviation Flights at -180/180 degrees&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  circ &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;which&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;abs&lt;/span&gt;(flights&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;lon.y &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; flights&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;lon.x) &lt;span style=&#34;color:#333&#34;&gt;&amp;gt;&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;180&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  flights[circ,]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  flights&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;lon.y[circ] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ifelse&lt;/span&gt;(flights&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;lon.y[circ] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;180&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;-180&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  flights&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;lat.y[circ] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;rowSums&lt;/span&gt;(flights[circ, &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;lat.x&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;lat.y&amp;#34;&lt;/span&gt;)]) &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  leg2 &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; airports &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;filter&lt;/span&gt;(airport &lt;span style=&#34;color:#333&#34;&gt;%in%&lt;/span&gt; flights&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;To[circ]) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;mutate&lt;/span&gt;(From &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;rep&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;length&lt;/span&gt;(circ))) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;mutate&lt;/span&gt;(lon.x &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt;flights&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;lon.y[circ], lat.x &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; flights&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;lat.y[circ]) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;select&lt;/span&gt;(From, To &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; airport, lon.x, lat.x, lon.y &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; lon, lat.y &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; lat)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  flights &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;rbind&lt;/span&gt;(flights, leg2)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Plot flight routes&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  worldmap &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;borders&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;world2&amp;#34;&lt;/span&gt;, colour&lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;#efede1&amp;#34;&lt;/span&gt;, fill&lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;#efede1&amp;#34;&lt;/span&gt;) 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ggplot&lt;/span&gt;() &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; worldmap &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;geom_point&lt;/span&gt;(data &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; airports, &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;aes&lt;/span&gt;(x &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; lon, y &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; lat), col &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;#970027&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;geom_text_repel&lt;/span&gt;(data&lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt;airports, &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;aes&lt;/span&gt;(x &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; lon, y &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; lat, label &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; airport), col &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;black&amp;#34;&lt;/span&gt;, size &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;, segment.color &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;NA&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;geom_curve&lt;/span&gt;(data &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; flights, &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;aes&lt;/span&gt;(x &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; lon.x, y &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; lat.x, xend &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; lon.y, yend &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; lat.y), col &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;#b29e7d&amp;#34;&lt;/span&gt;, size &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;.4&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;theme_void&lt;/span&gt;()
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ggsave&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;flights_map.png&amp;#34;&lt;/span&gt;, dpi &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;300&lt;/span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
&lt;a href = &#34;https://www.r-bloggers.com/&#34; target=&#34;_blank&#34; title=&#34;Proudly associated with R-Bloggers&#34;&gt;
  &lt;button class=&#34;button is-link is-medium&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-r-project&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span&gt;As seen on R Bloggers&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;

      </description>
    </item>
    
    
    
    <item>
      <title>Project Euler 14: Longest Collatz sequence</title>
      <link>https://lucidmanager.org/data-science/project-euler-14/</link>
      <pubDate>Thu, 02 Mar 2017 00:00:00 +0000</pubDate>
      <author>Peter Prevos</author>
      <guid>https://lucidmanager.org/data-science/project-euler-14/</guid>
      <description>
	
&lt;p&gt;
Euler Problem 14 looks at the Collatz Conjecture. These playful sequences, named after German mathematician Lothar Collatz (1910–1990), cause mathematicians a lot of headaches. What is most interesting about this problem is how a simple set of rules can create intricate complexity.&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-1&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-1&#34;&gt;
Project Euler 14 Definition
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-1&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The following iterative sequence is defined for the set of positive integers:&lt;/p&gt;
&lt;p&gt;
$f(n) = \begin{cases} \frac{n}{2} &amp;amp;\text{if } n \equiv 0 \pmod{2}\\[4px] 3n+1 &amp;amp; \text{if } n\equiv 1 \pmod{2} .\end{cases}$&lt;/p&gt;
&lt;p&gt;
Using the rule above, and starting with 13, we generate the following sequence:&lt;/p&gt;
&lt;p&gt;
$$13 \rightarrow 40 \rightarrow 20 \rightarrow 10 \rightarrow 5 \rightarrow 16 \rightarrow 8 \rightarrow 4 \rightarrow 2 \rightarrow 1 $$&lt;/p&gt;
&lt;p&gt;
It can be seen that this sequence (starting at 13 and finishing at 1) contains 10 terms. Although it has not been proved yet (Collatz Problem), it is thought that all starting numbers finish at 1.&lt;/p&gt;
&lt;p&gt;
Which starting number, under one million, produces &lt;a href=&#34;https://projecteuler.net/problem=14&#34;&gt;the longest chain&lt;/a&gt;?&lt;/p&gt;
&lt;p&gt;
NOTE: Once the chain starts, the terms are allowed to go above one million.&lt;/p&gt;
&lt;figure&gt;
&lt;div style=&#34;position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden;&#34;&gt;
      &lt;iframe allow=&#34;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share; fullscreen&#34; loading=&#34;eager&#34; referrerpolicy=&#34;strict-origin-when-cross-origin&#34; src=&#34;https://www.youtube.com/embed/5mFpVDpKX70?autoplay=0&amp;amp;controls=1&amp;amp;end=0&amp;amp;loop=0&amp;amp;mute=0&amp;amp;start=0&#34; style=&#34;position: absolute; top: 0; left: 0; width: 100%; height: 100%; border:0;&#34; title=&#34;YouTube video&#34;&gt;&lt;/iframe&gt;
    &lt;/div&gt;

&lt;figcaption&gt;
Uncrackable? The Collatz Conjecture - Numberphile.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-2&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-2&#34;&gt;
Proposed solutions
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-2&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;This problem is highly computationally intensive, and it highlights R&amp;#39;s lack of speed. Generating one million Collatz sequences and finding the longest one requires a lot more than a minute of processing time allowed for in Project Euler.&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://github.com/pprevos/ProjectEuler/&#34; target=&#34;_blank&#34;
   title=&#34;Download ProjectEuler from GitHub&#34;
   alt=&#34;Download ProjectEuler from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;ProjectEuler&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-3&#34; class=&#34;outline-3&#34;&gt;
&lt;h3 id=&#34;headline-3&#34;&gt;
Brute Force
&lt;/h3&gt;
&lt;div id=&#34;outline-text-headline-3&#34; class=&#34;outline-text-3&#34;&gt;
&lt;p&gt;The brute force solution tests every answer and takes a long time to run.&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Project Euler 14: Longest Collatz Sequence&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Brute Force&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  collatz.chain &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;function&lt;/span&gt;(n) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      chain &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;vector&lt;/span&gt;()
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      i &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;while&lt;/span&gt; (n &lt;span style=&#34;color:#333&#34;&gt;!=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (n&lt;span style=&#34;color:#333&#34;&gt;%%&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;              n &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; n &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;else&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          n &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;3&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; n &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          chain[i] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; n
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          i &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; i &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;return&lt;/span&gt;(chain)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  answer &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  collatz.max &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;for&lt;/span&gt; (n &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;in&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1E6&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      collatz.length &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;length&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;collatz.chain&lt;/span&gt;(n))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (collatz.length &lt;span style=&#34;color:#333&#34;&gt;&amp;gt;&lt;/span&gt; collatz.max) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          answer &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; n
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          collatz.max &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; collatz.length
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;print&lt;/span&gt;(answer)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-4&#34; class=&#34;outline-3&#34;&gt;
&lt;h3 id=&#34;headline-4&#34;&gt;
Optimised solution
&lt;/h3&gt;
&lt;div id=&#34;outline-text-headline-4&#34; class=&#34;outline-text-3&#34;&gt;
&lt;p&gt;The optimised version of the code stores the length of all sequences in an array. When the code generates a sequence and lands on a number already analysed, then it adds that previous number to the current one and moves on to the next step. This approach requires more memory but saves a lot of computation time. A minor tweak to the code optimises the rule for uneven numbers. Tripling an uneven number and adding one will always result in an even number so we can skip one step. This solution is more than twice as fast as the first version.&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Optimised method&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  collatz.length &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;vector&lt;/span&gt;(length &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1E6&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  collatz.length[1] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;for&lt;/span&gt; (n &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;in&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1E6&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      x &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; n
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      count &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;while&lt;/span&gt; (x &lt;span style=&#34;color:#333&#34;&gt;!=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;&amp;amp;&lt;/span&gt; x &lt;span style=&#34;color:#333&#34;&gt;&amp;gt;=&lt;/span&gt; n) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (x &lt;span style=&#34;color:#333&#34;&gt;%%&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;              x &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; x &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;              count &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; count &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;else&lt;/span&gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;              x &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; (&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;3&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; x &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;              count &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; count &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      count &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; count &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; collatz.length[x]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      collatz.length[n] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; count
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  answer &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;which.max&lt;/span&gt;(collatz.length)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;print&lt;/span&gt;(answer)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-5&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-5&#34;&gt;
Visualising Collatz Sequences
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-5&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The Collatz sequence is an example of a simple mathematical rule that can create an unpredictable pattern. The number of steps required to reach one is listed in &lt;a href=&#34;https://oeis.org/A006577&#34;&gt;sequence A006577&lt;/a&gt; of the Online Encyclopedia of Integer Sequences. The image below visualises the number of steps for the first 1000 positive numbers. The scatterplot shows some unusual patterns. Does this visualisation show that the Collatz Sequence does have a pattern after all?&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/project-euler/problem-014a.png&#34; alt=&#34;Number of steps to terminate a Collatz sequence.&#34; title=&#34;Number of steps to terminate a Collatz sequence.&#34;/&gt;
&lt;figcaption&gt;
Number of steps to terminate a Collatz sequence.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Visualise&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(ggplot2)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  collatz &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;data.frame&lt;/span&gt;(n &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10000&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                        steps &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; collatz.length[1&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10000&lt;/span&gt;])
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ggplot&lt;/span&gt;(collatz, &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;aes&lt;/span&gt;(n, steps)) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;geom_point&lt;/span&gt;(size &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;.5&lt;/span&gt;, col &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;darkblue&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;theme_minimal&lt;/span&gt;(base_size &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;labs&lt;/span&gt;(title &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Collatz Sequence&amp;#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;         subtitle &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Number of steps to reach 1&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ggsave&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;../../../../static/images/project-euler/problem-014a.png&amp;#34;&lt;/span&gt;, width &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;6&lt;/span&gt;, height &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;4&lt;/span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-6&#34; class=&#34;outline-3&#34;&gt;
&lt;h3 id=&#34;headline-6&#34;&gt;
Collatz Chains
&lt;/h3&gt;
&lt;div id=&#34;outline-text-headline-6&#34; class=&#34;outline-text-3&#34;&gt;
&lt;p&gt;The Collatz sequences can also be visualised using networks. Each step between two numbers is an edge, and the numbers are the vertices. For example, the network for the Collatz sequence for number 10 is 5–16, 16–8, 8–4, 4–2, 2–1. When generating subsequent sequences, the network will start to overlap, and a tree of sequences appears. The tree below combines the Collatz sequences for the numbers 2 to 26. Number 27 has a very long sequence, making the tree much harder to read. The code to create this network uses the &lt;em&gt;igraph&lt;/em&gt; package. The edge list is a data frame where each row indicates a connected set of numbers.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/project-euler/problem-014b.png&#34; alt=&#34;Collatz network 1-26.&#34; title=&#34;Collatz netork 1-26&#34;/&gt;
&lt;figcaption&gt;
Collatz network \((n \leq 26)\).
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#888&#34;&gt;## Collatz Chains&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(igraph)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;edgelist &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;data.frame&lt;/span&gt;(a &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;, b &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;for&lt;/span&gt; (n &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;in&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;3&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;26&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;   chain &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;as.character&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(n, &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;collatz.chain&lt;/span&gt;(n)))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;   chain &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;data.frame&lt;/span&gt;(a &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; chain[&lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;length&lt;/span&gt;(chain)], b &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; chain[&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;-1&lt;/span&gt;])
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;   edgelist &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;rbind&lt;/span&gt;(edgelist, chain)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;g &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;graph.edgelist&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;as.matrix&lt;/span&gt;(edgelist))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;g &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;simplify&lt;/span&gt;(g)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;png&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;../../../../static/images/project-euler/problem-014b.png&amp;#34;&lt;/span&gt;, 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    width &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1024&lt;/span&gt;, height &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;768&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;par&lt;/span&gt;(mar &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;rep&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;,&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;4&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;V&lt;/span&gt;(g)&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;color &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;degree&lt;/span&gt;(g, mode &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;out&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;plot&lt;/span&gt;(g,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;     layout &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; layout.auto,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;     vertex.color &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;V&lt;/span&gt;(g)&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;color,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;     vertex.frame.color &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;NA&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;     frame.color &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;white&amp;#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;     vertex.size &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;6&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;     vertex.label.cex &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1.5&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;     vertex.label.color &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;black&amp;#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;     edge.arrow.size &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0.2&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;     edge.color &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;grey&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;     )
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;dev.off&lt;/span&gt;()&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
&lt;a href = &#34;https://www.r-bloggers.com/&#34; target=&#34;_blank&#34; title=&#34;Proudly associated with R-Bloggers&#34;&gt;
  &lt;button class=&#34;button is-link is-medium&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-r-project&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span&gt;As seen on R Bloggers&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

      </description>
    </item>
    
    
    
    <item>
      <title>Project Euler 13: Adding Very Large Integers</title>
      <link>https://lucidmanager.org/data-science/project-euler-13/</link>
      <pubDate>Thu, 23 Feb 2017 00:00:00 +0000</pubDate>
      <author>Peter Prevos</author>
      <guid>https://lucidmanager.org/data-science/project-euler-13/</guid>
      <description>
	
&lt;p&gt;
Project Euler 13 asks to add one hundred numbers with fifty digits. This seems like a simple problem where it not that most computers are not designed to deal with numbers with a lot of integers. For example: 2&lt;sup&gt;64&lt;/sup&gt; = 18446744073709551616. When asking R to compute this value, we get 1.844674e+19, losing most of the digits and limiting the accuracy of the results. Computers solve this problem using &lt;a href=&#34;https://en.wikipedia.org/wiki/Arbitrary-precision_arithmetic&#34;&gt;Arbitrary-Precision Arithmetic&lt;/a&gt;. Many software libraries can process long integers without losing accuracy. Euler Problem 13 requires this type of approach.&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-1&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-1&#34;&gt;
Project Euler 13 Definition
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-1&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;Work out the first ten digits of the sum of the following one-hundred &lt;a href=&#34;https://projecteuler.net/problem=13&#34;&gt;50-digit numbers&lt;/a&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-2&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-2&#34;&gt;
Solution
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-2&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The easy way to solve this problem is to use the &lt;a href=&#34;https://cran.r-project.org/web/packages/gmp/index.html&#34;&gt;GMP package&lt;/a&gt; for working with huge integers. This package uses a special number types such as Big Rational and Big Integer. The number of digits in these number types is only limited by the size of your computer&amp;#39;s memory.&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://github.com/pprevos/ProjectEuler/&#34; target=&#34;_blank&#34;
   title=&#34;Download ProjectEuler from GitHub&#34;
   alt=&#34;Download ProjectEuler from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;ProjectEuler&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Project Euler 13: Adding Very Large Numbers&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## GMP Library&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(gmp)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  numbers &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;readLines&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;data/p013_numbers.txt&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  gmp_method &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;function&lt;/span&gt;() {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      digits &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sum&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;as.bigz&lt;/span&gt;(numbers))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      answer &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;substr&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;as.character&lt;/span&gt;(digits),&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;,&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;return&lt;/span&gt;(answer)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;gmp_method&lt;/span&gt;()&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-3&#34; class=&#34;outline-3&#34;&gt;
&lt;h3 id=&#34;headline-3&#34;&gt;
Using Base-R
&lt;/h3&gt;
&lt;div id=&#34;outline-text-headline-3&#34; class=&#34;outline-text-3&#34;&gt;
&lt;p&gt;To find the solution to this problem using only base R, I wrote a function to add numbers using strings instead of integers. The function adds leading zeros to the smallest number to make them both the same length. The function then proceeds to add numbers in the same way we were taught in primary school.&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Base-R Solution&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  big.add &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;function&lt;/span&gt;(a, b) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#888&#34;&gt;## Add leading zeros to smallest number&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;nchar&lt;/span&gt;(a)&lt;span style=&#34;color:#333&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;nchar&lt;/span&gt;(b)) 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      a &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;paste0&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;paste&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;rep&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;, &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;nchar&lt;/span&gt;(b) &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;nchar&lt;/span&gt;(a)), collapse &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;&amp;#34;&lt;/span&gt;), a)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;nchar&lt;/span&gt;(a)&lt;span style=&#34;color:#333&#34;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;nchar&lt;/span&gt;(b)) 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      b &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;paste0&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;paste&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;rep&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;, &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;nchar&lt;/span&gt;(a) &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;nchar&lt;/span&gt;(b)), collapse &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;&amp;#34;&lt;/span&gt;), b)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    solution &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;vector&lt;/span&gt;()
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    remainder &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;for&lt;/span&gt; (i &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;in&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;nchar&lt;/span&gt;(b)&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      p &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;as.numeric&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;substr&lt;/span&gt;(a, i, i))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      q &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;as.numeric&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;substr&lt;/span&gt;(b, i, i))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      r &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; p &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; q &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; remainder
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (r &lt;span style=&#34;color:#333&#34;&gt;&amp;gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;&amp;amp;&lt;/span&gt; i &lt;span style=&#34;color:#333&#34;&gt;!=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        solution &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(solution, r &lt;span style=&#34;color:#333&#34;&gt;%%&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        remainder &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; (r &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; (r &lt;span style=&#34;color:#333&#34;&gt;%%&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt;)) &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      } &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;else&lt;/span&gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        solution &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(solution, r)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        remainder &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;return&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;paste&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;rev&lt;/span&gt;(solution), collapse &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;&amp;#34;&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;With this function, the problem is easy to solve. The second part of the code runs this function over the one hundred numbers provided on the Euler Problem page and calculates the answer.&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    base_method &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;function&lt;/span&gt;() {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        answer &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;for&lt;/span&gt; (i &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;in&lt;/span&gt; numbers)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            answer &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;big.add&lt;/span&gt;(answer, i)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        answer &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;substr&lt;/span&gt;(answer, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;return&lt;/span&gt;(answer)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;base_method&lt;/span&gt;()&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
This solution for managing very large numbers is reused in &lt;a href=&#34;https://lucidmanager.org/data-science/project-euler-25/&#34;&gt;Project Euler 25&lt;/a&gt; which asks for a Fibonacci number with 1000 decimals.
&lt;a href = &#34;https://www.r-bloggers.com/&#34; target=&#34;_blank&#34; title=&#34;Proudly associated with R-Bloggers&#34;&gt;
  &lt;button class=&#34;button is-link is-medium&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-r-project&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span&gt;As seen on R Bloggers&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

      </description>
    </item>
    
    
    
    <item>
      <title>Project Euler 12: Highly Divisible Triangular Number</title>
      <link>https://lucidmanager.org/data-science/project-euler-12/</link>
      <pubDate>Thu, 16 Feb 2017 00:00:00 +0000</pubDate>
      <author>Peter Prevos</author>
      <guid>https://lucidmanager.org/data-science/project-euler-12/</guid>
      <description>
	
&lt;p&gt;
Project Euler 12 takes us to the realm of triangular numbers and proper divisors. The image below shows a hands-on method to visualise the number of divisors of ten. Cuisenaire rods are learning aids to explore mathematics. Each of the coloured rods has a different integer length, which allows students to explore basic arithmetic. In this article, I discuss a method to generate triangular numbers using the R language.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://upload.wikimedia.org/wikipedia/commons/6/67/Cuisenaire-Rods.jpg&#34; alt=&#34;Cuisenaire-rods (Source: Wikimedia user Celcom / CC BY).&#34; title=&#34;Cuisenaire-rods (Source: Wikimedia user Celcom / CC BY).&#34; width=&#34;400&#34;/&gt;
&lt;figcaption&gt;
Cuisenaire-rods (Source: Wikimedia user Celcom / CC BY).
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;div id=&#34;outline-container-headline-1&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-1&#34;&gt;
Project Euler 12 Definition
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-1&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The sequence of triangle numbers is generated by adding the natural numbers. So the 7th triangle number would be \(1 + 2 + 3 + 4 + 5 + 6 + 7 = 28\). The first ten terms would be: \(1, 3, 6, 10, 15, 21, 28, 36, 45, 55, \ldots\)&lt;/p&gt;
&lt;p&gt;
Let us list the factors of the first seven triangle numbers:&lt;/p&gt;
&lt;div class=&#34;src src-text&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-text&#34; data-lang=&#34;text&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt; 1: 1
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt; 3: 1, 3
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt; 6: 1, 2, 3,  6
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;10: 1, 2, 5, 10
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;15: 1, 3, 5, 15
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;21: 1, 3, 7, 21
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;28: 1, 2, 4,  7, 14, 28&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
We can see that 28 is the first triangle number to have over five divisors.&lt;/p&gt;
&lt;p&gt;
What is the value of the first triangle number to have over &lt;a href=&#34;https://projecteuler.net/problem=12&#34;&gt;five hundred divisors&lt;/a&gt;?&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-2&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-2&#34;&gt;
Solution
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-2&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;In the video below, Vishal Kataria explains a simple method to determine the number of divisors using prime factorization. The prime factorization of \(n\) is given by:&lt;/p&gt;
&lt;p&gt;
$$n = p^{\alpha_1}_1 \times p^{\alpha_2}_2 \times \ldots \times p^{\alpha_k}_k$$&lt;/p&gt;
&lt;p&gt;
Where \(p\) is a prime factor, and \(\alpha\) is the number of times the factor is repeated.&lt;/p&gt;
&lt;p&gt;
The number of proper divisors is for \(n\) is:&lt;/p&gt;
&lt;p&gt;
$$d = (\alpha_1 + 1) (\alpha_2 + 1) \times \ldots \times (\alpha_k + 1)$$&lt;/p&gt;
&lt;p&gt;
The code reuses the prime factorisation function developed for &lt;a href=&#34;https://lucidmanager.org/data-science/project-euler-3/&#34;&gt;Euler Problem 3&lt;/a&gt;. This function results in a vector of all prime factors, e.g. the prime factors of 28 are 2, 2 and 7.&lt;/p&gt;
&lt;p&gt;
The code to solve this problem determines the values for alpha using the run-length &lt;code class=&#34;verbatim&#34;&gt;rle()&lt;/code&gt; function. This function counts the number of times each element in a sequence is repeated. The outcome of this function is a vector of the values and the number of times each is repeated. The prime factors of 28 are 2, 2 and 7, and their run lengths are 2 and 1. The number of divisors can now be determined.&lt;/p&gt;
&lt;p&gt;
$$ 28 = 2^2 \times 7^1$$&lt;/p&gt;
&lt;p&gt;
$$ d = (2+1)(1+1) = 6$$&lt;/p&gt;
&lt;p&gt;
The six divisors of 28 are 1, 2, 4, 7, 14 and 28.&lt;/p&gt;
&lt;p&gt;
The code to solve Project Euler 12 is shown below. The loop continues until it finds a triangular number with 500 divisors. The first two lines increment the index and create the next triangular number. The third line in the loop determines the number of times each factor is repeated (the run lengths). The last line calculates the number of divisors using the above formula.&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://github.com/pprevos/ProjectEuler/&#34; target=&#34;_blank&#34;
   title=&#34;Download ProjectEuler from GitHub&#34;
   alt=&#34;Download ProjectEuler from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;ProjectEuler&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Project Euler 12: Highly Divisible Triangular Number&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  i &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  divisors &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;while&lt;/span&gt; (divisors &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;500&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      i &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; i &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      triangle &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; (i &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; (i &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;)) &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      pf &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;prime.factors&lt;/span&gt;(triangle)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      alpha &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;rle&lt;/span&gt;(pf)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      divisors &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;prod&lt;/span&gt;(alpha&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;lengths &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  answer &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; triangle
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;print&lt;/span&gt;(answer)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;figure&gt;
&lt;p&gt;&lt;div style=&#34;position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden;&#34;&gt;
      &lt;iframe allow=&#34;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share; fullscreen&#34; loading=&#34;eager&#34; referrerpolicy=&#34;strict-origin-when-cross-origin&#34; src=&#34;https://www.youtube.com/embed/_QIx_zmh9nU?autoplay=0&amp;amp;controls=1&amp;amp;end=0&amp;amp;loop=0&amp;amp;mute=0&amp;amp;start=0&#34; style=&#34;position: absolute; top: 0; left: 0; width: 100%; height: 100%; border:0;&#34; title=&#34;YouTube video&#34;&gt;&lt;/iframe&gt;
    &lt;/div&gt;

&lt;a href = &#34;https://www.r-bloggers.com/&#34; target=&#34;_blank&#34; title=&#34;Proudly associated with R-Bloggers&#34;&gt;
  &lt;button class=&#34;button is-link is-medium&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-r-project&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span&gt;As seen on R Bloggers&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;
&lt;/p&gt;
&lt;figcaption&gt;
Vishal Kataria. Number Theory - Number of Divisors.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;/div&gt;
&lt;/div&gt;

      </description>
    </item>
    
    
    
    <item>
      <title>Percentile Calculations in Water Quality Regulations</title>
      <link>https://lucidmanager.org/data-science/percentile-calculations/</link>
      <pubDate>Fri, 10 Feb 2017 00:00:00 +0000</pubDate>
      <author>Peter Prevos</author>
      <guid>https://lucidmanager.org/data-science/percentile-calculations/</guid>
      <description>
	
&lt;p&gt;
Percentile calculations can be more tricky than at first meets the eye. A &lt;a href=&#34;https://en.wikipedia.org/wiki/Percentile&#34;&gt;percentile&lt;/a&gt; indicates the value below which a percentage of observations fall. Some percentiles have special names, such as the &lt;a href=&#34;https://en.wikipedia.org/wiki/Quartile&#34;&gt;quartile&lt;/a&gt; or the &lt;a href=&#34;https://en.wikipedia.org/wiki/Decile&#34;&gt;decile&lt;/a&gt;, both of which are &lt;a href=&#34;https://en.wikipedia.org/wiki/Quantile&#34;&gt;quantiles&lt;/a&gt;. This deceivingly simple definition hides the various ways to determine this number. Unfortunately, there is &lt;a href=&#34;https://www.amherst.edu/media/view/129116/original/Sample+Quantiles.pdf&#34;&gt;no standard definition for percentiles&lt;/a&gt;, so which method do you use? &lt;/p&gt;
&lt;p&gt;
The &lt;a href=&#34;https://stat.ethz.ch/R-manual/R-devel/library/stats/html/quantile.html&#34;&gt;quantile&lt;/a&gt; function in R generates sample percentiles corresponding to the given probabilities. By default, the quantile function provides the quartiles and the minimum and maximum values. The code snippet below creates semi-random data, plots the histogram and visualises the third quartile.&lt;/p&gt;
&lt;p&gt;
The code for this page is available on GitHub in &lt;code class=&#34;verbatim&#34;&gt;case-studies/percentiles.R&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://github.com/pprevos/r4h2o/&#34; target=&#34;_blank&#34;
   title=&#34;Download r4h2o from GitHub&#34;
   alt=&#34;Download r4h2o from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;r4h2o&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Percentile calculations&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Example&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;set.seed&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1969&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  test.data &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;rnorm&lt;/span&gt;(n &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10000&lt;/span&gt;, mean &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;100&lt;/span&gt;, sd &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;15&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(ggplot2)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ggplot&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;as.data.frame&lt;/span&gt;(test.data), &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;aes&lt;/span&gt;(test.data)) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;geom_histogram&lt;/span&gt;(binwidth &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;, &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;aes&lt;/span&gt;(y &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; ..density..), fill &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;dodgerblue&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;geom_line&lt;/span&gt;(stat &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;function&amp;#34;&lt;/span&gt;, fun &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; dnorm, args &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;list&lt;/span&gt;(mean &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;100&lt;/span&gt;, sd &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;15&lt;/span&gt;), colour &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;red&amp;#34;&lt;/span&gt;, size &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;geom_area&lt;/span&gt;(stat &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;function&amp;#34;&lt;/span&gt;, fun &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; dnorm, args &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;list&lt;/span&gt;(mean &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;100&lt;/span&gt;, sd &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;15&lt;/span&gt;),
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;              colour &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;red&amp;#34;&lt;/span&gt;, fill &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;red&amp;#34;&lt;/span&gt;, alpha &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0.5&lt;/span&gt;, xlim &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;quantile&lt;/span&gt;(test.data, &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0.5&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0.75&lt;/span&gt;))) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;theme_bw&lt;/span&gt;(base_size &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;8&lt;/span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/hydroinformatics/percentile-example.png&#34; alt=&#34;Percentile Calculations example&#34; title=&#34;Percentile Calculations example&#34; width=&#34;50%&#34;/&gt;
&lt;figcaption&gt;
Percentile Calculations example.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
The quantile function with default settings and the 95&lt;sup&gt;th&lt;/sup&gt; percentile give the following results:&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;quantile&lt;/span&gt;(test.data)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;pre class=&#34;example&#34;&gt;
       0%       25%       50%       75%      100% 
 39.91964  89.68041 100.16437 110.01910 153.50195
&lt;/pre&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;quantile&lt;/span&gt;(test.data, probs &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0.95&lt;/span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;pre class=&#34;example&#34;&gt;
     95% 
124.7775
&lt;/pre&gt;
&lt;div id=&#34;outline-container-headline-1&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-1&#34;&gt;
Methods of percentile calculation
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-1&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The quantile function in R provides nine different ways to calculate percentiles. Each of these options uses another method to interpolate between observed values. I will not discuss the mathematical nuances between these methods. &lt;a href=&#34;https://www.amherst.edu/media/view/129116/original/Sample+Quantiles.pdf&#34;&gt;Hyndman and Fan&lt;/a&gt; (1996) provides a detailed discussion of these methods.&lt;/p&gt;
&lt;p&gt;
The differences between the nine available methods only matter in highly skewed distributions, such as water quality data. All methods provide the same outcome for the normal distribution simulated above, as illustrated by the following code.&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sapply&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;9&lt;/span&gt;, &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;function&lt;/span&gt;(m) &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;quantile&lt;/span&gt;(test.data, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0.95&lt;/span&gt;, type &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; m))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;pre class=&#34;example&#34;&gt;
     95%      95%      95%      95%      95%      95%      95% 
124.7775 124.7775 124.7775 124.7775 124.7775 124.7775 124.7775 
     95%      95% 
124.7775 124.7775
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-2&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-2&#34;&gt;
Percentile calculations in water quality
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-2&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The &lt;a href=&#34;https://nhmrc.gov.au/about-us/publications/australian-drinking-water-guidelines&#34;&gt;Australian Drinking Water Quality Guidelines&lt;/a&gt; (November 2016) specify that: &amp;#34;based on aesthetic considerations, the turbidity should not exceed 5 NTU at the consumer&amp;#39;s tap&amp;#34;. The Victorian Safe Drinking Water Regulations(2015) relax this requirement and require that:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;The 95&lt;sup&gt;th&lt;/sup&gt; percentile of results for samples in any 12 months must be less than or equal to 5.0 NTU.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;
The Victorian water quality regulator &lt;a href=&#34;https://www2.health.vic.gov.au/Api/downloadmedia/%7BA1F6D255-D5C7-4B7E-AAE5-8B7451EDE81A%7D&#34;&gt;also specifies&lt;/a&gt; that the percentile should be calculated with the &lt;em&gt;Weibull Method&lt;/em&gt;. This requirement raises two questions: What is the Weibull method? How do you implement this requirement in R? &lt;/p&gt;
&lt;p&gt;
The term Weibull Method is confusing as this is not a name generally used by statisticians. In Hyndman &amp;amp; Fan (1996), this method has the less poetic name $\hat{Q}_8(p)$. The Victorian water quality regulator references &lt;a href=&#34;http://amzn.to/2k8shr8&#34;&gt;McBride (2005)&lt;/a&gt;, who calls it the Weibull method. &lt;a href=&#34;https://en.wikipedia.org/wiki/Waloddi_Weibull&#34;&gt;Waloddi Weibull&lt;/a&gt;, a Swedish engineer famous for his distribution, was one of the first to describe this method.&lt;/p&gt;
&lt;p&gt;
This theoretical interlude aside, how can we practically apply this to water quality data?&lt;/p&gt;
&lt;p&gt;
If you are interested in how the Weibull method works, the &lt;code class=&#34;verbatim&#34;&gt;weibull.quantile()&lt;/code&gt; function below calculates a quantile &lt;em&gt;p&lt;/em&gt; for a vector &lt;em&gt;x&lt;/em&gt; using this method. This function gives the same result as &lt;code class=&#34;verbatim&#34;&gt;quantile(x, p, type = 6)&lt;/code&gt;.&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Weibull method&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  weibull.quantile &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;function&lt;/span&gt;(x, p) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#888&#34;&gt;# Order Samples from large to small&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    x &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; x&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;[order&lt;/span&gt;(x, decreasing &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;FALSE&lt;/span&gt;)]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#888&#34;&gt;# Determine the ranking of percentile according to Weibull (1939)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    r &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; p &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; (&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;length&lt;/span&gt;(x) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#888&#34;&gt;# Linear interpolation&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    rfrac &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; (r &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;floor&lt;/span&gt;(r))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;return&lt;/span&gt;((&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; rfrac) &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; x&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;[floor&lt;/span&gt;(r)] &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; rfrac &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; x&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;[floor&lt;/span&gt;(r) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;])
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;weibull.quantile&lt;/span&gt;(test.data, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0.95&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;quantile&lt;/span&gt;(test.data, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0.95&lt;/span&gt;, type &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;6&lt;/span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;pre class=&#34;example&#34;&gt;
124.777545386916
&lt;/pre&gt;
&lt;div id=&#34;outline-container-headline-3&#34; class=&#34;outline-3&#34;&gt;
&lt;h3 id=&#34;headline-3&#34;&gt;
Turbidity Data Example
&lt;/h3&gt;
&lt;div id=&#34;outline-text-headline-3&#34; class=&#34;outline-text-3&#34;&gt;
&lt;p&gt;Turbidity data is not normally distributed as it is always greater than zero. In this example, we use simulated data from a fictional water system called Gormsey. This data is also used in book &lt;a href=&#34;https://lucidmanager.org/tags/r4h2o/&#34;&gt;Data Science for Water Utilities.&lt;/a&gt;&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Visualise Data&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(tidyverse)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  gormsey &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;read_csv&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;~/Documents/projects/r4h2o/data/water_quality.csv&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  turbidity &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;filter&lt;/span&gt;(gormsey, Measure &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Turbidity&amp;#34;&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;&amp;amp;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                               Suburb &lt;span style=&#34;color:#333&#34;&gt;%in%&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Blancathey&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Tarnstead&amp;#34;&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ggplot&lt;/span&gt;(turbidity, &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;aes&lt;/span&gt;(Result)) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;geom_density&lt;/span&gt;(fill &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;dodgerblue&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;aes&lt;/span&gt;(y &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; ..density..)) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;scale_x_continuous&lt;/span&gt;(trans &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;log10&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;facet_wrap&lt;/span&gt;(&lt;span style=&#34;color:#333&#34;&gt;~&lt;/span&gt;Suburb) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;theme_bw&lt;/span&gt;() &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;labs&lt;/span&gt;(title &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Turbidity Distribution&amp;#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;         x&lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;log Results&amp;#34;&lt;/span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/hydroinformatics/gormsey-turbidity.png&#34; alt=&#34;Turbidity at customer tap&#34; title=&#34;Turbidity at customer tap&#34; width=&#34;70%&#34;/&gt;
&lt;figcaption&gt;
Turbidity at customer tap in two towns in the fictional country of Gormsey.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
When we calculate the percentiles for all nine methods available in the base-R function, we see that the Weibull method generally provides the most conservative result for heavily skewed data.&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;# Calculate all percentile methods&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;tapply&lt;/span&gt;(turbidity&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;Result, turbidity&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;Suburb,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;         &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;function&lt;/span&gt;(x) &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sapply&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;9&lt;/span&gt;, &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;function&lt;/span&gt;(m) &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;quantile&lt;/span&gt;(x, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0.95&lt;/span&gt;, type &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; m)))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;pre class=&#34;example&#34;&gt;
$Blancathey
   95%    95%    95%    95%    95%    95%    95%    95%    95% 
6.0200 6.0200 6.0200 5.8820 6.2720 6.6500 5.9165 6.3980 6.3665 

$Tarnstead
     95%      95%      95%      95%      95%      95%      95% 
4.480000 4.480000 4.480000 4.432500 4.607500 4.837000 4.442000 
     95%      95% 
4.684000 4.664875
&lt;/pre&gt;
&lt;p&gt;
&lt;a href=&#34;https://lucidmanager.org/data-science/descriptive-statistics-in-water-quality/&#34;&gt;Chapter 4&lt;/a&gt; of my book &lt;em&gt;Data Science for Water Utilities&lt;/em&gt; discusses calculating descriptive statistics with R&lt;/p&gt;
&lt;p&gt;

  &lt;div class=&#34;box&#34;&gt;
    &lt;div class=&#34;media&#34;&gt;
      &lt;figure class=&#34;media-left&#34;&gt;
        &lt;p class=&#34;image is-128x128&#34;&gt;
          &lt;img src=&#34;https://lucidmanager.org/images/books/2023_ds4wu.jpg&#34; alt = &#34;Data Science for Water Utilities&#34; title = &#34;Data Science for Water Utilities&#34;&gt;
        &lt;/p&gt;
      &lt;/figure&gt;
      &lt;div class=&#34;media-content&#34;&gt;
        &lt;div class=&#34;content&#34;&gt;
          &lt;p class=&#34;is-size-5 has-text-weight-bold&#34;&gt;Data Science for Water Utilities&lt;/p&gt;
          &lt;p class=&#34;mb-2&#34;&gt;Data Science for Water Utilities published by CRC Press is an applied, practical guide that shows water professionals how to use data science to solve urban water management problems using the R language for statistical computing.&lt;/p&gt;
          &lt;div class=&#34;buttons&#34;&gt;
            &lt;a class=&#34;button is-info&#34; href=&#34;https://routledge.com/9781032354545&#34; target=&#34;_blank&#34;&gt;
              &lt;span class=&#34;icon&#34;&gt;
                &lt;i class=&#34;fas fa-shopping-cart&#34;&gt;&lt;/i&gt;
              &lt;/span&gt;
              &lt;span&gt;Routledge&lt;/span&gt;
            &lt;/a&gt;
            &lt;a class=&#34;button is-info&#34; href=&#34;https://www.amazon.com/Data-Science-Water-Utilities-Chapman/dp/1032354550&#34; target=&#34;_blank&#34;&gt;
              &lt;span class=&#34;icon&#34;&gt;
                &lt;i class=&#34;fas fa-shopping-cart&#34;&gt;&lt;/i&gt;
              &lt;/span&gt;
              &lt;span&gt;Amazon&lt;/span&gt;
            &lt;/a&gt;
          &lt;/div&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://www.r-bloggers.com/&#34; target=&#34;_blank&#34; title=&#34;Proudly associated with R-Bloggers&#34;&gt;
  &lt;button class=&#34;button is-link is-medium&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-r-project&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span&gt;As seen on R Bloggers&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

      </description>
    </item>
    
    
    
    <item>
      <title>Project Euler 11: Largest product in a grid</title>
      <link>https://lucidmanager.org/data-science/project-euler-11/</link>
      <pubDate>Thu, 09 Feb 2017 00:00:00 +0000</pubDate>
      <author>Peter Prevos</author>
      <guid>https://lucidmanager.org/data-science/project-euler-11/</guid>
      <description>
	
&lt;p&gt;
Project Euler 11 is a relatively straightforward application of vector arithmetics in R. Just like &lt;a href=&#34;https://lucidmanager.org/data-science/project-euler-8/&#34;&gt;problem 8&lt;/a&gt;, it asks for the largest product.&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-1&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-1&#34;&gt;
Project Euler 11 Definition
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-1&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;In the 20×20 grid below, four numbers along a diagonal line have been marked in red. The product of these numbers is 26 × 63 × 78 × 14 = 1788696.&lt;/p&gt;
&lt;p&gt;
What is the &lt;a href=&#34;https://projecteuler.net/problem=11&#34;&gt;greatest product&lt;/a&gt; of four adjacent numbers in the same direction (up, down, left, right, or diagonally) in the 20×20 grid?&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/project-euler/problem-011.png&#34; alt=&#34;Project Euler problem 11&#34; title=&#34;Project Euler problem 11&#34;/&gt;
&lt;figcaption&gt;
Project Euler 11.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-2&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-2&#34;&gt;
Solution
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-2&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The solution applies straightforward vector arithmetic to multiply the verticals, horizontals and diagonals. The product of all verticals is an array of the product of rows 1 to 4, rows 2 to 5 and so on. The code uses a similar logic for the horizontals and the diagonals. &lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://github.com/pprevos/ProjectEuler/&#34; target=&#34;_blank&#34;
   title=&#34;Download ProjectEuler from GitHub&#34;
   alt=&#34;Download ProjectEuler from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;ProjectEuler&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Project Euler 11: Largest product in a grid&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  square &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;as.matrix&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;read.delim&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;data/p011_matrix.txt&amp;#34;&lt;/span&gt;, header &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;FALSE&lt;/span&gt;, sep &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34; &amp;#34;&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  prod.vert &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; square[1&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;17&lt;/span&gt;, ] &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; square[2&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;18&lt;/span&gt;, ] &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; square[3&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;19&lt;/span&gt;, ] &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; square[4&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;20&lt;/span&gt;, ]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  prod.hori &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; square[ ,&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;17&lt;/span&gt;] &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; square[ ,&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;18&lt;/span&gt;] &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; square[ ,&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;3&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;19&lt;/span&gt;] &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; square[ ,&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;4&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;20&lt;/span&gt;]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  prod.dia1 &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; square[1&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;17&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;17&lt;/span&gt;] &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; square[2&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;18&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;18&lt;/span&gt;] &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; square[3&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;19&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;3&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;19&lt;/span&gt;] &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; square[4&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;20&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;4&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;20&lt;/span&gt;]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  prod.dia2 &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; square[4&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;20&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;17&lt;/span&gt;] &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; square[3&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;19&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;18&lt;/span&gt;] &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; square[2&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;18&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;3&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;19&lt;/span&gt;] &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; square[1&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;17&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;4&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;20&lt;/span&gt;]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  answer &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;max&lt;/span&gt;(prod.vert, prod.hori, prod.dia1, prod.dia2)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;print&lt;/span&gt;(answer)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;a href = &#34;https://www.r-bloggers.com/&#34; target=&#34;_blank&#34; title=&#34;Proudly associated with R-Bloggers&#34;&gt;
  &lt;button class=&#34;button is-link is-medium&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-r-project&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span&gt;As seen on R Bloggers&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/div&gt;
&lt;/div&gt;

      </description>
    </item>
    
    
    
    <item>
      <title>Project Euler 10: Summation of Primes</title>
      <link>https://lucidmanager.org/data-science/project-euler-10/</link>
      <pubDate>Thu, 02 Feb 2017 00:00:00 +0000</pubDate>
      <author>Peter Prevos</author>
      <guid>https://lucidmanager.org/data-science/project-euler-10/</guid>
      <description>
	
&lt;p&gt;
Project Euler 10 asks for the summation of primes. Computationally this is a simple problem because we can re-use the prime sieve developed for &lt;a href=&#34;https://lucidmanager.org/data-science/project-euler-3/&#34;&gt;Project Euler 3&lt;/a&gt;. Mathematicians consider primes the basic building blocks of number theory. No matter how hard we look, however, they do not seem to obey any logical sequence. There is thus no algorithm to create prime numbers, all we can do is mine them from the infinite field of integers. &lt;/p&gt;
&lt;div id=&#34;outline-container-headline-1&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-1&#34;&gt;
Project Euler 10 Definition
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-1&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. Find the &lt;a href=&#34;https://projecteuler.net/problem=10&#34;&gt;sum of all the primes&lt;/a&gt; below two million.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-2&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-2&#34;&gt;
Solution
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-2&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The sieve of Eratosthenes function from Project Euler 3 generates the prime numbers between two and two million, which can then easily be summed.&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://github.com/pprevos/ProjectEuler/&#34; target=&#34;_blank&#34;
   title=&#34;Download ProjectEuler from GitHub&#34;
   alt=&#34;Download ProjectEuler from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;ProjectEuler&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Project Euler 10: Summation of Primes&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;source&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;problem003.R&amp;#34;&lt;/span&gt;, echo &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;FALSE&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  primes &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;esieve&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2e6&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  answer &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sum&lt;/span&gt;(primes)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;print&lt;/span&gt;(answer)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-3&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-3&#34;&gt;
Goldbach&amp;#39;s Conjecture
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-3&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The summing of primes reveals an interesting problem in mathematics. Goldbach&amp;#39;s conjecture is one of the oldest and best-known unsolved problems in number theory. In 1742 Goldbach wrote a letter to Euler in which he states that:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Every even integer greater than 2 can be expressed as the sum of two primes.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;
A Goldbach partition is the expression of a given even number as a sum of two primes, for example:&lt;/p&gt;
&lt;p&gt;
$$100 = 3 + 97 = 11 + 89 = 17 + 83 = 29 + 71 = 41 + 59 = 47 + 53$$&lt;/p&gt;
&lt;p&gt;
All even numbers greater than 12 can be written as the sum of two primes in more than two ways. &lt;a href=&#34;https://oeis.org/A045917/&#34;&gt;Sequence A045917&lt;/a&gt; in the Online Encyclopedia of Integer Sequences lists the number of ways in which \(2n\) can be written as the sum of two primes (\(n\geq1\)). This conjecture is as yet unproven and one of the most persistent problems in mathematics. &lt;/p&gt;
&lt;figure&gt;
&lt;div style=&#34;position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden;&#34;&gt;
      &lt;iframe allow=&#34;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share; fullscreen&#34; loading=&#34;eager&#34; referrerpolicy=&#34;strict-origin-when-cross-origin&#34; src=&#34;https://www.youtube.com/embed/MxiTG96QOxw?autoplay=0&amp;amp;controls=1&amp;amp;end=0&amp;amp;loop=0&amp;amp;mute=0&amp;amp;start=0&#34; style=&#34;position: absolute; top: 0; left: 0; width: 100%; height: 100%; border:0;&#34; title=&#34;YouTube video&#34;&gt;&lt;/iframe&gt;
    &lt;/div&gt;

&lt;figcaption&gt;
Goldbach Conjecture - Numberphile.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
We can write some code to verify that every integer greater than two and less than \(n\) can be expressed as the sum of two primes. This code recreates sequence A045917 and visualises the first 1000 results. There does not seem to be a pattern in the number of ways to sum two primes. However, When visualising the sequence with a scatter plot, horizontal bands of points appear.&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Goldbach&amp;#39;s Conjecture&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  goldbach &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;function&lt;/span&gt;(n) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (n&lt;span style=&#34;color:#333&#34;&gt;%%&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;!=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;) &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;return&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;This only works for even numbers&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (n &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;) &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;return&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      primes &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;rev&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;esieve&lt;/span&gt;(n)) &lt;span style=&#34;color:#888&#34;&gt;# All primes below n&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      k &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;length&lt;/span&gt;(primes)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      g &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;for&lt;/span&gt; (i &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;in&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;k) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;for&lt;/span&gt; (j &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;in&lt;/span&gt; i&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;k) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt;((primes[i] &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; primes[j]) &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; n) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            &lt;span style=&#34;color:#888&#34;&gt;# print(paste(n, &amp;#34;=&amp;#34;, primes[i], &amp;#34;+&amp;#34;, primes[j]))&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            g &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; g &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;return&lt;/span&gt;(g)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(ggplot2)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    n &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;seq&lt;/span&gt;(from &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;, by &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;, length.out &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1000&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    A002375 &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;data.frame&lt;/span&gt;(n,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                          g &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sapply&lt;/span&gt;(n, goldbach))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ggplot&lt;/span&gt;(A002375, &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;aes&lt;/span&gt;(n, g)) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;geom_point&lt;/span&gt;(col &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;red&amp;#34;&lt;/span&gt;, size &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;.5&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;theme_minimal&lt;/span&gt;(base_size &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;labs&lt;/span&gt;(title &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Goldbach&amp;#39;s Expressions&amp;#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;           subtitle &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Number of ways to write an even number as the sum of two primes&amp;#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;           x &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Even number&amp;#34;&lt;/span&gt;, y &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Number of prime sums&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ggsave&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;images/problem-010.png&amp;#34;&lt;/span&gt;, width &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;6&lt;/span&gt;, height &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;4&lt;/span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;figure&gt;
&lt;p alt=&#34;Number of ways to write an even number n as the sum of two primes (4 ≤ n ≤ 1,000), (sequence A002375 in the OEIS)&#34; title=&#34;Number of ways to write an even number n as the sum of two primes (4 ≤ n ≤ 1,000), (sequence A002375 in the OEIS)&#34;&gt;&lt;img src=&#34;https://lucidmanager.org/images/project-euler/problem-010.png&#34; alt=&#34;/images/project-euler/problem-010.png&#34; title=&#34;/images/project-euler/problem-010.png&#34;/&gt;
&lt;a href = &#34;https://www.r-bloggers.com/&#34; target=&#34;_blank&#34; title=&#34;Proudly associated with R-Bloggers&#34;&gt;
  &lt;button class=&#34;button is-link is-medium&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-r-project&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span&gt;As seen on R Bloggers&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;
&lt;/p&gt;
&lt;figcaption&gt;
Number of ways to write an even number \(n\) as the sum of two primes \((2 \leq n \leq 1000)\), (sequence A002375 in the OEIS).
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;/div&gt;
&lt;/div&gt;

      </description>
    </item>
    
    
    
    <item>
      <title>Spike Detection in Water Treatment Data in the R Language</title>
      <link>https://lucidmanager.org/data-science/spike-detection/</link>
      <pubDate>Mon, 30 Jan 2017 00:00:00 +0000</pubDate>
      <author>Peter Prevos</author>
      <guid>https://lucidmanager.org/data-science/spike-detection/</guid>
      <description>
	
&lt;p&gt;
Spike detection in time series is essential to water treatment plant control. Spikes in SCADA data are events in the data stream of water treatment plants or similar installations. These spikes can indicate problems with the process and could result in an increased risk to public health. The WSAA &lt;em&gt;Health Based Targets Manual&lt;/em&gt; specifies a series of decision rules to assess the performance of filtration processes. For example, this rule evaluates the performance of conventional filtration:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Individual filter turbidity ≤ 0.2 NTU for 95% of the month and not &amp;gt; 0.5 NTU for ≥ 15 consecutive minutes.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;
&lt;a href=&#34;https://lucidmanager.org/data-science/percentile-calculations/&#34;&gt;Turbidity&lt;/a&gt; is a measure for the cloudiness of fluid because of large numbers of individual particles otherwise invisible to the naked eye. Turbidity is an essential parameter in water treatment because a high level of cloudiness strongly correlates with the presence of microbes. This article uses the R language to show how to implement this specific decision rule.&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-1&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-1&#34;&gt;
Simulation
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-1&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;I first create a simulated SCADA feed for turbidity to create a minimum working example. The &lt;em&gt;turbidity&lt;/em&gt; data frame contains 24 hours of data. The &lt;a href=&#34;https://stat.ethz.ch/R-manual/R-devel/library/base/html/seq.POSIXt.html&#34;&gt;seq.POSIXt()&lt;/a&gt; function creates 24 hours of timestamps at a one-minute spacing. In addition, the &lt;a href=&#34;https://www.rdocumentation.org/search?q=rnorm&#34;&gt;rnorm()&lt;/a&gt; function creates 1440 turbidity readings with an average of 0.1 NTU and a standard deviation of 0.01 NTU. The image below visualises the simulated data. The next step is to assess this data following the decision rule.&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://github.com/pprevos/r4h2o/&#34; target=&#34;_blank&#34;
   title=&#34;Download r4h2o from GitHub&#34;
   alt=&#34;Download r4h2o from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;r4h2o&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;# Simulate turbidity data&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;set.seed&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1234&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  turbidity &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;data.frame&lt;/span&gt;(DateTime &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;seq.POSIXt&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;as.POSIXct&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;2017-01-01 00:00:00&amp;#34;&lt;/span&gt;),
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                                                by &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;min&amp;#34;&lt;/span&gt;, length.out &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;24&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;60&lt;/span&gt;),
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                          Turbidity &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;rnorm&lt;/span&gt;(n &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;24&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;60&lt;/span&gt;, mean &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0.1&lt;/span&gt;, sd &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0.01&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                          )&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
The second section simulates five spikes in the data. The first line picks a random start time for the spike. The second line in the for-loop picks a duration between 10 and 30 minutes. In addition, the third line simulates the value of the spike. The mean value of the spike is determined by the &lt;a href=&#34;https://www.rdocumentation.org/packages/stats/versions/3.6.2/topics/Binomial&#34;&gt;rbinom()&lt;/a&gt; function to create either a low or a high spike. The remainder of the spike simulation inserts the new data into the turbidity data frame.&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Spike detection&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Simulate data&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;for&lt;/span&gt; (i &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;in&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;5&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    time &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sample&lt;/span&gt;(turbidity&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;DateTime, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    duration &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sample&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;30&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    value &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;rnorm&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0.5&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;rbinom&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0.5&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0.3&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0.05&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    start &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;which&lt;/span&gt;(turbidity&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;DateTime &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; time)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    turbidity&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;Turbidity[start&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;(start&lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;duration &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;)] &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;rnorm&lt;/span&gt;(duration, value, value&lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(ggplot2)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ggplot&lt;/span&gt;(turbidity, &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;aes&lt;/span&gt;(x &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; DateTime, y &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; Turbidity)) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;geom_line&lt;/span&gt;(size &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0.2&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;geom_hline&lt;/span&gt;(yintercept &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0.5&lt;/span&gt;, col &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;red&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ylim&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;,&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;max&lt;/span&gt;(turbidity&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;Turbidity)) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;theme_bw&lt;/span&gt;(base_size &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ggtitle&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Simulated SCADA data&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ggsave&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;spikes.png&amp;#34;&lt;/span&gt;, width &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;4&lt;/span&gt;, height &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;3&lt;/span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
The image below visualises the simulated data using the mighty &lt;a href=&#34;https://ggplot2.tidyverse.org/&#34;&gt;ggplot2&lt;/a&gt; package. Only four spikes are visible because two of them overlap. The next step is to assess this data following the decision rule.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/hydroinformatics/spikes.png&#34; alt=&#34;Simulated turbidity spikes&#34; title=&#34;Simulated turbidity spikes&#34; width=&#34;500&#34;/&gt;
&lt;figcaption&gt;
Simulated turbidity spikes.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-2&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-2&#34;&gt;
SCADA Spike Detection
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-2&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The following code searches for all spikes over 0.50 NTU using the &lt;a href=&#34;https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/rle&#34;&gt;runlength function&lt;/a&gt;. This function transforms a vector into a vector of values and lengths. For example, the run length of the vector &lt;code class=&#34;verbatim&#34;&gt;c(1, 1, 2, 2, 2, 3, 3, 3, 3, 5, 5, 6)&lt;/code&gt; is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code class=&#34;verbatim&#34;&gt;lengths: int [1:5] 2 3 4 2 1&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code class=&#34;verbatim&#34;&gt;values : num [1:5] 1 2 3 5 6&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The value 1 has a length of 1. The value 2 has a length of 3, and so on. The spike detection code creates the run length for turbidity levels greater than 0.5, which results in a boolean vector. The &lt;a href=&#34;https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/cumsum&#34;&gt;cumsum&lt;/a&gt; function calculates the starting point of each spike which allows us to calculate their duration.&lt;/p&gt;
&lt;p&gt;
The code results in a data frame with all spikes higher than 0.50 NTU and longer than 15 minutes. The spike at 11:29 was higher than 0.50 NTU and lasted 24 minutes. The other three spikes are either lower than 0.50 NTU. The first high spike lasted less than 15 minutes.&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;# Spike Detection Function&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  spike.detect &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;function&lt;/span&gt;(DateTime, Value, Height, Duration) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    runlength &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;rle&lt;/span&gt;(Value &lt;span style=&#34;color:#333&#34;&gt;&amp;gt;&lt;/span&gt; Height)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    spikes &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;data.frame&lt;/span&gt;(Spike &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; runlength&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;values,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                         times &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;cumsum&lt;/span&gt;(runlength&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;lengths))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    spikes&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;Times &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; DateTime[spikes&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;times]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    spikes&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;Event &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;,spikes&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;Times[&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;-1&lt;/span&gt;] &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; spikes&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;Times[&lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;nrow&lt;/span&gt;(spikes)])
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    spikes &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;subset&lt;/span&gt;(spikes, Spike &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;TRUE&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;&amp;amp;&lt;/span&gt; Event &lt;span style=&#34;color:#333&#34;&gt;&amp;gt;&lt;/span&gt; Duration)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;return&lt;/span&gt;(spikes)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;spike.detect&lt;/span&gt;(turbidity&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;DateTime, turbidity&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;Turbidity, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0.5&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;15&lt;/span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
This approach was used for prototyping a &lt;a href=&#34;https://issuu.com/australianwater/docs/water_journal_november_2015/64&#34;&gt;software package&lt;/a&gt; to assess water treatment plant data following the Health-Based Targets Manual. The finished product has been written in SQL and is available under an Open Source sharing license.&lt;/p&gt;
&lt;p&gt;
The book &lt;em&gt;Data Science for Water Utilities&lt;/em&gt; discusses &lt;a href=&#34;https://lucidmanager.org/data-science/detecting-outliers-and-anomalies/&#34;&gt;anomaly detection&lt;/a&gt; in more detail.&lt;/p&gt;
&lt;p&gt;

  &lt;div class=&#34;box&#34;&gt;
    &lt;div class=&#34;media&#34;&gt;
      &lt;figure class=&#34;media-left&#34;&gt;
        &lt;p class=&#34;image is-128x128&#34;&gt;
          &lt;img src=&#34;https://lucidmanager.org/images/books/2023_ds4wu.jpg&#34; alt = &#34;Data Science for Water Utilities&#34; title = &#34;Data Science for Water Utilities&#34;&gt;
        &lt;/p&gt;
      &lt;/figure&gt;
      &lt;div class=&#34;media-content&#34;&gt;
        &lt;div class=&#34;content&#34;&gt;
          &lt;p class=&#34;is-size-5 has-text-weight-bold&#34;&gt;Data Science for Water Utilities&lt;/p&gt;
          &lt;p class=&#34;mb-2&#34;&gt;Data Science for Water Utilities published by CRC Press is an applied, practical guide that shows water professionals how to use data science to solve urban water management problems using the R language for statistical computing.&lt;/p&gt;
          &lt;div class=&#34;buttons&#34;&gt;
            &lt;a class=&#34;button is-info&#34; href=&#34;https://routledge.com/9781032354545&#34; target=&#34;_blank&#34;&gt;
              &lt;span class=&#34;icon&#34;&gt;
                &lt;i class=&#34;fas fa-shopping-cart&#34;&gt;&lt;/i&gt;
              &lt;/span&gt;
              &lt;span&gt;Routledge&lt;/span&gt;
            &lt;/a&gt;
            &lt;a class=&#34;button is-info&#34; href=&#34;https://www.amazon.com/Data-Science-Water-Utilities-Chapman/dp/1032354550&#34; target=&#34;_blank&#34;&gt;
              &lt;span class=&#34;icon&#34;&gt;
                &lt;i class=&#34;fas fa-shopping-cart&#34;&gt;&lt;/i&gt;
              &lt;/span&gt;
              &lt;span&gt;Amazon&lt;/span&gt;
            &lt;/a&gt;
          &lt;/div&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://www.r-bloggers.com/&#34; target=&#34;_blank&#34; title=&#34;Proudly associated with R-Bloggers&#34;&gt;
  &lt;button class=&#34;button is-link is-medium&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-r-project&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span&gt;As seen on R Bloggers&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;

      </description>
    </item>
    
    
    
    <item>
      <title>Project Euler 9: Special Pythagorean Triplet</title>
      <link>https://lucidmanager.org/data-science/project-euler-9/</link>
      <pubDate>Thu, 26 Jan 2017 00:00:00 +0000</pubDate>
      <author>Peter Prevos</author>
      <guid>https://lucidmanager.org/data-science/project-euler-9/</guid>
      <description>
	
&lt;p&gt;
Project Euler 9 is about the ancient problem or Pythagorean triples. My background is in carpentry and the essential Pythagorean triple 3:4:5 is a standard tool to check whether an angle is straight. When building a structure, simply mark 3 and 4 units of length on the two sides and check whether the diagonal is precisely five units to ensure your angles are straight. There are, however, an infinite number of triples that form right angles.&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-1&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-1&#34;&gt;
Project Euler 9 Definition
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-1&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;A Pythagorean triplet is a set of three natural numbers, \(a &amp;lt; b &amp;lt; c\), for which, \(a^2 + b^2 = c^2\).&lt;/p&gt;
&lt;p&gt;
For example, \(3^2 + 4^2 = 9 + 16 = 25 = 5^2\).&lt;/p&gt;
&lt;p&gt;
There exists exactly one Pythagorean triplet for which \(a + b + c = 1000\).&lt;/p&gt;
&lt;p&gt;
&lt;a href=&#34;https://projecteuler.net/problem=9&#34;&gt;Find the product&lt;/a&gt; \(abc\).&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-2&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-2&#34;&gt;
Brute Force Solutions
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-2&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;This solution uses brute force and checks all combinations of \(a\), \(b\) and \(c\). To limit the solution space, I used the fact that \(a &amp;lt; b &amp;lt; c\), which implies that \(a &amp;lt; s/3\),  and \(a &amp;lt; b &amp;lt; s/2\), where s is the sum of the three sides.&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://github.com/pprevos/ProjectEuler/&#34; target=&#34;_blank&#34;
   title=&#34;Download ProjectEuler from GitHub&#34;
   alt=&#34;Download ProjectEuler from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;ProjectEuler&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Project Euler 9: Special Pythagorean Triplet&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  a &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  b &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  c &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  s &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1000&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  found &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;FALSE&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;for&lt;/span&gt; (a &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;in&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;floor&lt;/span&gt;((s &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;3&lt;/span&gt;))) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;for&lt;/span&gt; (b &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;in&lt;/span&gt; (a &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;)&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;floor&lt;/span&gt;(s &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;)) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          c &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; s &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; a &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; b
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (a^2 &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; b^2 &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; c^2) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;              found &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;TRUE&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;              &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;break&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (found) &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;break&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  answer &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; a &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; b &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; c
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;print&lt;/span&gt;(answer)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
Alan Jordan provided a faster brute force method. Since \(min(a)=1\) and \(max(b)=499\), we can walk \(a\) and \(b\) towards each other. That way there’s a max of 498 possibilities instead of 292*497 in the first solution.&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;# Improved Brute Force&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  a &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  b &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;499&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;repeat&lt;/span&gt;{
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      c &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sqrt&lt;/span&gt;(a^2 &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; b^2) 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (a &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; b &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; c &lt;span style=&#34;color:#333&#34;&gt;&amp;gt;&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1000&lt;/span&gt;) {b &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; b &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;else&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (a &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; b &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; c &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1000&lt;/span&gt;) {a &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; a &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;else&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (a &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; b &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; c &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1000&lt;/span&gt;) {&lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;break&lt;/span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  answer &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; a &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; b &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; c
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;print&lt;/span&gt;(answer)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-3&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-3&#34;&gt;
Euclid’s Method
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-3&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;Euclid’s formula generates Pythagorean triples for an arbitrary pair of integers m and n with \(m &amp;gt; n &amp;gt; 0\). The formula states that the integers \(a = m^2 - n^2, b = 2mn, c = m^2 = n^2\) form a Pythagorean triple.&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Euclid&amp;#39;s Method&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  abc_sum &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1000&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  x &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; abc_sum &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  min &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;floor&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sqrt&lt;/span&gt;(x &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  max &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ceiling&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sqrt&lt;/span&gt;(x))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  m &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; min&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;max
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  m &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; m[x &lt;span style=&#34;color:#333&#34;&gt;%%&lt;/span&gt; m &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  n &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; ((x &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt; m) &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; m)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  a &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; m &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; n
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  b &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; m^2 &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; n^2
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  c &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; m^2 &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; n^2
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  answer &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; a &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; b &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; c
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;print&lt;/span&gt;(answer)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;figure&gt;
&lt;p alt=&#34;Scatter plot of the legs (a, b) of the first Pythagorean triples with a and b less than 6000. Negative values are included to illustrate the parabolic patterns.&#34; title=&#34;Scatter plot of the legs (a, b) of the first Pythagorean triples with a and b less than 6000. Negative values are included to illustrate the parabolic patterns.&#34;&gt;&lt;img src=&#34;https://upload.wikimedia.org/wikipedia/commons/e/e2/Pythagorean_Triples_Scatter_Plot.png&#34; alt=&#34;https://upload.wikimedia.org/wikipedia/commons/e/e2/Pythagorean_Triples_Scatter_Plot.png&#34; title=&#34;https://upload.wikimedia.org/wikipedia/commons/e/e2/Pythagorean_Triples_Scatter_Plot.png&#34;/&gt;
&lt;a href = &#34;https://www.r-bloggers.com/&#34; target=&#34;_blank&#34; title=&#34;Proudly associated with R-Bloggers&#34;&gt;
  &lt;button class=&#34;button is-link is-medium&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-r-project&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span&gt;As seen on R Bloggers&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;
&lt;/p&gt;
&lt;figcaption&gt;
Scatter plot of the legs (a, b) of the first Pythagorean triples with a and b less than 6000. Negative values are included to illustrate the parabolic patterns. By Dearjean13 CC BY-SA 4.0.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;/div&gt;
&lt;/div&gt;

      </description>
    </item>
    
    
    
    <item>
      <title>Trumpworld Analysis: Ownership Relations in his Business Network</title>
      <link>https://lucidmanager.org/data-science/trumpworld-analysis/</link>
      <pubDate>Mon, 23 Jan 2017 00:00:00 +0000</pubDate>
      <author>Peter Prevos</author>
      <guid>https://lucidmanager.org/data-science/trumpworld-analysis/</guid>
      <description>
	
&lt;p&gt;
You do not need a machine learning algorithm to predict that the presidency of Donald Trump will be controversial. Judging by his pointed language in the campaign, we can rest assured that Trump will break many conventions of world politics. One of the most discussed aspects of his reign is the massive potential for conflicts of interest. Trump&amp;#39;s complex business empire is entangled with national and international politics. Buzzfeed has mapped many of the relationships between businesses and people in what they have dubbed &lt;a href=&#34;https://www.buzzfeed.com/johntemplon/help-us-map-trumpworld&#34;&gt;Trumpworld&lt;/a&gt;. &lt;/p&gt;
&lt;p&gt;
They provided the data to enable citizens data science into the wheeling and dealings of Donald J. Trump. The &lt;a href=&#34;https://docs.google.com/spreadsheets/d/1Z5Vo5pbvxKJ5XpfALZXvCzW26Cl4we3OaN73K9Ae5Ss/edit&#34;&gt;raw data set&lt;/a&gt; consists of three subsets of connections between: &lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Organisations&lt;/li&gt;
&lt;li&gt;People&lt;/li&gt;
&lt;li&gt;People and organisations&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This article presents a quick analysis of the data using the R language for statistical computing and the iGraph package for analysing networks.&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-1&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-1&#34;&gt;
Trumpworld Analysis
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-1&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;This article analyses the connections between organisations using the mighty &lt;a href=&#34;http://igraph.org/r/&#34;&gt;igraph package&lt;/a&gt;.The package implements social network analysis for the R language which can be used for many purposes, such as analysing &lt;a href=&#34;https://lucidmanager.org/data-science/analyse-enron-corpus/&#34;&gt;email traffic&lt;/a&gt; or &lt;a href=&#34;https://lucidmanager.org/data-science/pacific-island-hopping/&#34;&gt;flight paths&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;
The code snippet below reads the data and converts it to a graph that can be examined using social network analysis techniques. I have saved the raw data available on Google Sheets file as CSV files. This data is filtered to contain only ownership relationships and the names of the organisations.&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://github.com/pprevos/digital-humanities/&#34; target=&#34;_blank&#34;
   title=&#34;Download digital-humanities from GitHub&#34;
   alt=&#34;Download digital-humanities from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;digital-humanities&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Analysing Trumpworld&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(tidyverse)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(igraph)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  trumpworld.org &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;read_csv&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;trumpworld/TrumpWorld Data org-org.csv&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;filter&lt;/span&gt;(Connection &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Ownership&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;select&lt;/span&gt;(OrgA &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Organization A&amp;#34;&lt;/span&gt;, OrgB &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Organization B&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  org.ownership &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; trumpworld.org &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      as.matrix &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;graph.edgelist&lt;/span&gt;()
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;par&lt;/span&gt;(mar&lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;rep&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;,&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;4&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;plot&lt;/span&gt;(org.ownership,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  layout &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; layout.fruchterman.reingold,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      vertex.label &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;NA&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      vertex.size &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      edge.arrow.size &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;.1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  )&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/digital-humanities/trumpworld-org1.png&#34; alt=&#34;Trumpworld Analysis: Ownership Relations in his Business Network&#34; title=&#34;Trumpworld Analysis: Ownership Relations in his Business Network&#34;/&gt;
&lt;figcaption&gt;
Trumpworld Analysis: Ownership Relations in his Business Network
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-2&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-2&#34;&gt;
Network Analysis
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-2&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;This network contains 309 ownership relationships between 322 distinct organisations. When we plot the data, we see that most connections are dyadic connections between two firms. The plot is organised with the &lt;a href=&#34;https://en.wikipedia.org/wiki/Force-directed_graph_drawing&#34;&gt;Fruchterman-Reingold&lt;/a&gt; algorithm to improve its clarity. We can also see a large cluster in the centre. The names have been removed for clarity. Our excursion into Trumpland continues by zooming in on the subnetwork. The second code section excises this connected subnetwork so we can analyse it in more detail.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-3&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-3&#34;&gt;
Digging Deeper
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-3&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The node with the highest &lt;a href=&#34;https://en.wikipedia.org/wiki/Degree_(graph_theory)&#34;&gt;degree&lt;/a&gt; identifies the business with the most holdings. This analysis shows that DJT Holdings LLC owns 33 other organisations. These organisations hold other organisations. We can now use the cluster function to investigate this subnetwork using the decompose function, which splits the network into its unconnected subnetworks. The code then selects and visualises the largest of the subnetworks. &lt;/p&gt;
&lt;p&gt;
This Trumpworld analysis shows that the ownership network is a &lt;a href=&#34;https://en.wikipedia.org/wiki/Star_(graph_theory)&#34;&gt;star network&lt;/a&gt;. DJT Holdings LLC centrally controls all organisations. Perhaps this graph visualises the management style of the soon to be president Trump. Trump centrally controls his empire, which is typical of a family business. Does this chart visualise Trump&amp;#39;s leadership style? Is the star network an expression of his lack of trust and thus desire to oversee everything directly?&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;which.max&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;degree&lt;/span&gt;(org.ownership))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  org.ownership.d &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;decompose&lt;/span&gt;(org.ownership)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  largest &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;which.max&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sapply&lt;/span&gt;(org.ownership.d, diameter))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;plot&lt;/span&gt;(org.ownership.d[[largest]],
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;       layout&lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt;layout.fruchterman.reingold,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;       vertex.label.cex &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;.5&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;       vertex.size &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;5&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      edge.arrow.size &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;.1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  )&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;img src=&#34;https://lucidmanager.org/images/digital-humanities/trump-holdings.jpg&#34; alt=&#34;/images/digital-humanities/trump-holdings.jpg&#34; title=&#34;/images/digital-humanities/trump-holdings.jpg&#34; /&gt;&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://www.r-bloggers.com/&#34; target=&#34;_blank&#34; title=&#34;Proudly associated with R-Bloggers&#34;&gt;
  &lt;button class=&#34;button is-link is-medium&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-r-project&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span&gt;As seen on R Bloggers&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;

      </description>
    </item>
    
    
    
    <item>
      <title>Mapping antipodes using the ggmap package</title>
      <link>https://lucidmanager.org/data-science/mapping-antipodes/</link>
      <pubDate>Fri, 20 Jan 2017 00:00:00 +0000</pubDate>
      <author>Peter Prevos</author>
      <guid>https://lucidmanager.org/data-science/mapping-antipodes/</guid>
      <description>
	
&lt;p&gt;
When I was much younger, growing-up in the Netherlands I was fascinated by the conundrum of what happens when you drill a hole straight through the centre of the earth. I always believed that I would turn up in Australia. Well, I did end up in Australia, but not by drilling a hole. But is this really the case? This article is about mapping antipodes using the &lt;a href=&#34;https://github.com/dkahle/ggmap&#34;&gt;ggmap package&lt;/a&gt; of the R language.&lt;/p&gt;
&lt;p&gt;
The antipodes of any place on earth is the place that is diametrically opposite to it. A pair of antipodes are connected by a straight line running through the centre of the Earth. These points are as far away from each other as is possible on this planet. Two people are antipodes when they live on opposite sides of the globe.&lt;/p&gt;
&lt;p&gt;
The term antipodes was first coined by the ancient Greeks. Antipodes are people whose feet (πούς/pouch in Ancient Greek) are directly opposite each other. This concept was a direct consequence the &lt;a href=&#34;https://lucidmanager.org/data-science/mapping-ptolemy-geography/&#34;&gt;Hellenistic understanding of geography&lt;/a&gt;, but the existence of such lands or peoples was often debated or outright denied.&lt;/p&gt;
&lt;p&gt;
How can we use coding in R to solve this conundrum?&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-1&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-1&#34;&gt;
Mapping antipodes with ggmap
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-1&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;We can roughly recreate the antipodean map on Wikipedia with the &lt;a href=&#34;https://cran.r-project.org/web/packages/globe/&#34;&gt;globe package&lt;/a&gt;. This package, written by Adrian Baddeley, plots 2D and 3D views of the earth. The package contains a data file with major coastlines that can be used to create a flipped map of the world. The ggplot2 package uses the globe to visualise maps.&lt;/p&gt;
&lt;p&gt;
The package contains a data file with major coastlines that can be used to create a flipped map of the world. To turn a spatial location into its antipode you subtract 180 degrees from the longitude and reverse the sign of the latitude, shown below.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/geography/antipodes.png&#34; alt=&#34;Antipodean globe&#34; title=&#34;Antipodean globe&#34; width=&#34;500&#34;/&gt;
&lt;figcaption&gt;
Antipodean globe.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
&lt;a href = &#34;https://github.com/pprevos/geography/&#34; target=&#34;_blank&#34;
   title=&#34;Download geography from GitHub&#34;
   alt=&#34;Download geography from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;geography&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(ggplot2)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Antipodean globe&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  world &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;map_data&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;world&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  anti_world &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; world &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;mutate&lt;/span&gt;(long &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; long &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;180&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;             lat &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; lat)           
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ggplot&lt;/span&gt;() &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;geom_polygon&lt;/span&gt;(data &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; world,  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;aes&lt;/span&gt;(long, lat, group &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; group), fill &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;grey&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;geom_polygon&lt;/span&gt;(data &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; anti_world, &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;aes&lt;/span&gt;(long, lat, group &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; group),
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                   fill &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;blue&amp;#34;&lt;/span&gt;, alpha &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0.2&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;coord_map&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;ortho&amp;#34;&lt;/span&gt;, orientation &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;100&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;00&lt;/span&gt;)) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;theme_void&lt;/span&gt;()&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
We can also use the &lt;a href=&#34;https://cran.r-project.org/web/packages/ggmap/index.html&#34;&gt;ggmap package&lt;/a&gt; to visualise antipodes. This package, developed by David Kahle antipodean R-guru Hadley Wickham, has a neat geocoding function to obtain a spatial location. You will need a Google API to enable the &lt;a href=&#34;https://lucidmanager.org/data-science/geocoding-with-ggmap/&#34;&gt;geocoding function&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;
The antipode function takes the description of a location and a zoom level to plot a dot on the antipode location. The gridExtra package is used to create a faceted map, which is not otherwise possible in ggmap.&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(gridExtra)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(ggmap)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  api &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;readLines&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;google.api&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#888&#34;&gt;# Text file with the API key&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;register_google&lt;/span&gt;(key &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; api)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Antipode function&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  antipode &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;function&lt;/span&gt;(location, zm &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;6&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#888&#34;&gt;# Map location&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      lonlat &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;geocode&lt;/span&gt;(location)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      loc1 &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;get_map&lt;/span&gt;(lonlat, zoom &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; zm)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      map1 &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ggmap&lt;/span&gt;(loc1) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;geom_point&lt;/span&gt;(data &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; lonlat, &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;aes&lt;/span&gt;(lon, lat, col &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;red&amp;#34;&lt;/span&gt;, size &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt;)) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;theme&lt;/span&gt;(legend.position &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;none&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#888&#34;&gt;# Define antipode&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      lonlat&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;lon &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; lonlat&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;lon&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;-180&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (lonlat&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;lon &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;-180&lt;/span&gt;) 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          lonlat&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;lon &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;360&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; lonlat&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;lon
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      lonlat&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;lat &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt;lonlat&lt;span style=&#34;color:#333&#34;&gt;$&lt;/span&gt;lat
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      loc2 &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;get_map&lt;/span&gt;(lonlat, zoom &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; zm)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      map2 &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ggmap&lt;/span&gt;(loc2) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;geom_point&lt;/span&gt;(data &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; lonlat, &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;aes&lt;/span&gt;(lon, lat, col &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;red&amp;#34;&lt;/span&gt;, size &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt;)) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;theme&lt;/span&gt;(legend.position &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;none&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;grid.arrange&lt;/span&gt;(map1, map2, nrow &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;antipode&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Rector Nelissenstraat 47 Hoensbroek&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;4&lt;/span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
This code solves the problem I was thinking about as a child. Running the code shows that the antipodes location of the home I grew up in is not in Australia, but quite a way south of New Australia. Although I did end-up living in Australia withouth having to dig a hole in the earth.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/geography/antipode-hoensbroek.jpg&#34; alt=&#34;Antipode map of my birthplace, Hoensbroek, the Netherlands&#34; title=&#34;Antipode map of my birthplace, Hoensbroek, the Netherlands&#34;/&gt;
&lt;figcaption&gt;
Antipode map of my birthplace, Hoensbroek, the Netherlands.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
&lt;a href = &#34;https://www.r-bloggers.com/&#34; target=&#34;_blank&#34; title=&#34;Proudly associated with R-Bloggers&#34;&gt;
  &lt;button class=&#34;button is-link is-medium&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-r-project&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span&gt;As seen on R Bloggers&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;

      </description>
    </item>
    
    
    
    <item>
      <title>Project Euler 8: Largest product in a series</title>
      <link>https://lucidmanager.org/data-science/project-euler-8/</link>
      <pubDate>Thu, 19 Jan 2017 00:00:00 +0000</pubDate>
      <author>Peter Prevos</author>
      <guid>https://lucidmanager.org/data-science/project-euler-8/</guid>
      <description>
	
&lt;p&gt;
Project Euler 8 combines mathematics with text analysis. Just like &lt;a href=&#34;https://lucidmanager.org/data-science/project-euler-11/&#34;&gt;problem 11&lt;/a&gt;, it asks for the largest product in a grid.&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-1&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-1&#34;&gt;
Project Euler 8 Definition
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-1&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The four adjacent digits in the 1,000-digit number below with the greatest product are 9 × 9 × 8 × 9 = 5832.&lt;/p&gt;
&lt;div class=&#34;center-block&#34; style=&#34;text-align: center; margin-left: auto; margin-right: auto;&#34;&gt;
&lt;div class=&#34;src src-text&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-text&#34; data-lang=&#34;text&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;73167176531330624919225119674426574742355349194934
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;96983520312774506326239578318016984801869478851843
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;85861560789112949495459501737958331952853208805511
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;12540698747158523863050715693290963295227443043557
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;66896648950445244523161731856403098711121722383113
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;62229893423380308135336276614282806444486645238749
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;30358907296290491560440772390713810515859307960866
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;70172427121883998797908792274921901699720888093776
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;65727333001053367881220235421809751254540594752243
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;52584907711670556013604839586446706324415722155397
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;53697817977846174064955149290862569321978468622482
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;83972241375657056057490261407972968652414535100474
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;82166370484403199890008895243450658541227588666881
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;16427171479924442928230863465674813919123162824586
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;17866458359124566529476545682848912883142607690042
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;24219022671055626321111109370544217506941658960408
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;07198403850962455444362981230987879927244284909188
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;84580156166097919133875499200524063689912560717606
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;05886116467109405077541002256983155200055935729725
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;71636269561882670428252483600823257530420752963450&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Find the thirteen adjacent digits in the 1,000-digit number that have the greatest product. What is the &lt;a href=&#34;https://projecteuler.net/problem=8&#34;&gt;value of this product&lt;/a&gt;?&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-2&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-2&#34;&gt;
Euler Problem 8 Proposed Solution
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-2&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The first step is to define the digits as a character string with the 1000 digits. The answer is found by cycling through all 13-character n-grams to find the highest product using the prod function. The next step strips the 13 numbers into separate digits and multiplies them with each other with the &lt;code class=&#34;verbatim&#34;&gt;prod()&lt;/code&gt; function.&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://github.com/pprevos/ProjectEuler/&#34; target=&#34;_blank&#34;
   title=&#34;Download ProjectEuler from GitHub&#34;
   alt=&#34;Download ProjectEuler from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;ProjectEuler&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Project Euler 8: Largest product in a series&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  digits &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  ngram &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;13&lt;/span&gt; &lt;span style=&#34;color:#888&#34;&gt;# Define length&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  answer &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;# Clycle through digits&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;for&lt;/span&gt; (i &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;in&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;nchar&lt;/span&gt;(digits) &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; ngram &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;)) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      adjecent &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;substr&lt;/span&gt;(digits, i, i &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; ngram &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;) &lt;span style=&#34;color:#888&#34;&gt;# 13 consecutive digits&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      mult &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;prod&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;as.numeric&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;unlist&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;strsplit&lt;/span&gt;(adjecent, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;&amp;#34;&lt;/span&gt;)))) &lt;span style=&#34;color:#888&#34;&gt;# Define product&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (mult &lt;span style=&#34;color:#333&#34;&gt;&amp;gt;&lt;/span&gt; answer) &lt;span style=&#34;color:#888&#34;&gt;# Largest? &lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          answer &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; mult
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;print&lt;/span&gt;(answer)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;a href = &#34;https://www.r-bloggers.com/&#34; target=&#34;_blank&#34; title=&#34;Proudly associated with R-Bloggers&#34;&gt;
  &lt;button class=&#34;button is-link is-medium&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-r-project&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span&gt;As seen on R Bloggers&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/div&gt;
&lt;/div&gt;

      </description>
    </item>
    
    
    
    <item>
      <title>Project Euler 7: 10,001st Prime Number</title>
      <link>https://lucidmanager.org/data-science/project-euler-7/</link>
      <pubDate>Thu, 12 Jan 2017 00:00:00 +0000</pubDate>
      <author>Peter Prevos</author>
      <guid>https://lucidmanager.org/data-science/project-euler-7/</guid>
      <description>
	
&lt;p&gt;
Project Euler 7 delves into the wonderful world of prime numbers. These numbers are interesting because they don&amp;#39;t follow a predictable pattern. There is no algorithm to calculate primes, which is what makes them valuable in cryptography. &lt;/p&gt;
&lt;p&gt;
As the numbers get larger, the gaps between consecutive primes also increase. There are, however, some interesting patterns. There seem to be infinitely many &lt;a href=&#34;https://oeis.org/A077800&#34;&gt;twin primes&lt;/a&gt;, which are prime numbers that with a difference of two, such as (3, 5), (5, 7) and (11, 13). Another pattern in prime numbers are &lt;a href=&#34;https://oeis.org/A023201&#34;&gt;sexy primes&lt;/a&gt;, which differ by six: e.g. (5,11), (7, 13) and (11, 17). They are called sexy not because these primes are particularly attractive but because of the Latin word for six.&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-1&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-1&#34;&gt;
Project Euler 7 Definition
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-1&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. What is the &lt;a href=&#34;https://projecteuler.net/problem=7&#34;&gt;1,0001st prime number&lt;/a&gt;?&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-2&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-2&#34;&gt;
Solution
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-2&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;Finding the n&lt;sup&gt;th&lt;/sup&gt; prime number can only be solved using brute force because primes do not follow a predictable pattern. We don&amp;#39;t have to check whether a number is divisible by any number between 1 and itself. We only have to check whether a number is divisible by all primes between 2 and its square root. The &lt;code class=&#34;verbatim&#34;&gt;is.prime()&lt;/code&gt; function determines whether a number is a prime number by checking that it is not divisible by any prime number up to the square root of the number. The sieve of Eratosthenes used in &lt;a href=&#34;https://lucidmanager.org/data-science/project-euler-3/&#34;&gt;Euler Problem 3&lt;/a&gt; generates the prime numbers. The next step cycles through all odd numbers and increments a counter when we find a prime, stopping at 10001.&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://github.com/pprevos/ProjectEuler/&#34; target=&#34;_blank&#34;
   title=&#34;Download ProjectEuler from GitHub&#34;
   alt=&#34;Download ProjectEuler from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;ProjectEuler&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Project Euler 7: 10,0001st Prime Number&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;source&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;problem003.R&amp;#34;&lt;/span&gt;, echo &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;FALSE&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  is.prime &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;function&lt;/span&gt;(n) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (n &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;) &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;return&lt;/span&gt;(&lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;FALSE&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (n &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;) &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;return&lt;/span&gt;(&lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;TRUE&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      primes &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;esieve&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ceiling&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sqrt&lt;/span&gt;(n)))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;return&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;prod&lt;/span&gt;(n &lt;span style=&#34;color:#333&#34;&gt;%%&lt;/span&gt; primes &lt;span style=&#34;color:#333&#34;&gt;!=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  answer &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  n &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt; &lt;span style=&#34;color:#888&#34;&gt;# Start counter&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;while&lt;/span&gt; (n &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10001&lt;/span&gt;) { &lt;span style=&#34;color:#888&#34;&gt;# Find 10001 prime numbers&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      answer &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; answer &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt; &lt;span style=&#34;color:#888&#34;&gt;# Next number&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;is.prime&lt;/span&gt;(answer)) { 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          n &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; n &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt; &lt;span style=&#34;color:#888&#34;&gt;# Increment counter&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;print&lt;/span&gt;(answer)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-3&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-3&#34;&gt;
Sexy Primes
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-3&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The largest gap between two primes for the first 10,001 is 72. Sexy primes with a gap of 6 are the most common, as shown in the figure below.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://lucidmanager.org/images/project-euler/problem-007.png&#34; alt=&#34;Frequency or prime gaps for the first 10,001 prime numbers.&#34; title=&#34;Frequency or prime gaps for the first 10,001 prime numbers.&#34;/&gt;
&lt;figcaption&gt;
Frequency or prime gaps for the first 10,001 prime numbers.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Sexy Primes&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(ggplot2)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(dplyr)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  primes &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;esieve&lt;/span&gt;(answer)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  p &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;length&lt;/span&gt;(primes)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  gaps &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;tibble&lt;/span&gt;(Gap &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; primes[2&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;p] &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; primes[1&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;(p &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;)],
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                     Sexy &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; Gap &lt;span style=&#34;color:#333&#34;&gt;%%&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;6&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;count&lt;/span&gt;(Gap, Sexy)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ggplot&lt;/span&gt;(gaps, &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;aes&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;factor&lt;/span&gt;(Gap), n, fill &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; Sexy)) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;geom_col&lt;/span&gt;() &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;scale_fill_manual&lt;/span&gt;(values &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;( &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;#7391AB&amp;#34;&lt;/span&gt;, &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;#A62102&amp;#34;&lt;/span&gt;), name &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Sexy Prime Gap&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;theme_minimal&lt;/span&gt;(base_size &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;labs&lt;/span&gt;(title &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Frequency of prime gaps for the first 10,000 primes&amp;#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          x &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Prime Gap&amp;#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          y &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Frequency&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;guide_legend&lt;/span&gt;(title &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;Sexy Primes&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ggsave&lt;/span&gt;(&lt;span style=&#34;background-color:#fff0f0&#34;&gt;&amp;#34;images/problem-007.png&amp;#34;&lt;/span&gt;, width &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;6&lt;/span&gt;, height &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;4&lt;/span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;figure&gt;
&lt;div style=&#34;position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden;&#34;&gt;
      &lt;iframe allow=&#34;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share; fullscreen&#34; loading=&#34;eager&#34; referrerpolicy=&#34;strict-origin-when-cross-origin&#34; src=&#34;https://www.youtube.com/embed/WJ12DYBuazY?autoplay=0&amp;amp;controls=1&amp;amp;end=0&amp;amp;loop=0&amp;amp;mute=0&amp;amp;start=0&#34; style=&#34;position: absolute; top: 0; left: 0; width: 100%; height: 100%; border:0;&#34; title=&#34;YouTube video&#34;&gt;&lt;/iframe&gt;
    &lt;/div&gt;

&lt;figcaption&gt;
Sexy Primes - Numberphile.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
&lt;a href = &#34;https://www.r-bloggers.com/&#34; target=&#34;_blank&#34; title=&#34;Proudly associated with R-Bloggers&#34;&gt;
  &lt;button class=&#34;button is-link is-medium&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-r-project&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span&gt;As seen on R Bloggers&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;

      </description>
    </item>
    
    
    
    <item>
      <title>Project Euler 6: Sum Square Difference</title>
      <link>https://lucidmanager.org/data-science/project-euler-6/</link>
      <pubDate>Thu, 05 Jan 2017 00:00:00 +0000</pubDate>
      <author>Peter Prevos</author>
      <guid>https://lucidmanager.org/data-science/project-euler-6/</guid>
      <description>
	
&lt;p&gt;
Project problem 6 is another trivial one that only requires a basic understanding of coding in R, but it does teach us something about computer science.&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-1&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-1&#34;&gt;
Project Euler 6 Definition
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-1&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The sum of the squares of the first ten natural numbers is:&lt;/p&gt;
&lt;p&gt;
$$1^2 + 2^2 + \ldots + 10^2 = 385$$&lt;/p&gt;
&lt;p&gt;
The square of the sum of the first ten natural numbers is:&lt;/p&gt;
&lt;p&gt;
$$(1 + 2 + \ldots + 10)^2 = 552 = 3025$$&lt;/p&gt;
&lt;p&gt;
The difference between the sum of the squares of the first ten natural numbers and the square of the sum is \(3025 - 385 = 2640\). Find the &lt;a href=&#34;https://projecteuler.net/problem=6&#34;&gt;difference&lt;/a&gt; between the sum of the squares of the first one hundred natural numbers and the square of the sum.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-2&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-2&#34;&gt;
Solution
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-2&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;This is a straightforward problem for vector processing capabilities in R.&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://github.com/pprevos/ProjectEuler/&#34; target=&#34;_blank&#34;
   title=&#34;Download ProjectEuler from GitHub&#34;
   alt=&#34;Download ProjectEuler from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;ProjectEuler&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#888&#34;&gt;## Project Euler 6: Sum square difference&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;answer &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sum&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;100&lt;/span&gt;)^2 &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sum&lt;/span&gt;((&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;100&lt;/span&gt;)^2)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;print&lt;/span&gt;(answer)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
This problem can also be solved arithmetically. When Carl Friedrich Gauss (1777–1855) when he was a child his teacher challenged his students to add all numbers from 1 to 100. All of his friends struggled to add all the 100 numbers one by one, but Carl completed the task in a few seconds. Gauss found that the sum of natural numbers 1 to \(n\) is:&lt;/p&gt;
&lt;p&gt;
$$\sum_{i=1}^{n} i = \frac{n(n + 1)}{2}$$&lt;/p&gt;
&lt;p&gt;
The code shown above does what Carl’s classmates would have done, using brute force to count the total. Gauss’ solution only required a straightforward step. Computer scientists use &lt;em&gt;Big O&lt;/em&gt; notation to describe the steps it takes to complete an algorithm. In Big-O Notation, the first solution is \(O(n)\) which means that the number of steps in the algorithm grows in direct proportion to the data set. In this case, we only look at 100 numbers. But solving this problem for many billions of numbers would pose some practical issues. The brute-force solution is notated as \(O(n)\) as it takes as many steps as there are elements. Gauss’ solution is \(O(1)\), which means that the solution only requires one step, regardless of the size of the data set.&lt;/p&gt;
&lt;p&gt;
The &lt;a href=&#34;https://brilliant.org/wiki/sum-of-n-n2-or-n3/&#34;&gt;sum of the squares&lt;/a&gt; of the first \(n\) natural numbers is:&lt;/p&gt;
&lt;p&gt;
$$\sum_{i=1}^{n} i^2 = \frac{n(n+1)(2n+1)}{6}$$&lt;/p&gt;
&lt;p&gt;
With these two formulas, a fast solution without having to use loops can be written.&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;n &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;100&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;answer &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; ((n &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; (n &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;)) &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;)^2 &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; (n &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; (n &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; (&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; n &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;)) &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;6&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;print&lt;/span&gt;(answer)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;a href = &#34;https://www.r-bloggers.com/&#34; target=&#34;_blank&#34; title=&#34;Proudly associated with R-Bloggers&#34;&gt;
  &lt;button class=&#34;button is-link is-medium&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-r-project&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span&gt;As seen on R Bloggers&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/div&gt;
&lt;/div&gt;

      </description>
    </item>
    
    
    
    <item>
      <title>Project Euler 5: Smallest Multiple</title>
      <link>https://lucidmanager.org/data-science/project-euler-5/</link>
      <pubDate>Thu, 29 Dec 2016 00:00:00 +0000</pubDate>
      <author>Peter Prevos</author>
      <guid>https://lucidmanager.org/data-science/project-euler-5/</guid>
      <description>
	
&lt;p&gt;
Project Euler 5 relates to the divisibility of numbers and is an almost trivial problem that is included here only for completeness.&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-1&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-1&#34;&gt;
Project Euler 5
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-1&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. What is the smallest positive number that is &lt;a href=&#34;https://projecteuler.net/problem=5&#34;&gt;evenly divisible&lt;/a&gt; by all of the numbers from 1 to 20?&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-2&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-2&#34;&gt;
Solution
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-2&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The solution will also be divisible by the numbers 1 to 10, so we can start at 2520 and increment by 2520. The loop checks whether the number is divisible by the numbers 1 to 20. &lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://github.com/pprevos/ProjectEuler/&#34; target=&#34;_blank&#34;
   title=&#34;Download ProjectEuler from GitHub&#34;
   alt=&#34;Download ProjectEuler from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;ProjectEuler&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Project Euler 5: Smallest Multiple&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  answer &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2520&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;while&lt;/span&gt; (&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sum&lt;/span&gt;(answer &lt;span style=&#34;color:#333&#34;&gt;%%&lt;/span&gt; (&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;20&lt;/span&gt;)) &lt;span style=&#34;color:#333&#34;&gt;!=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      answer &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; answer &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2520&lt;/span&gt; &lt;span style=&#34;color:#888&#34;&gt;# Increase by smallest number divisible by 1:10&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;print&lt;/span&gt;(answer)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
David Radcliffe provided a more generalised way to derive the answer. To solve this problem, we need the Least Common Denominator, which is the smallest number that two or more numbers will divide into evenly. To find the LCD, we first need to know the Greatest Common Divisor \(gcd(x, y)\). The Lowest Common Denominator is the product two numbers, divided by the \(gcd\).&lt;/p&gt;
&lt;p&gt;
The first function calculates the Greatest Common Divisor of tho numbers, \(gcd(x, y)\), using the Euclidean Algorithm in a recursive function. The second function derives the \(lcm\).&lt;/p&gt;
&lt;p&gt;
The &lt;code class=&#34;verbatim&#34;&gt;Reduce()&lt;/code&gt; function takes a function, and a list of data items and recursively applies the function to the elements of the list. Reduce performs the function in the first parameter over the first two elements, then over that result and the third element, and so on. A nice way to see what &lt;code class=&#34;verbatim&#34;&gt;Reduce()&lt;/code&gt; does is to use &lt;code class=&#34;verbatim&#34;&gt;accumulate = TRUE&lt;/code&gt;. This option returns its state after processing the elements of the list.&lt;/p&gt;
&lt;p&gt;
This solution thus finds the Lowest Common Denominator for the numbers 1 to 20.&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Generalised method &lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  gcd &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;function&lt;/span&gt; (x, y) &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;ifelse&lt;/span&gt;(x &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;, y, &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;gcd&lt;/span&gt;(y &lt;span style=&#34;color:#333&#34;&gt;%%&lt;/span&gt; x, x))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  lcm &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;function&lt;/span&gt; (x, y) x &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; y &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;gcd&lt;/span&gt;(x, y)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;Reduce&lt;/span&gt;(lcm, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;20&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;Reduce&lt;/span&gt;(lcm, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;20&lt;/span&gt;, accumulate &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;TRUE&lt;/span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
&lt;a href = &#34;https://www.r-bloggers.com/&#34; target=&#34;_blank&#34; title=&#34;Proudly associated with R-Bloggers&#34;&gt;
  &lt;button class=&#34;button is-link is-medium&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-r-project&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span&gt;As seen on R Bloggers&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;

      </description>
    </item>
    
    
    
    <item>
      <title>Project Euler 4: Largest Palindromic Product</title>
      <link>https://lucidmanager.org/data-science/project-euler-4/</link>
      <pubDate>Thu, 22 Dec 2016 00:00:00 +0000</pubDate>
      <author>Peter Prevos</author>
      <guid>https://lucidmanager.org/data-science/project-euler-4/</guid>
      <description>
	
&lt;p&gt;
Project Euler 4 takes us to the palindromic numbers, a number that stays the same when reading it backwards. Palindromic numbers serve no practical purpose other than recreational mathematics.&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-1&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-1&#34;&gt;
Project Euler 4 Definition
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-1&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99. Find the &lt;a href=&#34;https://projecteuler.net/problem=4&#34;&gt;largest palindrome&lt;/a&gt; made from the product of two 3-digit numbers.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-2&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-2&#34;&gt;
Solution
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-2&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The formal definition for Palindromic numbers in base-\(b\), with \(k\) digits \(a_i\) is:&lt;/p&gt;
&lt;p&gt;
$$\sum_{i=0}^ka_ib^i = \sum_{i=k}^0a_ib^i$$&lt;/p&gt;
&lt;p&gt;
The solution code uses a helper-function to reverse numbers. The &lt;code class=&#34;verbatim&#34;&gt;reverse()&lt;/code&gt; function takes the modulus of 10 to extract the last digit until all digits have been processed. The solution code searches for palindromic numbers, starting at the highest product of three-digit numbers.&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://github.com/pprevos/ProjectEuler/&#34; target=&#34;_blank&#34;
   title=&#34;Download ProjectEuler from GitHub&#34;
   alt=&#34;Download ProjectEuler from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;ProjectEuler&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Project Euler 4: Largest Palindromic Product&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Reverse a number&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  reverse &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;function&lt;/span&gt;(n) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      reverse &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;while&lt;/span&gt; (n &lt;span style=&#34;color:#333&#34;&gt;&amp;gt;&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          reverse &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; reverse &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; (n &lt;span style=&#34;color:#333&#34;&gt;%%&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          n &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;floor&lt;/span&gt;(n &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;return&lt;/span&gt;(reverse)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Euler problem 4&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;for&lt;/span&gt; (i &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;in&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;999&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;900&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;for&lt;/span&gt; (j &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;in&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;990&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;900&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          p &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; i &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; j
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (p &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;reverse&lt;/span&gt;(p)) 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;              &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;break&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (p &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;reverse&lt;/span&gt;(p)) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;break&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  answer &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; i &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; j
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;print&lt;/span&gt;(answer)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-3&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-3&#34;&gt;
Lychrel Numbers
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-3&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The Lychrell process can convert almost all non-palindromic numbers to a palindromic number. The process reverses the non-palindromic number and adds the result to the original number. If the result is not a palindromic number, the process repeats until we reach a palindrome. For example, 59 becomes a palindrome after 3 iterations: 59 + 95 = 154, 154 + 451 = 605, 605 + 506 = 1111.&lt;/p&gt;
&lt;p&gt;
Any number that will never become a palindrome using the Lychrel process is a so-called Lychrel number. Most numbers will convert to a palindrome after a few iterations. However, the number 196 apparently never becomes a palindrome, even after hundreds of thousands of repetitions. There is no proof that Lychrel numbers actually exist. Any code to undertake the Lychrel process could run forever, which is the infamous halting problem in computer science. &lt;a href=&#34;https://oeis.org/A023108&#34;&gt;Sequence A023108&lt;/a&gt; of the Online Encyclopedia of Integers lists numbers that apparently never iterate to become a palindrome.&lt;/p&gt;
&lt;p&gt;
The &lt;code class=&#34;verbatim&#34;&gt;lychrel()&lt;/code&gt; function tests a number for Lychrel property by following the palindrome process. The output of this function is the resulting palindrome. As these numbers can be huge, we maximise the number of available digits. Unfortunately, there is no way of telling if it will ever stop for certain numbers. We know that the first 195 will terminate, but the number 196 &lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Lychrel Numbers&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  lychrel &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;function&lt;/span&gt;(l, verbose &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;FALSE&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;while&lt;/span&gt;(l &lt;span style=&#34;color:#333&#34;&gt;!=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;reverse&lt;/span&gt;(l)) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      l &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; l &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;reverse&lt;/span&gt;(l)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;print&lt;/span&gt;(l)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  lychrel &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;function&lt;/span&gt;(l) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;while&lt;/span&gt;(l &lt;span style=&#34;color:#333&#34;&gt;!=&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;reverse&lt;/span&gt;(l))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      l &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; l &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;reverse&lt;/span&gt;(l)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;return&lt;/span&gt;(l)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## First 195 non-Lychrel numbers&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;options&lt;/span&gt;(digits &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;22&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sapply&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;195&lt;/span&gt;, lychrel)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## This function call apparently never halts - run at your own risk!&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;lychrel&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;196&lt;/span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;figure&gt;
&lt;p&gt;&lt;div style=&#34;position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden;&#34;&gt;
      &lt;iframe allow=&#34;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share; fullscreen&#34; loading=&#34;eager&#34; referrerpolicy=&#34;strict-origin-when-cross-origin&#34; src=&#34;https://www.youtube.com/embed/bN8PE3eljdA?autoplay=0&amp;amp;controls=1&amp;amp;end=0&amp;amp;loop=0&amp;amp;mute=0&amp;amp;start=0&#34; style=&#34;position: absolute; top: 0; left: 0; width: 100%; height: 100%; border:0;&#34; title=&#34;YouTube video&#34;&gt;&lt;/iframe&gt;
    &lt;/div&gt;

&lt;a href = &#34;https://www.r-bloggers.com/&#34; target=&#34;_blank&#34; title=&#34;Proudly associated with R-Bloggers&#34;&gt;
  &lt;button class=&#34;button is-link is-medium&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-r-project&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span&gt;As seen on R Bloggers&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;
&lt;/p&gt;
&lt;figcaption&gt;
What&amp;#39;s special about 196? - Numberphile.
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;/div&gt;
&lt;/div&gt;

      </description>
    </item>
    
    
    
    <item>
      <title>Project Euler 3: Largest Prime Factor</title>
      <link>https://lucidmanager.org/data-science/project-euler-3/</link>
      <pubDate>Fri, 09 Dec 2016 00:00:00 +0000</pubDate>
      <author>Peter Prevos</author>
      <guid>https://lucidmanager.org/data-science/project-euler-3/</guid>
      <description>
	
&lt;p&gt;
Project Euler 3 is about prime factors, which are some of the most important numbers of the digital economy. RSA encryption is based on the fact that determining the prime factors of huge numbers takes a very long time. So much time in fact that the cost of breaking the encryption outweigh the benefits of obtaining the secret.&lt;/p&gt;
&lt;p&gt;
Prime numbers are the basic building blocks of the natural numbers as every number, except the primes themselves. Every natural number can be written as the product of a series of primes. The RSA encryption system uses large primes to keep electronic messages away from prying eyes, as explained in the Numberphile video below.&lt;/p&gt;
&lt;p&gt;
&lt;div style=&#34;position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden;&#34;&gt;
      &lt;iframe allow=&#34;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share; fullscreen&#34; loading=&#34;eager&#34; referrerpolicy=&#34;strict-origin-when-cross-origin&#34; src=&#34;https://www.youtube.com/embed/M7kEpw1tn50?autoplay=0&amp;amp;controls=1&amp;amp;end=0&amp;amp;loop=0&amp;amp;mute=0&amp;amp;start=0&#34; style=&#34;position: absolute; top: 0; left: 0; width: 100%; height: 100%; border:0;&#34; title=&#34;YouTube video&#34;&gt;&lt;/iframe&gt;
    &lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
Prime numbers are a favourite topic in Project Euler. &lt;a href=&#34;https://lucidmanager.org/data-science/project-euler-7/&#34;&gt;Problem 7&lt;/a&gt; asks for the first ten thousand primes, and &lt;a href=&#34;https://lucidmanager.org/data-science/project-euler-10/&#34;&gt;Problem 10&lt;/a&gt; looks at the sum of all the primes below two million.&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-1&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-1&#34;&gt;
Project Euler 3 Definition
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-1&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The prime factors of 13195 are 5, 7, 13 and 29. What is the &lt;a href=&#34;https://projecteuler.net/problem=3&#34;&gt;largest prime factor&lt;/a&gt; of the number 600851475143?&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-2&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-2&#34;&gt;
Generating Prime Numbers
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-2&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;This solution relies on two functions that can be used for multiples problems. The Sieve of Eratosthenes generates prime numbers from 2 to &lt;em&gt;n&lt;/em&gt;. The code is commented to explain the sieve. The image shows how numbers from 1 to 100 are sieved to find the primes.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://upload.wikimedia.org/wikipedia/commons/b/b9/Sieve_of_Eratosthenes_animation.gif&#34; alt=&#34;Sieve of Eratosthenes (Source: SKopp at German Wikipedia / CC BY-SA).&#34; title=&#34;Sieve of Eratosthenes (Source: SKopp at German Wikipedia / CC BY-SA).&#34;/&gt;
&lt;figcaption&gt;
Sieve of Eratosthenes (Source: SKopp at German Wikipedia / CC BY-SA).
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;
This problem uses two functions: &lt;code class=&#34;verbatim&#34;&gt;prime.factors()&lt;/code&gt; and &lt;code class=&#34;verbatim&#34;&gt;esieve()&lt;/code&gt;. The &lt;code class=&#34;verbatim&#34;&gt;prime.factors()&lt;/code&gt; function generates the list of unique prime divisors and then produces the factors. The factors are identified by dividing the number by the candidate prime factors until the result is 1.&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-3&#34; class=&#34;outline-3&#34;&gt;
&lt;h3 id=&#34;headline-3&#34;&gt;
Base R Solution
&lt;/h3&gt;
&lt;div id=&#34;outline-text-headline-3&#34; class=&#34;outline-text-3&#34;&gt;
&lt;a href = &#34;https://github.com/pprevos/ProjectEuler/&#34; target=&#34;_blank&#34;
   title=&#34;Download ProjectEuler from GitHub&#34;
   alt=&#34;Download ProjectEuler from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;ProjectEuler&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;


&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Project Euler 3: Largest Prime Factor&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Base R solution&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  esieve &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;function&lt;/span&gt;(n) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (n &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;) &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;return&lt;/span&gt;(&lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;NULL&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (n &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;) &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;return&lt;/span&gt;(n)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#888&#34;&gt;## Create a list of consecutive integers {2,3,... n}.&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      l &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;n
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#888&#34;&gt;## Start counter&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      i &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#888&#34;&gt;## Select p as the first prime number in the list, p = 2.&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      p &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;while&lt;/span&gt; (p^2 &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;=&lt;/span&gt; n) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          &lt;span style=&#34;color:#888&#34;&gt;## Remove all multiples of p from the l.&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          l &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; l[l &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; p &lt;span style=&#34;color:#333&#34;&gt;|&lt;/span&gt; l &lt;span style=&#34;color:#333&#34;&gt;%%&lt;/span&gt; p&lt;span style=&#34;color:#333&#34;&gt;!=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          &lt;span style=&#34;color:#888&#34;&gt;## set p equal to the next integer in l which has not been removed.&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          i &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; i &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt; &lt;span style=&#34;color:#888&#34;&gt;## Repeat steps 3 and 4 until p^2 &amp;gt; n,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          &lt;span style=&#34;color:#888&#34;&gt;## all the remaining numbers in the list are primes&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          p &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; l[i]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;return&lt;/span&gt;(l)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  prime.factors &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;function&lt;/span&gt; (n) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#888&#34;&gt;## Define list of factors&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      factors &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;()
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#888&#34;&gt;## Define primes to be tested&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      primes &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;esieve&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;floor&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sqrt&lt;/span&gt;(n)))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#888&#34;&gt;## Idenitfy prime divisors&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      d &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;which&lt;/span&gt;(n &lt;span style=&#34;color:#333&#34;&gt;%%&lt;/span&gt; primes &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;) 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#888&#34;&gt;## No prime divisors&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;length&lt;/span&gt;(d) &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;) 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;return&lt;/span&gt;(n)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#888&#34;&gt;## Test candidate primes&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;for&lt;/span&gt; (q &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;in&lt;/span&gt; primes[d]) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          &lt;span style=&#34;color:#888&#34;&gt;## Generate list of factors&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;while&lt;/span&gt; (n &lt;span style=&#34;color:#333&#34;&gt;%%&lt;/span&gt; q &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;              factors &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(factors, q)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;              n &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; n&lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt;q } } 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;              &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (n &lt;span style=&#34;color:#333&#34;&gt;&amp;gt;&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;) 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                  factors &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(factors, n)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;return&lt;/span&gt;(factors)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;max&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;prime.factors&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;600851475143&lt;/span&gt;))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-4&#34; class=&#34;outline-3&#34;&gt;
&lt;h3 id=&#34;headline-4&#34;&gt;
Number package
&lt;/h3&gt;
&lt;div id=&#34;outline-text-headline-4&#34; class=&#34;outline-text-3&#34;&gt;
&lt;p&gt;The solution can also be found by using the &lt;code class=&#34;verbatim&#34;&gt;primeFactors()&lt;/code&gt; function in the &lt;a href=&#34;https://cran.r-project.org/web/packages/numbers/index.html&#34;&gt;numbers package&lt;/a&gt;. This package provides a range of functions related to prime numbers and is much faster than the basic R code.&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Numbers package&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(numbers)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;max&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;primeFactors&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;600851475143&lt;/span&gt;))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
&lt;a href = &#34;https://www.r-bloggers.com/&#34; target=&#34;_blank&#34; title=&#34;Proudly associated with R-Bloggers&#34;&gt;
  &lt;button class=&#34;button is-link is-medium&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-r-project&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span&gt;As seen on R Bloggers&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

      </description>
    </item>
    
    
    
    <item>
      <title>Project Euler 2: Sum of Even Fibonacci Numbers</title>
      <link>https://lucidmanager.org/data-science/project-euler-2/</link>
      <pubDate>Thu, 08 Dec 2016 00:00:00 +0000</pubDate>
      <author>Peter Prevos</author>
      <guid>https://lucidmanager.org/data-science/project-euler-2/</guid>
      <description>
	
&lt;p&gt;
Project Euler 2 looks at Fibonacci numbers. This number sequence seems to describe our sense of natural beauty and aesthetics. The spiral staircase uses Fibonacci numbers as part of its geometry. Euler Problem 2 is a bit less poetic as it only asks to generate and sum even numbers. &lt;a href=&#34;https://lucidmanager.org/data-science/project-euler-25/&#34;&gt;Euler Problem 25&lt;/a&gt; also deals with Fibonacci numbers and asks to find the first such number with 1000 digits.&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-1&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-1&#34;&gt;
Project Euler 2 Definition
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-1&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:&lt;/p&gt;
&lt;p&gt;
$1, 2, 3, 5, 8, 13, 21, 34, 55, 89, \ldots$&lt;/p&gt;
&lt;p&gt;
By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the &lt;a href=&#34;https://projecteuler.net/problem=2&#34;&gt;sum of the even-valued terms&lt;/a&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-2&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-2&#34;&gt;
Euler Problem 2 Solution in R
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-2&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;The code generates Fibonacci numbers until it reaches the value of four million. The code then sums the even numbers in the sequence. &lt;/p&gt;
&lt;p&gt;
The intuitive way to calculate Fibonacci numbers is to use a recursive function. This function calls itself until it reaches the first two numbers.&lt;/p&gt;
&lt;p&gt;
&lt;a href = &#34;https://github.com/pprevos/ProjectEuler/&#34; target=&#34;_blank&#34;
   title=&#34;Download ProjectEuler from GitHub&#34;
   alt=&#34;Download ProjectEuler from GitHub&#34;&gt;
  &lt;button class=&#34;button is-medium is-primary&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-github&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span style=&#34;font-family: monospace&#34;&gt;ProjectEuler&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;

&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Project Euler 2: Sum of Even Fibonacci Numbers&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Recursive function&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  fib &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;function&lt;/span&gt;(n) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (n &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;) &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;return&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;return&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;fib&lt;/span&gt;(n &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;fib&lt;/span&gt;(n &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;fib&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;15&lt;/span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
This function calculates the \(n^{th}\) Fibonacci number by calling itself with a reduced index until it reaches 1. Recursive functions are, however, very memory hungry and not very fast. A much faster method is to create a vector of Fibonacci numbers and add the even ones. The code below generates Fibonacci numbers until it reaches the value of four million. The code then sums the even numbers in the sequence.&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Sequential function&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  fib &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;while&lt;/span&gt; (&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;max&lt;/span&gt;(fib) &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;4E+06&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      len &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;length&lt;/span&gt;(fib)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      fib &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;c&lt;/span&gt;(fib, fib[len &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;] &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; fib[len])
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  answer &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sum&lt;/span&gt;(fib[fib &lt;span style=&#34;color:#333&#34;&gt;%%&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;])
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;print&lt;/span&gt;(answer)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-3&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-3&#34;&gt;
Using the GMP Package
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-3&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;A series of R packages exist to generate Fibonacci numbers. The &lt;a href=&#34;https://cran.r-project.org/web/packages/gmp/index.html&#34;&gt;GMP package&lt;/a&gt; for Multiple Precision Arithmetic provides the &lt;code class=&#34;verbatim&#34;&gt;fibnum()&lt;/code&gt; function to calculate the n^th Fibonacci number. This package is also able to work with huge numbers. Using this package is much faster than the base R code used above. Interestingly, the GMP library uses tables for Fibonacci numbers up to twelve quintillion (10&lt;sup&gt;18&lt;/sup&gt;).&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;# Using the GMP Package&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;library&lt;/span&gt;(gmp)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  i &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  answer &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  fib &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;fibnum&lt;/span&gt;(i)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;while&lt;/span&gt; (fib &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;=&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;4E6&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (fib &lt;span style=&#34;color:#333&#34;&gt;%%&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      answer &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; answer &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; fib
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    i &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; i &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    fib &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;fibnum&lt;/span&gt;(i)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;print&lt;/span&gt;(answer)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-4&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-4&#34;&gt;
Analytic Solution
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-4&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;An analytical solution prevents using recursion and a brute force method to find the answer. Binet&amp;#39;s formula for the n^th Fibonacci number lets us determine a Fibonacci number without recursion:&lt;/p&gt;
&lt;p&gt;
$$F_n=\frac{\varphi^n-\frac{1}{(-\varphi)^n}}{\sqrt{5}}$$&lt;/p&gt;
&lt;p&gt;
We know that $\varphi$ is the Golden Ratio, so we can simplify this formula:&lt;/p&gt;
&lt;p&gt;
$$F_n\sqrt{5}=\frac{(1+\sqrt{5})^n}{2^n}$$&lt;/p&gt;
&lt;p&gt;
This formula results in a simple function to generate a sequence of Fibonacci numbers:&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;## Analytical solution&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  binet &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;function&lt;/span&gt;(n) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;round&lt;/span&gt;((((&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sqrt&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;5&lt;/span&gt;))^n) &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;^n) &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sqrt&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;5&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;binet&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;10&lt;/span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
The &lt;a href=&#34;https://math.stackexchange.com/questions/374758/how-can-i-find-an-inverse-to-the-binet-formula&#34;&gt;inverse of the Binet formula&lt;/a&gt; gives us:&lt;/p&gt;
&lt;p&gt;
$n=\left[ \log_\phi \sqrt{5}(F_n-\frac{1}{2}) \right]$ holds for all $n\ge 3$&lt;/p&gt;
&lt;p&gt;
This formula allows us to work out which Fibonacci number is less than or equal to 4 million:&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  phi &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; (&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sqrt&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;5&lt;/span&gt;)) &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  n_4e6 &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;floor&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;log&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sqrt&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;5&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; (&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;4E6&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0.5&lt;/span&gt;), base &lt;span style=&#34;color:#333&#34;&gt;=&lt;/span&gt; phi))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
Using this formula, we find that the first 33 Fibonacci numbers are equal or less than four million. Next step is to sum all the even numbers.&lt;/p&gt;
&lt;p&gt;
According to the &lt;a href=&#34;https://oeis.org/A014445&#34;&gt;Online Encyclopedia of Integer Sequences&lt;/a&gt; (lemma A014445), every third Fibonacci number is even. We can now use Binet&amp;#39;s formula to sum every third Fibonacci number.&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sum&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;binet&lt;/span&gt;(&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;seq&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;3&lt;/span&gt;, n_4e6, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;3&lt;/span&gt;)))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
&lt;a href = &#34;https://www.r-bloggers.com/&#34; target=&#34;_blank&#34; title=&#34;Proudly associated with R-Bloggers&#34;&gt;
  &lt;button class=&#34;button is-link is-medium&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-r-project&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span&gt;As seen on R Bloggers&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;

      </description>
    </item>
    
    
    
    <item>
      <title>Project Euler 1: Multiples of 3 and 5</title>
      <link>https://lucidmanager.org/data-science/project-euler-1/</link>
      <pubDate>Thu, 01 Dec 2016 00:00:00 +0000</pubDate>
      <author>Peter Prevos</author>
      <guid>https://lucidmanager.org/data-science/project-euler-1/</guid>
      <description>
	
&lt;p&gt;
This &lt;a href=&#34;http://projecteuler.net/problem=001&#34;&gt;first Euler problem&lt;/a&gt; provides a gentle introduction to solving mathematical problems with code. This problem involves finding the sum of two related arithmetic sequences.&lt;/p&gt;
&lt;p&gt;
The natural numbers below 10 that are multiples of 3 or 5 results are 3, 5, 6 and 9. The sum of these numbers is $3+5+6+9=23$. Your assignment is to find the sum of all the multiples of 3 or 5 below 1000.&lt;/p&gt;
&lt;p&gt;
This problem is similar to one posed to a mathematics child prodigy that lived around the same time as Euler. When Carl Friedrich Gauss (1777–1855) was ten years old, his teacher gave his pupils a maths problem to ponder on. The teacher was perhaps seeking some quiet time for himself by giving the kids a hard problem. He asked them to sum all the numbers from 1 to 100, which surely would give him enough time to enjoy a cuppa.&lt;/p&gt;
&lt;p&gt;
Young Carl Gauss stood up after just a minute and confidently proudly raised his slate with 5050 written in large chalk letters to the teacher. All the other kids were still diligently adding numbers. How was Carl able to finish his task so quickly?  The &lt;a href=&#34;https://doi.org/10.1511/2006.59.200&#34;&gt;details and full truth of this story&lt;/a&gt; are lost in history. Perhaps Carl realised the solution while writing the series forwards and backwards:&lt;/p&gt;
&lt;p&gt;
$$1 + 2 + 3 + \ldots + 98 + 99 + 100$$&lt;/p&gt;
&lt;p&gt;
$$100 + 99 + 98 + \ldots + 3 + 2 + 1$$&lt;/p&gt;
&lt;p&gt;
Little Carl calculated the sum of some of the pairs and noticed a regularity:&lt;/p&gt;
&lt;p&gt;
$$1 + 100 = 2 + 99 = 3 + 98 = \ldots 100 + 1 = 99 + 2 = 98 + 3 = 101$$&lt;/p&gt;
&lt;p&gt;
Each of the pairs adds up to 101 and there are 50 pairs, therefore the sum of all numbers is half the sum of all pairs. Gauss was not the first to discover this method. Some thirteen hundred years earlier, Indian mathematician and astronomer Aryabhata had already discovered this method to sum a arithmetical sequence.&lt;/p&gt;
&lt;div id=&#34;outline-container-headline-1&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-1&#34;&gt;
Proposed Solutions
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-1&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;This anecdote shows that there are two ways to solve this first problem. The smart method that Gauss used or the slow brute force method that his fellow pupils used. The brute force solution requires many steps to find the answer. The smart solution uses a one-step formula, such as the young Gauss and Aryabhata had figured out.&lt;/p&gt;
&lt;p&gt;
The most inefficient solution is to test whether all numbers between 1 and 999 are divisible by 3 or 5 and then add all these number. The R language provides several ways to step through a sequence of numbers.&lt;/p&gt;
&lt;p&gt;
The code below loops trough the numbers between 1 and 999.&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;# Project Euler 1: Multiples of 3 and 5&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;# Loop&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  answer &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;for&lt;/span&gt; (i &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;in&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;999&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;if&lt;/span&gt; (i &lt;span style=&#34;color:#333&#34;&gt;%%&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;3&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;|&lt;/span&gt; i &lt;span style=&#34;color:#333&#34;&gt;%%&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;5&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      answer &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; answer &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; i
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;print&lt;/span&gt;(i)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;print&lt;/span&gt;(answer)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
This first solution is elaborate. Using for-loops in R should always be a last resort in R coding because they are the slowest way to compute over a series of steps. The next two solutions solve this problem by using vector arithmetic. These methods are much faster because they run the loop directly in machine code instead of having to compile each step.&lt;/p&gt;
&lt;p&gt;
The solution for the first problem only requires one line of code when using vector arithmetic.&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;# Vector aritmetic&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;sum&lt;/span&gt;((&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;999&lt;/span&gt;)&lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;[&lt;/span&gt;((&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;999&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;%%&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;3&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;|&lt;/span&gt; ((&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#333&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;999&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;%%&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;5&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;0&lt;/span&gt;)])&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;outline-container-headline-2&#34; class=&#34;outline-2&#34;&gt;
&lt;h2 id=&#34;headline-2&#34;&gt;
Analytical solution
&lt;/h2&gt;
&lt;div id=&#34;outline-text-headline-2&#34; class=&#34;outline-text-2&#34;&gt;
&lt;p&gt;Although the vector arithmetic is much faster than the for-loop, it is still not as fast as using the methods discovered by Aryabhata and Gauss. The two brute-force solutions are very fast because of the low number of steps to find a solution. An analytical solution reduces the processing time significantly, as it only requires one step.&lt;/p&gt;
&lt;p&gt;
The problem involves &lt;em&gt;arithmetic progressions&lt;/em&gt;, which are sequences of numbers with a constant difference. The sequence 1, 4, 7, 10, 13, … is an arithmetic progression with a common difference of three.&lt;/p&gt;
&lt;p&gt;
We can generalise the method discovered by Gauss. The sum of an arithmetic progression ($S_n$), where $n$ is the number of elements and $a_1$ and $a_n$ are the lowest and highest value, is:&lt;/p&gt;
&lt;p&gt;
$$S_n= \frac{n(a_{1} + a_n)}{2}$$&lt;/p&gt;
&lt;p&gt;
The numbers divisible by $n=3$ can be expressed as:&lt;/p&gt;
&lt;p&gt;
$$\mathrm{sum}_3(999)=3+6+9+12+ \ldots + 999 = 3(1+2+3+4+ \ldots + 333)$$&lt;/p&gt;
&lt;p&gt;
We can now calculate the sum of all divisors by combining the above progression with the formula for arithmetic progressions as expressed in the above code, where $m$ is the divisor and $n$ the extent of the sequence. $p$ is the highest number less than $n$ divisible by $m$. In the case of 5, this number is 995.&lt;/p&gt;
&lt;p&gt;
$$p = n \lfloor (m/n) \rfloor$$&lt;/p&gt;
&lt;p&gt;
Substitution gives:&lt;/p&gt;
&lt;p&gt;
$$\mathrm{sum}_m(n) =  p \frac{1+(p/m)}{2}$$&lt;/p&gt;
&lt;div class=&#34;src src-r&#34;&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#888&#34;&gt;# Analytical solution&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  SumDivBy &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;function&lt;/span&gt;(m, n) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      p &lt;span style=&#34;color:#333&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;floor&lt;/span&gt;(n &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt; m) &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; m &lt;span style=&#34;color:#888&#34;&gt;# Round to multiple of n&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#080;font-weight:bold&#34;&gt;return&lt;/span&gt; (p &lt;span style=&#34;color:#333&#34;&gt;*&lt;/span&gt; (&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;1&lt;/span&gt; &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; (p &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt; m)) &lt;span style=&#34;color:#333&#34;&gt;/&lt;/span&gt; &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;2&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;SumDivBy&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;3&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;999&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;SumDivBy&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;5&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;999&lt;/span&gt;) &lt;span style=&#34;color:#333&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#06b;font-weight:bold&#34;&gt;SumDivBy&lt;/span&gt;(&lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;15&lt;/span&gt;, &lt;span style=&#34;color:#60e;font-weight:bold&#34;&gt;999&lt;/span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
&lt;a href = &#34;https://www.r-bloggers.com/&#34; target=&#34;_blank&#34; title=&#34;Proudly associated with R-Bloggers&#34;&gt;
  &lt;button class=&#34;button is-link is-medium&#34;&gt;
    &lt;span class=&#34;icon is-large&#34;&gt;
      &lt;i class=&#34;fab fa-r-project&#34;&gt;&lt;/i&gt;
    &lt;/span&gt;
    &lt;span&gt;As seen on R Bloggers&lt;/span&gt;
  &lt;/button&gt;
&lt;/a&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;

      </description>
    </item>
    
    
  </channel>
</rss>
