<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="http://www.r-bloggers.com/analyzing-mexican-votes-in-the-u-n/"></div></div><div><a data-url="http://www.r-bloggers.com/analyzing-mexican-votes-in-the-u-n/" data-counturl="http://www.r-bloggers.com/analyzing-mexican-votes-in-the-u-n/" data-count="horizontal" data-text="Analyzing Mexican votes in the U.N." class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.enelmargen.org/datascience/un-voting-patterns"> En El Margen - R-English</a></strong>, and kindly contributed to <a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Last week I discovered the <a target="_blank" href="https://github.com/dgrtwo/unvotes">unvotes</a> package on github so I thought i’d do some number-crunching to see if I find anything interesting.</p>
<p>The package provides the voting history of countries in the United Nations General Assembly, along with information such as date, description, and topics for each vote.</p>
<p>The author makes it clear to reference the original publication of the data:</p>
<blockquote>
<p>Erik Voeten “Data and Analyses of Voting in the UN General Assembly” Routledge Handbook of International Organization, edited by Bob Reinalda (published May 27, 2013)</p>
</blockquote>
<p>I installed from CRAN and created a small function that counts the amount of times two countries “agree”, that is they vote the same way in a given resolution.</p>
<div>
<pre><code data-lang="r"><span>library</span><span>(</span>dplyr<span>)</span>
<span>library</span><span>(</span>magrittr<span>)</span>
<span>library</span><span>(</span>unvotes<span>)</span>

<span># the database of all votes </span>
votes <span>&lt;-</span> un_votes <span>%>%</span>
  left_join<span>(</span><span>.</span><span>,</span> un_roll_calls<span>)</span> <span>%>%</span>
  left_join<span>(</span><span>.</span><span>,</span> un_roll_call_issues<span>)</span>

