<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/exploring-world-gender-statistics-with-shiny/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://shiring.github.io/shiny/2017/01/29/WGS"> Shirin's playgRound</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>This week I explored the World Gender Statistics dataset. You can look at 160 measurements over 56 years with <a target="_blank" rel="nofollow" href="https://shiring.shinyapps.io/wgs_app/">my Shiny app here</a>.</p>
<p><img alt="" src="https://i0.wp.com/shiring.github.io/shiny/2017/01/29/wgs_app.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p></p>
<p>I prepared the data as follows:</p>
<h3>Data.csv</h3>
<ul>
<li>Country.Name: the name of the country</li>
<li>Country.Code: the country’s code</li>
<li>Indicator.Name: the name of the variable that this row represents</li>
<li>Indicator.Code: a unique id for the variable</li>
<li>1960 – 2016: one column EACH for the value of the variable in each year it was available</li>
</ul>
<div>
<pre><code><span>dataset</span><span> </span><span>&lt;-</span><span> </span><span>read.csv</span><span>(</span><span>&quot;Data.csv&quot;</span><span>)</span><span>
</span><span>dataset_subs</span><span> </span><span>&lt;-</span><span> </span><span>dataset</span><span>[</span><span>grep</span><span>(</span><span>&quot;.FE|.MA&quot;</span><span>,</span><span> </span><span>dataset</span><span>$</span><span>Indicator.Code</span><span>),</span><span> </span><span>]</span><span>
</span><span>head</span><span>(</span><span>dataset_subs</span><span>)</span><span>
</span><span>dataset_subs</span><span>$</span><span>Indicator.Name</span><span> </span><span>&lt;-</span><span> </span><span>as.character</span><span>(</span><span>dataset_subs</span><span>$</span><span>Indicator.Name</span><span>)</span><span>
</span><span>dataset_fem</span><span> </span><span>&lt;-</span><span> </span><span>dataset</span><span>[</span><span>grep</span><span>(</span><span>&quot;female&quot;</span><span>,</span><span> </span><span>dataset</span><span>$</span><span>Indicator.Name</span><span>),</span><span> </span><span>]</span><span>
</span><span>dataset_fem</span><span>$</span><span>Indicator.Name</span><span> </span><span>&lt;-</span><span> </span><span>gsub</span><span>(</span><span>&quot;female&quot;</span><span>,</span><span> </span><span>&quot;&quot;</span><span>,</span><span> </span><span>dataset_fem</span><span>$</span><span>Indicator.Name</span><span>)</span><span>
</span><span>dataset_fem</span><span>$</span><span>Indicator.Code</span><span> </span><span>&lt;-</span><span> </span><span>gsub</span><span>(</span><span>&quot;.FE&quot;</span><span>,</span><span> </span><span>&quot;&quot;</span><span>,</span><span> </span><span>dataset_fem</span><span>$</span><span>Indicator.Code</span><span>)</span><span>
</span><span>dataset_fem</span><span>$</span><span>gender</span><span> </span><span>&lt;-</span><span> </span><span>&quot;female&quot;</span><span>
</span><span>dataset_male</span><span> </span><span>&lt;-</span><span> </span><span>dataset</span><span>[</span><span>-</span><span>grep</span><span>(</span><span>&quot;female&quot;</span><span>,</span><span> </span><span>dataset</span><span>$</span><span>Indicator.Name</span><span>),</span><span> </span><span>]</span><span>
</span><span>dataset_male</span><span>$</span><span>Indicator.Name</span><span> </span><span>&lt;-</span><span> </span><span>gsub</span><span>(</span><span>&quot;male&quot;</span><span>,</span><span> </span><span>&quot;&quot;</span><span>,</span><span> </span><span>dataset_male</span><span>$</span><span>Indicator.Name</span><span>)</span><span>
</span><span>dataset_male</span><span>$</span><span>Indicator.Code</span><span> </span><span>&lt;-</span><span> </span><span>gsub</span><span>(</span><span>&quot;.FE&quot;</span><span>,</span><span> </span><span>&quot;&quot;</span><span>,</span><span> </span><span>dataset_male</span><span>$</span><span>Indicator.Code</span><span>)</span><span>
</span><span>dataset_male</span><span>$</span><span>gender</span><span> </span><span>&lt;-</span><span> </span><span>&quot;male&quot;</span><span>
</span><span>dataset_fem</span><span> </span><span>&lt;-</span><span> </span><span>dataset_fem</span><span>[</span><span>which</span><span>(</span><span>dataset_fem</span><span>$</span><span>Indicator.Name</span><span> </span><span>%in%</span><span> </span><span>dataset_male</span><span>$</span><span>Indicator.Name</span><span>),</span><span> </span><span>]</span><span>
</span><span>dataset_male</span><span> </span><span>&lt;-</span><span> </span><span>dataset_male</span><span>[</span><span>which</span><span>(</span><span>dataset_male</span><span>$</span><span>Indicator.Name</span><span> </span><span>%in%</span><span> </span><span>dataset_fem</span><span>$</span><span>Indicator.Name</span><span>),</span><span> </span><span>]</span><span>
</span><span>dataset_fem</span><span> </span><span>&lt;-</span><span> </span><span>dataset_fem</span><span>[</span><span>which</span><span>(</span><span>dataset_fem</span><span>$</span><span>Country.Code</span><span> </span><span>%in%</span><span> </span><span>dataset_male</span><span>$</span><span>Country.Code</span><span>),</span><span> </span><span>]</span><span>
</span><span>dataset_male</span><span> </span><span>&lt;-</span><span> </span><span>dataset_male</span><span>[</span><span>which</span><span>(</span><span>dataset_male</span><span>$</span><span>Country.Code</span><span> </span><span>%in%</span><span> </span><span>dataset_fem</span><span>$</span><span>Country.Code</span><span>),</span><span> </span><span>]</span><span>
</span><span>library</span><span>(</span><span>dplyr</span><span>)</span><span>
</span><span>dataset_fem</span><span> </span><span>&lt;-</span><span> </span><span>arrange</span><span>(</span><span>dataset_fem</span><span>,</span><span> </span><span>Country.Code</span><span>)</span><span>
</span><span>dataset_male</span><span> </span><span>&lt;-</span><span> </span><span>arrange</span><span>(</span><span>dataset_male</span><span>,</span><span> </span><span>Country.Code</span><span>)</span><span>
</span><span>dataset_fem</span><span>$</span><span>Country.Code</span><span> </span><span>&lt;-</span><span> </span><span>as.character</span><span>(</span><span>dataset_fem</span><span>$</span><span>Country.Code</span><span>)</span><span>
</span><span>dataset_male</span><span>$</span><span>Country.Code</span><span> </span><span>&lt;-</span><span> </span><span>as.character</span><span>(</span><span>dataset_male</span><span>$</span><span>Country.Code</span><span>)</span><span>
</span><span>save</span><span>(</span><span>dataset_fem</span><span>,</span><span> </span><span>file</span><span> </span><span>=</span><span> </span><span>&quot;dataset_fem.RData&quot;</span><span>)</span><span>
</span><span>save</span><span>(</span><span>dataset_male</span><span>,</span><span> </span><span>file</span><span> </span><span>=</span><span> </span><span>&quot;dataset_male.RData&quot;</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code><span>length</span><span>(</span><span>unique</span><span>(</span><span>dataset_fem</span><span>$</span><span>Indicator.Name</span><span>))</span><span> </span><span>==</span><span> </span><span>length</span><span>(</span><span>unique</span><span>(</span><span>dataset_male</span><span>$</span><span>Indicator.Name</span><span>))</span><span>
</span><span>for</span><span> </span><span>(</span><span>n</span><span> </span><span>in</span><span> </span><span>1</span><span>:</span><span>length</span><span>(</span><span>unique</span><span>(</span><span>dataset_fem</span><span>$</span><span>Indicator.Name</span><span>)))</span><span> </span><span>{</span><span>
</span><span>code</span><span> </span><span>&lt;-</span><span> </span><span>unique</span><span>(</span><span>dataset_fem</span><span>$</span><span>Indicator.Name</span><span>)[</span><span>n</span><span>]</span><span>
</span><span>print</span><span>(</span><span>code</span><span>)</span><span>
</span><span>fem</span><span> </span><span>&lt;-</span><span> </span><span>dataset_fem</span><span>[</span><span>which</span><span>(</span><span>dataset_fem</span><span>$</span><span>Indicator.Name</span><span> </span><span>==</span><span> </span><span>code</span><span>),</span><span> </span><span>]</span><span>
</span><span>male</span><span> </span><span>&lt;-</span><span> </span><span>dataset_male</span><span>[</span><span>which</span><span>(</span><span>dataset_male</span><span>$</span><span>Indicator.Name</span><span> </span><span>==</span><span> </span><span>code</span><span>),</span><span> </span><span>]</span><span>
</span><span>for</span><span> </span><span>(</span><span>i</span><span> </span><span>in</span><span> </span><span>1</span><span>:</span><span>nrow</span><span>(</span><span>fem</span><span>))</span><span> </span><span>{</span><span>
</span><span>if</span><span> </span><span>(</span><span>i</span><span> </span><span>==</span><span> </span><span>1</span><span>)</span><span> </span><span>{</span><span>
</span><span>diff</span><span> </span><span>&lt;-</span><span> </span><span>male</span><span>[</span><span>i</span><span>,</span><span> </span><span>5</span><span>:</span><span>61</span><span>]</span><span> </span><span>/</span><span> </span><span>fem</span><span>[</span><span>i</span><span>,</span><span> </span><span>5</span><span>:</span><span>61</span><span>]</span><span>
</span><span>diff_table</span><span> </span><span>&lt;-</span><span> </span><span>cbind</span><span>(</span><span>male</span><span>[</span><span>i</span><span>,</span><span> </span><span>c</span><span>(</span><span>1</span><span>:</span><span>4</span><span>)],</span><span> </span><span>diff</span><span>)</span><span>
</span><span>}</span><span> </span><span>else</span><span> </span><span>{</span><span>
</span><span>diff</span><span> </span><span>&lt;-</span><span> </span><span>male</span><span>[</span><span>i</span><span>,</span><span> </span><span>5</span><span>:</span><span>61</span><span>]</span><span> </span><span>/</span><span> </span><span>fem</span><span>[</span><span>i</span><span>,</span><span> </span><span>5</span><span>:</span><span>61</span><span>]</span><span>
</span><span>diff_table</span><span> </span><span>&lt;-</span><span> </span><span>rbind</span><span>(</span><span>diff_table</span><span>,</span><span> </span><span>cbind</span><span>(</span><span>male</span><span>[</span><span>i</span><span>,</span><span> </span><span>c</span><span>(</span><span>1</span><span>:</span><span>4</span><span>)],</span><span> </span><span>diff</span><span>))</span><span>
</span><span>}</span><span>
</span><span>}</span><span>
</span><span>if</span><span> </span><span>(</span><span>n</span><span> </span><span>==</span><span> </span><span>1</span><span>)</span><span> </span><span>{</span><span>
</span><span>diff_table_bind</span><span> </span><span>&lt;-</span><span> </span><span>diff_table</span><span>
</span><span>}</span><span> </span><span>else</span><span> </span><span>{</span><span>
</span><span>diff_table_bind</span><span> </span><span>&lt;-</span><span> </span><span>rbind</span><span>(</span><span>diff_table_bind</span><span>,</span><span> </span><span>diff_table</span><span>)</span><span>
</span><span>}</span><span>
</span><span>}</span><span>
</span><span>diff_table_bind</span><span>$</span><span>Country.Code</span><span> </span><span>&lt;-</span><span> </span><span>as.character</span><span>(</span><span>diff_table_bind</span><span>$</span><span>Country.Code</span><span>)</span><span>
</span><span>diff_table_bind</span><span>[</span><span>diff_table_bind</span><span> </span><span>==</span><span> </span><span>&quot;NaN&quot;</span><span>]</span><span> </span><span>&lt;-</span><span> </span><span>NA</span><span>
</span><span>save</span><span>(</span><span>diff_table_bind</span><span>,</span><span> </span><span>file</span><span> </span><span>=</span><span> </span><span>&quot;diff_table_bind.RData&quot;</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code><span>measures</span><span> </span><span>&lt;-</span><span> </span><span>unique</span><span>(</span><span>diff_table_bind</span><span>$</span><span>Indicator.Name</span><span>)</span><span>
</span><span>save</span><span>(</span><span>measures</span><span>,</span><span> </span><span>file</span><span> </span><span>=</span><span> </span><span>&quot;measures.RData&quot;</span><span>)</span><span>
</span><span>years</span><span> </span><span>&lt;-</span><span> </span><span>gsub</span><span>(</span><span>&quot;X&quot;</span><span>,</span><span> </span><span>&quot;&quot;</span><span>,</span><span> </span><span>colnames</span><span>(</span><span>diff_table_bind</span><span>)[</span><span>-</span><span>c</span><span>(</span><span>1</span><span>:</span><span>4</span><span>)])</span><span>
</span><span>years</span><span> </span><span>&lt;-</span><span> </span><span>years</span><span>[</span><span>-</span><span>length</span><span>(</span><span>years</span><span>)]</span><span>
</span><span>save</span><span>(</span><span>years</span><span>,</span><span> </span><span>file</span><span> </span><span>=</span><span> </span><span>&quot;years.RData&quot;</span><span>)</span><span>
</span></code></pre>
</div>
<h2>Map</h2>
<div>
<pre><code><span>library</span><span>(</span><span>plyr</span><span>)</span><span>
</span><span>library</span><span>(</span><span>rgdal</span><span>)</span><span>
</span><span>wmap_countries</span><span> </span><span>&lt;-</span><span> </span><span>readOGR</span><span>(</span><span>dsn</span><span>=</span><span>&quot;shapefiles&quot;</span><span>,</span><span> </span><span>layer</span><span>=</span><span>&quot;ne_110m_admin_0_countries&quot;</span><span>)</span><span>
</span><span>wmap_countries_df</span><span> </span><span>&lt;-</span><span> </span><span>fortify</span><span>(</span><span>wmap_countries</span><span>)</span><span>
</span><span>wmap_countries</span><span>@</span><span>data</span><span>$</span><span>id</span><span> </span><span>&lt;-</span><span> </span><span>rownames</span><span>(</span><span>wmap_countries</span><span>@</span><span>data</span><span>)</span><span>
</span><span>wmap_countries_df_final</span><span> </span><span>&lt;-</span><span> </span><span>join</span><span>(</span><span>wmap_countries_df</span><span>,</span><span> </span><span>wmap_countries</span><span>@</span><span>data</span><span>,</span><span> </span><span>by</span><span> </span><span>=</span><span> </span><span>&quot;id&quot;</span><span>)</span><span>
</span><span>wmap_countries_df_final</span><span>$</span><span>gu_a3</span><span> </span><span>&lt;-</span><span> </span><span>as.character</span><span>(</span><span>wmap_countries_df_final</span><span>$</span><span>gu_a3</span><span>)</span><span>
</span><span>save</span><span>(</span><span>wmap_countries_df_final</span><span>,</span><span> </span><span>file</span><span> </span><span>=</span><span> </span><span>&quot;wmap_countries_df_final.RData&quot;</span><span>)</span><span>
</span></code></pre>
</div>
<hr>
<p></p>
<div>
<pre><code>## R version 3.3.2 (2016-10-31)
## Platform: x86_64-apple-darwin13.4.0 (64-bit)
## Running under: macOS Sierra 10.12.1
## 
## locale:
## [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## loaded via a namespace (and not attached):
##  [1] backports_1.0.4 magrittr_1.5    rprojroot_1.1   tools_3.3.2    
##  [5] htmltools_0.3.5 yaml_2.1.14     Rcpp_0.12.8     stringi_1.1.2  
##  [9] rmarkdown_1.3   knitr_1.15.1    stringr_1.1.0   digest_0.6.11  
## [13] evaluate_0.10
</code></pre>
</div>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://shiring.github.io/shiny/2017/01/29/WGS"> Shirin's playgRound</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/Avljwq9vdxI"></a></div>