<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/exploring-european-attitudes-and-behaviours-using-the-european-social-survey/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://ropensci.org/blog/2018/06/14/essurvey/"> rOpenSci - open tools for open science</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p></p>
<h2>Introduction</h2>
<p>I never thought that I’d be programming software in my career. I started<br>
using R a little over 2 years now and it’s been one of the most important<br>
decisions in my career. Secluded in a small academic office with no one<br>
to discuss/interact about my new hobby, I started searching the web for<br>
tutorials and packages. After getting to know how amazing and nurturing<br>
the R community is, it made me want to become a data scientist. So I set<br>
out to do it. Throughout the journey I repeatedly found myself using<br>
the <a rel="nofollow" href="http://www.europeansocialsurvey.org/" target="_blank">European Social Survey</a> (ESS from now on), a<br>
really neat dataset that collects information on attitudes, beliefs and<br>
behaviour patterns of diverse populations in more than thirty European<br>
nations since 2002.</p>
<p>After seeing a niche in the R package community, I created the package<br>
<code>essurvey</code> (previously <code>ess</code> in CRAN) to access this data easily from R.<br>
The package was accepted in CRAN in September 2017 and was well received<br>
among social scientists.</p>
<p>The 4th of March of 2018 I submitted the package to rOpensci, intimidated but<br>
very excited about the peer review process. To my surprise, the process<br>
was enriching, respectful and transparent, unlike my previous experience<br>
in academic research.</p>
<h2>First steps</h2>
<p>Using the <code>essurvey</code> package is fairly easy. There are are two main<br>
families of functions: <code>import_*</code> and <code>show_*</code>. They complement<br>
each other and allow the user to almost never have to go to the ESS website.<br>
The only scenario where you need to enter is to register your new account. If you haven’t registered,<br>
create an account at <a rel="nofollow" href="http://www.europeansocialsurvey.org/user/new" target="_blank">http://www.europeansocialsurvey.org/user/new</a> and<br>
validate through your email inbox.</p>
<p>You can install the development version with</p>
<pre><code># install.packages(&quot;devtools&quot;)
devtools::install_github(&quot;ropensci/essurvey&quot;)
</code></pre>
<p>or the stable version from CRAN with</p>
<pre><code>install.packages(&quot;essurvey&quot;)
</code></pre>
<p>Let’s load the CRAN version and load it together with the <code>tidyverse</code><br>
set of packages to do some data manipulation.</p>
<pre><code>library(essurvey)
library(tidyverse)
</code></pre>
<p>Given that some <code>essurvey</code> functions require your email address, this<br>
vignette will use a fake email but everything should work accordingly if<br>
you registered with the ESS. We can set our email as an environment<br>
variable with <code>set_email</code></p>
<pre><code>set_email(&quot;your@email.com&quot;)
</code></pre>
<p>Before we continue, let’s briefly explain some terminology<br>
of the ESS data. Surveys are carried out every 2 years<br>
and each survey is called rounds or waves. For example,<br>
round one was the first round ever implemented, which dates<br>
back to 2002. The second round followed up in 2004 and it’s<br>
usually referred to as second round or second wave. There<br>
are currently eight rounds freely available.</p>
<p>The ESS has over a thousand questions that include interesting<br>
topics such as attitudes towards national governments, democracy,<br>
immigration, nationalism, public policy as well demographic<br>
and subjective health data on the participants of the survey.<br>
Most of these questions use likert-type <em>scales</em> which means that<br>
the possible answers to any given question range either from 0 through 5<br>
or 0 through 10. For example, an average question would be something like:<br>
how satisfied are you with democracy in your country? and the possible<br>
answers range from 0 to 10 where 0 means very unsatisfied and 10 very satisfied.</p>
<h2>Exploring government satisfacion in Spain</h2>
<p>Let’s suppose you don’t know which countries or rounds (waves) are<br>
available for the ESS. Then the <code>show_*</code> family of functions is your<br>
friend.</p>
<p>To find out which countries have participated you can use<br>
<code>show_countries()</code></p>
<pre><code>show_countries()