<span># a function to count the amount of times any given countries &quot;agree&quot;</span>
un_agree <span>&lt;-</span> <span>function</span><span>(</span>db<span>,</span> <span># the database</span>
                     country1<span>,</span> 
                     country2<span>,</span>
                     abstain <span>=</span> <span>TRUE</span> <span># if FALSE, do not count abstains</span>
                     <span>){</span>
  c1 <span>&lt;-</span> db <span>%>%</span> 
    filter<span>(</span>country <span>==</span> country1<span>)</span> <span>%>%</span>
    mutate<span>(</span><span>&quot;vote2&quot;</span> <span>=</span> vote<span>)</span>
  c2 <span>&lt;-</span> db <span>%>%</span> 
    filter<span>(</span>country <span>==</span> country2<span>)</span>
  j <span>&lt;-</span> c1 <span>%>%</span> select<span>(</span>vote2<span>,</span> rcid<span>)</span> <span>%>%</span>
    left_join<span>(</span><span>.</span><span>,</span> c2 <span>%>%</span> select<span>(</span>rcid<span>,</span> vote<span>),</span> by <span>=</span> <span>&quot;rcid&quot;</span><span>)</span> <span>%>%</span>
    filter<span>(</span><span>!</span><span>is.na</span><span>(</span>vote<span>))</span>
  <span>if</span><span>(</span><span>!</span>abstain<span>){</span>
  j <span>&lt;-</span> j <span>%>%</span> 
    filter<span>(</span>vote <span>!=</span> <span>&quot;abstain&quot;</span><span>)</span> <span>%>%</span>
    filter<span>(</span>vote2 <span>!=</span> <span>&quot;abstain&quot;</span><span>)</span>
  <span>}</span>
  j <span>&lt;-</span> j <span>%>%</span> 
    mutate<span>(</span><span>&quot;agree&quot;</span> <span>=</span> <span>ifelse</span><span>(</span>vote2 <span>==</span> <span>as.character</span><span>(</span>vote<span>),</span> <span>1</span><span>,</span> <span>0</span><span>))</span>
  <span>sum</span><span>(</span>j<span>$</span>agree<span>)</span><span>/</span><span>length</span><span>(</span>j<span>$</span>agree<span>)</span><span>*</span><span>100</span>
<span>}</span></code></pre>
</div>
<p>Now, it would be interesting to see how much my home country and its rowdy neighbour to the north see eye-to-eye…</p>
<div>
<pre><code data-lang="r">un_agree<span>(</span>votes<span>,</span> <span>&quot;Mexico&quot;</span><span>,</span> <span>&quot;United States&quot;</span><span>)</span>
<span># [1] 23.32385</span></code></pre>
</div>
<p>So about one-fourth of every vote. This goes up slightly when eliminating abstentions (29.7%).</p>
<p>However, this might be interesting to see by “sexenio”, or Mexican presidential term. Small caveat, the last vote in the dataset is september 9th, 2014.</p>
<div>
<pre><code data-lang="r"><span># some of the newest presidential terms...</span>
votes <span>%&lt;>%</span> mutate<span>(</span><span>&quot;mxterm&quot;</span> <span>=</span> <span>ifelse</span><span>(</span>date <span>>=</span> <span>'1988-12-01'</span> <span>&amp;</span> 
                                   date <span>&lt;=</span> <span>'1994-11-30'</span><span>,</span> <span>&quot;salinas&quot;</span><span>,</span> 
                            <span>ifelse</span><span>(</span>date <span>>=</span> <span>'1994-12-01'</span> <span>&amp;</span> 
                                   date <span>&lt;=</span> <span>'2000-11-30'</span><span>,</span> <span>&quot;zedillo&quot;</span><span>,</span>
                            <span>ifelse</span><span>(</span>date <span>>=</span> <span>'2000-12-01'</span> <span>&amp;</span> 
                                   date <span>&lt;=</span> <span>'2006-11-30'</span><span>,</span> <span>&quot;fox&quot;</span><span>,</span>
                            <span>ifelse</span><span>(</span>date <span>>=</span> <span>'2006-12-01'</span> <span>&amp;</span> 
                                   date <span>&lt;=</span> <span>'2012-11-30'</span><span>,</span> <span>&quot;calderon&quot;</span><span>,</span>
                            <span>ifelse</span><span>(</span>date <span>>=</span> <span>'2012-12-01'</span> <span>&amp;</span> 
                                   date <span>&lt;=</span> <span>'2018-11-30'</span><span>,</span> <span>&quot;penanieto&quot;</span><span>,</span> <span>&quot;others&quot;</span><span>))</span>
                                   <span>))))</span>
un_agree<span>(</span>votes <span>%>%</span> filter<span>(</span>mxterm <span>==</span><span>&quot;salinas&quot;</span><span>),</span> 
        <span>&quot;Mexico&quot;</span><span>,</span> <span>&quot;United States&quot;</span><span>)</span>
<span># [1] 10.77586</span>
un_agree<span>(</span>votes <span>%>%</span> filter<span>(</span>mxterm <span>==</span><span>&quot;zedillo&quot;</span><span>),</span> 
        <span>&quot;Mexico&quot;</span><span>,</span> <span>&quot;United States&quot;</span><span>)</span>
<span># [1] 20.76167</span>
un_agree<span>(</span>votes <span>%>%</span> filter<span>(</span>mxterm <span>==</span><span>&quot;fox&quot;</span><span>),</span> 
        <span>&quot;Mexico&quot;</span><span>,</span> <span>&quot;United States&quot;</span><span>)</span>
<span># [1] 9.129213</span>
un_agree<span>(</span>votes <span>%>%</span> filter<span>(</span>mxterm <span>==</span><span>&quot;calderon&quot;</span><span>),</span> 
        <span>&quot;Mexico&quot;</span><span>,</span> <span>&quot;United States&quot;</span><span>)</span>
<span># [1] 8.935743</span>
un_agree<span>(</span>votes <span>%>%</span> filter<span>(</span>mxterm <span>==</span><span>&quot;penanieto&quot;</span><span>),</span> 
        <span>&quot;Mexico&quot;</span><span>,</span> <span>&quot;United States&quot;</span><span>)</span>
