<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/working-with-spss-data-in-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://pachamaltese.github.io/2017-06-24_working_with_spss_data_in_r.html"> Pachá (Batteries Included)</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<h1>Introduction</h1>
<p>I was in need of importing SPSS© data for work. There are some options but I’ve used both <code>foreign</code> and <code>haven</code> R packages. I prefer <code>haven</code> because it integrates better with R’s tidyverse and started using it in detriment of <code>foreign</code> when I verified it behaves well with factors and solves the deprecated factors labels in newer R versions.</p>
<h1>The Data</h1>
<p>For this post I found <a rel="nofollow" href="http://encuesta.udp.cl/banco-de-datos/" target="_blank">Diego Portales University National Survey</a>. It consist in a publicly available survey applied since 2005 and applied at nation-wide level to ask people about their trust in institutions (e.g. government, police, firefighters, etc) and what its their option on same-sex marriage, restricting spaces to smoke, and more.</p>
<h1>Importing Data</h1>
<div>
<pre><span></span><span>#devtools::install_github(&quot;ropenscilabs/skimr&quot;)</span>

<span># Exploratory Data Analysis tools</span>
<span>library</span><span>(</span>ggplot2<span>)</span>
<span>library</span><span>(</span>dplyr<span>)</span>
<span>library</span><span>(</span>sjlabelled<span>)</span>
<span>library</span><span>(</span>skimr<span>)</span>
<span>library</span><span>(</span>readr<span>)</span>

<span># Import foreign statistical formats</span>
<span>library</span><span>(</span>haven<span>)</span>

<span># Data</span>
url <span>=</span> <span>&quot;http://encuesta.udp.cl/descargas/banco%20de%20datos/2015/Encuesta%20Nacional%20UDP%202015.sav&quot;</span>
sav <span>=</span> <span>&quot;2017-06-24_working_with_spss_data_in_r/udp_national_survey_2015.sav&quot;</span>

<span>if</span><span>(</span><span>!</span><span>file.exists</span><span>(</span>sav<span>)){</span>download.file<span>(</span><span>url</span><span>,</span>sav<span>)}</span>

survey <span>=</span> read_sav<span>(</span>sav<span>)</span>
</pre>
</div>
<h1>Exploring data</h1>
<p>To explore the data consider the survey is in spanish. So, “fecha” means date, “edad” means age, and sexo means “sex”.</p>
<div>
<pre><span></span><span># How many surveys do I have by day?</span>
daily <span>=</span> survey <span>%>%</span>
  mutate<span>(</span>Fecha <span>=</span> <span>as.Date</span><span>(</span>Fecha<span>,</span> <span>&quot;%d-%m-%Y&quot;</span><span>))</span> <span>%>%</span>
  rename<span>(</span>date <span>=</span> Fecha<span>)</span> <span>%>%</span> 
  group_by<span>(</span><span>date</span><span>)</span> <span>%>%</span>
  summarise<span>(</span>n <span>=</span> n<span>())</span>

ggplot<span>(</span>daily<span>,</span> aes<span>(</span><span>date</span><span>,</span> n<span>))</span> <span>+</span>
  geom_line<span>()</span>
</pre>
</div>
<p><img alt="plot of chunk exploring_1" title="plot of chunk exploring_1" src="https://i0.wp.com/pachamaltese.github.io/images/2017-06-24_working_with_spss_data_in_r/exploring_1-1.png?w=456" data-recalc-dims="1"></p>
<div>
<pre><span></span><span># How is the age distributed?</span>
<span>summary</span><span>(</span>survey<span>$</span>Edad_Entrevistado<span>)</span>
</pre>
</div>
<div>
<pre><span></span>   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  18.00   32.00   48.00   47.92   61.00   89.00 
</pre>
</div>
<div>
<pre><span></span>age <span>=</span> survey <span>%>%</span>
  mutate<span>(</span><span>as.integer</span><span>(</span>Edad_Entrevistado<span>))</span> <span>%>%</span> 
  rename<span>(</span>age <span>=</span> Edad_Entrevistado<span>)</span> <span>%>%</span> 
  group_by<span>(</span>age<span>)</span> <span>%>%</span>
  summarise<span>(</span>n <span>=</span> n<span>())</span>

ggplot<span>(</span>age<span>,</span> aes<span>(</span>age<span>,</span> n<span>))</span> <span>+</span>
  geom_line<span>()</span>
</pre>
</div>
<p><img alt="plot of chunk exploring_1" title="plot of chunk exploring_1" src="https://i1.wp.com/pachamaltese.github.io/images/2017-06-24_working_with_spss_data_in_r/exploring_1-2.png?w=456" data-recalc-dims="1"></p>
<div>
<pre><span></span><span># How is the sex distributed?</span>
survey <span>%>%</span>
  rename<span>(</span>sex_id <span>=</span> Sexo_Entrevistado<span>)</span> <span>%>%</span> 
  group_by<span>(</span>sex_id<span>)</span> <span>%>%</span>
  summarise<span>(</span>n <span>=</span> n<span>())</span>
</pre>
</div>
<div>
<pre><span></span># A tibble: 2 x 2
     sex_id     n
  &lt;dbl+lbl> </pre></div>