##  [1] &quot;Albania&quot;            &quot;Austria&quot;            &quot;Belgium&quot;           
##  [4] &quot;Bulgaria&quot;           &quot;Croatia&quot;            &quot;Cyprus&quot;            
##  [7] &quot;Czech Republic&quot;     &quot;Denmark&quot;            &quot;Estonia&quot;           
## [10] &quot;Finland&quot;            &quot;France&quot;             &quot;Germany&quot;           
## [13] &quot;Greece&quot;             &quot;Hungary&quot;            &quot;Iceland&quot;           
## [16] &quot;Ireland&quot;            &quot;Israel&quot;             &quot;Italy&quot;             
## [19] &quot;Kosovo&quot;             &quot;Latvia&quot;             &quot;Lithuania&quot;         
## [22] &quot;Luxembourg&quot;         &quot;Netherlands&quot;        &quot;Norway&quot;            
## [25] &quot;Poland&quot;             &quot;Portugal&quot;           &quot;Romania&quot;           
## [28] &quot;Russian Federation&quot; &quot;Slovakia&quot;           &quot;Slovenia&quot;          
## [31] &quot;Spain&quot;              &quot;Sweden&quot;             &quot;Switzerland&quot;       
## [34] &quot;Turkey&quot;             &quot;Ukraine&quot;            &quot;United Kingdom&quot;
</code></pre>
<p>This function actually looks up the countries in the ESS website. If new<br>
countries enter, this will automatically grab those countries as well.<br>
Let’s check out Spain. How many rounds has Spain participated in? We can<br>
use <code>show_country_rounds()</code></p>
<pre><code>sp_rnds &lt;- show_country_rounds(&quot;Spain&quot;)
sp_rnds

## [1] 1 2 3 4 5 6 7
</code></pre>
<p>Note that country names are case sensitive. Use the exact name printed<br>
out by <code>show_countries()</code>.</p>
<p>Using this information, we can download those specific rounds easily<br>
with <code>import_country</code>.</p>
<pre><code>spain &lt;-
  import_country(
    country = &quot;Spain&quot;,
    rounds = 1:7
    )
</code></pre>
<p>Note: Since <code>essurvey 1.0.0</code> all <code>ess_*</code> functions have been deprecated<br>
in favour of the <code>import_*</code> and <code>download_*</code> functions.</p>
<p><code>spain</code> will now be a list of <code>length(rounds)</code> containing a data frame<br>
for each round. The <code>import_*</code> family is concerned with downloading the<br>
data and thus always returns a list containing data frames unless only<br>
one round is specified, in which it returns a <code>tibble</code>. Conversely, the<br>
<code>show_*</code> family grabs information from the ESS website and always<br>
returns vectors.</p>
<p>To download all rounds for a country automatically you can use<br>
<code>import_all_cntrounds</code>.</p>
<pre><code>import_all_cntrounds(&quot;Spain&quot;)
</code></pre>
<p>ESS datasets flag missing values differently between questions.<br>
For example, questions with possible answers ranging from <code>0</code> through <code>5</code> have missing categories<br>
such as “Don’t know” and “Refusal” coded as <code>7</code> and <code>8</code> and <code>9</code>. But for questions with possible answers ranging from<br>
<code>0</code> through <code>10</code> missing values are coded as <code>77</code>, <code>88</code> and <code>99</code>. <code>recode_missings</code> accepts a<br>
<code>tibble</code> as a main argument and automatically returns a new <code>tibble</code><br>
with all missing values recoded as <code>NA</code>. You should check out<br>
<code>?recode_missings</code> for more details and elaborated examples.</p>
<p><strong>Note</strong>: I urge the reader not to recode these categories to missing<br>
without previously investigating the importance of these categories.</p>
<p>For example, let’s recode missing values in all Spanish waves, bind them<br>
into one single <code>tibble</code> and visualize how satisfied are Spaniards with<br>
their government. First, let’s extract a cleaner data.</p>
<pre><code>semi_cleaned &lt;-
  spain %>% 
  map(recode_missings) %>% 
  bind_rows() %>% 
  mutate(name = str_sub(name, end = 4)) %>% 
  select(name, stfgov)

semi_cleaned

## # A tibble: 13,543 x 2
##    name  stfgov
##    </code></pre>