<span># [1] 12.92517</span></code></pre>
</div>
<p>This is unexpected. It’s usually folk tale that the PAN-ista governments (Fox and especially Calderon) cooperated much more with their American counterparts than the rest. Maybe we could visualize this side by side in a graph:</p>
<div>
<pre><code data-lang="r">agree_by_year <span>&lt;-</span> votes <span>%>%</span> 
  filter<span>(</span>country <span>%in%</span> <span>c</span><span>(</span><span>&quot;Mexico&quot;</span><span>,</span> <span>&quot;United States&quot;</span><span>))</span> <span>%>%</span> 
  group_by<span>(</span>year <span>=</span> year<span>(</span><span>date</span><span>),</span> rcid<span>)</span> <span>%>%</span> 
  summarise<span>(</span><span>&quot;DifVote&quot;</span> <span>=</span> n_distinct<span>(</span>vote<span>))</span> <span>%>%</span> 
  mutate<span>(</span><span>&quot;Agree&quot;</span> <span>=</span> <span>ifelse</span><span>(</span>DifVote <span>==</span> <span>1</span><span>,</span> <span>1</span><span>,</span> <span>0</span><span>))</span> <span>%>%</span> 
  summarise<span>(</span><span>&quot;PercentageAgree&quot;</span> <span>=</span> <span>sum</span><span>(</span>Agree<span>)</span><span>/</span>n_distinct<span>(</span>rcid<span>),</span> 
            <span>&quot;VoteCount&quot;</span> <span>=</span> n_distinct<span>(</span>rcid<span>))</span>
  
g <span>&lt;-</span> ggplot<span>(</span>agree_by_year<span>,</span> 
        aes<span>(</span>x <span>=</span> year<span>,</span> 
            y <span>=</span> PercentageAgree<span>*</span><span>100</span><span>))</span><span>+</span>
  geom_line<span>(</span>colour <span>=</span> eem_colors<span>[</span><span>1</span><span>])</span><span>+</span>
  theme_eem<span>()</span><span>+</span>
  labs<span>(</span>title <span>=</span> <span>&quot;Do Mexico and U.S. agree in U.N. votes?&quot;</span><span>,</span>
       x <span>=</span> <span>&quot;Year&quot;</span><span>,</span> y <span>=</span> <span>&quot;(%)&quot;</span><span>)</span>
g</code></pre>
</div>
<p><img alt="Mexico and the US agree less on UN votes" src="http://i2.wp.com/www.enelmargen.org/images/posts/unvotes2.png?w=456" data-recalc-dims="1"></p>
<p>So the tendency has been to disagree more under these presidents. Off course, this could be due to a number of quirky data issues. Among them, the amount of different votes taken to the General Assembly (in orange)…</p>
<div>
<pre><code data-lang="r">g <span>+</span>
  geom_line<span>(</span>aes<span>(</span>x <span>=</span> year<span>,</span> y <span>=</span> VoteCount<span>),</span> colour <span>=</span> eem_colors<span>[</span><span>2</span><span>])</span></code></pre>
</div>
<p><img alt="Votes by year where Mexico and USA voted in UN" src="http://i1.wp.com/www.enelmargen.org/images/posts/unvotes3.png?w=456" data-recalc-dims="1"></p>
<p>Now, it might seem strange so much disagreement, so let’s see if this rate is small or large in the context of everyone else. I’m going to loop through every country, to see Mexico’s (historic) agreement with each one…</p>
<div>
<pre><code data-lang="r"><span># I found making a function to be easier for the rest</span>
<span># of the post... </span>
agree_world_with <span>&lt;-</span> <span>function</span><span>(</span>db<span>,</span> 
                             country_pivot<span>){</span>
v <span>&lt;-</span> <span>NULL</span>
<span>for</span><span>(</span>i <span>in</span> <span>1</span><span>:</span><span>length</span><span>(</span><span>unique</span><span>(</span>db<span>$</span>country<span>))){</span>
country <span>&lt;-</span> <span>unique</span><span>(</span>db<span>$</span>country<span>)[</span>i<span>]</span>
p <span>&lt;-</span> un_agree<span>(</span>db <span>=</span> votes<span>,</span> 
         country1 <span>=</span> country_pivot<span>,</span> 
         country2 <span>=</span> <span>unique</span><span>(</span>db<span>$</span>country<span>)[</span>i<span>])</span>
d <span>&lt;-</span> <span>data.frame</span><span>(</span><span>&quot;country&quot;</span> <span>=</span> country<span>,</span> <span>&quot;p&quot;</span> <span>=</span> p<span>)</span>
v <span>&lt;-</span> <span>rbind.data.frame</span><span>(</span>v<span>,</span> d<span>)}</span>
  <span>return</span><span>(</span>v<span>)</span>
<span>}</span>

<span># Now for the fun part... </span>
<span># which countries agree most with Mexico?</span>
agree_world_with<span>(</span>votes<span>,</span> <span>&quot;Mexico&quot;</span><span>)</span> <span>%>%</span> 
  arrange<span>(</span>desc<span>(</span>p<span>))</span> <span>%>%</span>
  filter<span>(</span>country <span>!=</span> <span>&quot;Mexico&quot;</span><span>)</span> <span>%>%</span> <span># obviously p=100</span>
  <span>head</span><span>()</span>
<span># country p</span>
<span>#1 Timor-Leste 95.82192</span>
<span>#2 Seychelles 94.46378</span>
<span>#3 Sao Tome and Principe 93.73695</span>
<span>#4 Guinea-Bissau 92.48287</span>
<span>#5 Zimbabwe 92.37548</span>
<span>#6 Suriname 92.37288</span>

<span># which countries agree less with Mexico?</span>
agree_world_with<span>(</span>votes<span>,</span> <span>&quot;Mexico&quot;</span><span>)</span> <span>%>%</span> 
  arrange<span>(</span>desc<span>(</span>p<span>))</span> <span>%>%</span>
  filter<span>(</span>country <span>!=</span> <span>&quot;Mexico&quot;</span><span>)</span> <span>%>%</span>
  <span>tail</span><span>()</span>
<span># country p</span>
<span>#194 Marshall Islands 35.27607</span>
<span>#195 Micronesia, Federated States of 27.76163</span>
<span>#196 Israel 25.96885</span>
<span>#197 United States 23.32385</span>
<span>#198 Palau 20.79327</span>
<span>#199 Zanzibar 0.00000</span></code></pre>
</div>
<p>For the record, I tried doing the same exercise for each “sexenio” and basically came up with the same number of countries on top, maybe we’re not so close to the U.S. after all?</p>
<p>What about abstentions? You would think Mexico’s historical no-intervention foreign policy would make it a cronical abstainer, but actually even the United States and France use this trick more often…</p>
<div>
<pre><code data-lang="r"><span># count abstentions by country</span>
abstentions <span>&lt;-</span> votes <span>%>%</span> 
  mutate<span>(</span><span>&quot;abstain&quot;</span> <span>=</span> <span>ifelse</span><span>(</span><span>as.character</span><span>(</span>vote<span>)</span> <span>==</span> <span>&quot;abstain&quot;</span><span>,</span> <span>1</span><span>,</span> <span>0</span><span>))</span> <span>%>%</span>
  group_by<span>(</span>country<span>)</span> <span>%>%</span> 
  summarise<span>(</span><span>&quot;prct_abstentions&quot;</span> <span>=</span> <span>sum</span><span>(</span>abstain<span>)</span><span>/</span>n_distinct<span>(</span>rcid<span>))</span> <span>%>%</span>
  arrange<span>(</span>desc<span>(</span>prct_abstentions<span>))</span>
abstentions<span>[</span><span>1</span><span>:</span><span>30</span><span>,</span> <span>]</span> <span>%>%</span> <span>as.data.frame</span>
<span># country prct_abstentions</span>
<span>#1 Zanzibar 1.0000000</span>
<span>#2 Federal Republic of Germany 0.5183636</span>
<span>#3 France 0.4210017</span>
<span>#4 Tonga 0.4038710</span>
<span>#5 South Sudan 0.3958333</span>
<span>#6 Japan 0.3929402</span>
<span>#7 Georgia 0.3845050</span>
<span>#8 Italy 0.3822297</span>
<span>#9 Micronesia, Federated States of 0.3818046</span>
<span>#10 Austria 0.3641872</span>
<span>#11 United Kingdom 0.3614412</span>
<span>#12 Korea, Republic of 0.3612663</span>
<span>#13 Belgium 0.3587247</span>
<span>#14 Luxembourg 0.3495841</span>
<span>#15 Portugal 0.3486631</span>
<span>#16 Canada 0.3462857</span>
<span>#17 Marshall Islands 0.3426431</span>
<span>#18 Netherlands 0.3395579</span>
<span>#19 Ireland 0.3381340</span>
<span>#20 Finland 0.3379368</span>
<span>#21 Australia 0.3326978</span>
<span>#22 Moldova, Republic of 0.3320053</span>
<span>#23 Sweden 0.3277905</span>
<span>#24 Israel 0.3275574</span>
<span>#25 Denmark 0.3267912</span>
<span>#26 Norway 0.3253862</span>
<span>#27 Iceland 0.3208144</span>
<span>#28 Estonia 0.3169192</span>
<span>#29 Latvia 0.3159236</span>
<span>#30 Spain 0.3145144</span>

<span># Mexico is actually at spot 122 with only 10% of its total votes!! </span>
abstentions<span>[</span><span>122</span><span>,</span> <span>]</span>
<span># 1 Mexico 0.1094109</span></code></pre>
</div>
<p>This dataset is actually really nice, we can make a small comparison of some foreign policy. For example NAFTA partners and their agreement on some issues related to Israel…</p>
<div>
<pre><code data-lang="r"><span># only the Palestinian conflict</span>
israelset <span>&lt;-</span> votes <span>%>%</span> filter<span>(</span>issue <span>==</span> <span>&quot;Palestinian conflict&quot;</span><span>)</span>
nafta <span>&lt;-</span> <span>c</span><span>(</span><span>&quot;Mexico&quot;</span><span>,</span> <span>&quot;United States&quot;</span><span>,</span> <span>&quot;Canada&quot;</span><span>)</span>
agree_with_israel <span>&lt;-</span> <span>data.frame</span><span>(</span><span>&quot;country&quot;</span> <span>=</span> nafta<span>,</span> 
                                <span>&quot;agree&quot;</span> <span>=</span> <span>c</span><span>(</span>
                                un_agree<span>(</span>israelset<span>,</span> nafta<span>[</span><span>1</span><span>],</span> <span>&quot;Israel&quot;</span><span>),</span>
                                un_agree<span>(</span>israelset<span>,</span> nafta<span>[</span><span>2</span><span>],</span> <span>&quot;Israel&quot;</span><span>),</span>
                                un_agree<span>(</span>israelset<span>,</span> nafta<span>[</span><span>3</span><span>],</span> <span>&quot;Israel&quot;</span><span>)</span>
                                <span>))</span>
agree_with_israel
<span># country agree</span>
<span>#1 Mexico 5.156724</span>
<span>#2 United States 77.620968</span>
<span>#3 Canada 25.226131</span></code></pre>
</div>
<p>I’m sure the kind readers at <strong>r-bloggers</strong> will come up with another useful analysis from this package. Maybe a map? <a target="_blank" href="https://twitter.com/eflores89">Tweet</a> me up!</p>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="http://www.r-bloggers.com/analyzing-mexican-votes-in-the-u-n/"></div></div><div><a data-url="http://www.r-bloggers.com/analyzing-mexican-votes-in-the-u-n/" data-counturl="http://www.r-bloggers.com/analyzing-mexican-votes-in-the-u-n/" data-count="vertical" data-text="Analyzing Mexican votes in the U.N." class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://www.enelmargen.org/datascience/un-voting-patterns"> En El Margen - R-English</a></strong>.</div>
<hr>
<a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="http://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="http://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="http://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="http://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="http://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="http://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="http://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="http://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="http://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="http://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="http://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="http://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="http://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="http://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="http://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="http://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/1RWOrTL6hBw"></a></div>