<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 href="http://encuesta.udp.cl/banco-de-datos/">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, &quot;fecha&quot; means date, &quot;edad&quot; means age, and sexo means &quot;sex&quot;.</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></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></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> &lt;int>
1         1   651
2         2   651
</pre></div>
<h1>Exploring labels</h1>
<p>In the last tibble we have no idea what is 1 and 2.</p>
<div><pre><span></span>survey <span>%>%</span>
  select<span>(</span>Sexo_Entrevistado<span>)</span> <span>%>%</span> 
  rename<span>(</span>sex_id <span>=</span> Sexo_Entrevistado<span>)</span> <span>%>%</span> 
  distinct<span>()</span> <span>%>%</span> 
  mutate<span>(</span>sex <span>=</span> as_factor<span>(</span>sex_id<span>))</span>
</pre></div>
<div><pre><span></span># A tibble: 2 x 2
     sex_id    sex
  &lt;dbl+lbl> &lt;fctr>
1         2  Mujer
2         1 Hombre
</pre></div>
<p>The last column (in spanish) shows us that in this survey &quot;1 = Male&quot; and &quot;2 = Female&quot;.</p>
<p>I could run</p>
<div><pre><span></span>survey <span>%>%</span>
  rename<span>(</span>sex <span>=</span> Sexo_Entrevistado<span>)</span> <span>%>%</span> 
  mutate<span>(</span>sex <span>=</span> <span>as.integer</span><span>(</span>sex<span>))</span> <span>%>%</span> 
  mutate<span>(</span>sex <span>=</span> recode<span>(</span>sex<span>,</span> <span>`1`</span> <span>=</span> <span>&quot;Male&quot;</span><span>,</span> <span>`2`</span> <span>=</span> <span>&quot;Female&quot;</span><span>))</span> <span>%>%</span> 
  group_by<span>(</span>sex<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     n
   &lt;chr> &lt;int>
1 Female   651
2   Male   651
</pre></div>
<p>The column names are labelled as well. Here <code>sjlabelled</code> helps if I want to know for example what &quot;P12&quot; means. But instead of just translating labels I'll describe the complete dataset.</p>
<h1>Describing the dataset</h1>
<div><pre><span></span>valid_replies <span>=</span> survey <span>%>%</span> 
  mutate_if<span>(</span>is.labelled<span>,</span><span>as.numeric</span><span>)</span> <span>%>%</span> 
  skim<span>()</span> <span>%>%</span>
  filter<span>(</span>stat<span>==</span><span>&quot;complete&quot;</span><span>)</span> <span>%>%</span> 
  mutate<span>(</span>description <span>=</span> get_label<span>(</span>survey<span>))</span> <span>%>%</span> 
  select<span>(</span>var<span>,</span>description<span>,</span>everything<span>())</span> <span>%>%</span> 
  select<span>(</span><span>-</span><span>c</span><span>(</span>stat<span>,</span>level<span>,</span>type<span>))</span> <span>%>%</span> 
  rename<span>(</span>pcent_valid <span>=</span> value<span>)</span> <span>%>%</span> 
  mutate<span>(</span>pcent_valid <span>=</span> <span>paste0</span><span>(</span><span>100</span><span>*</span><span>round</span><span>(</span>pcent_valid <span>/</span> <span>nrow</span><span>(</span>survey<span>),</span><span>2</span><span>),</span><span>'%'</span><span>))</span>

histograms <span>=</span> survey <span>%>%</span> 
  mutate_if<span>(</span>is.labelled<span>,</span><span>as.numeric</span><span>)</span> <span>%>%</span> 
  skim<span>()</span> <span>%>%</span>
  filter<span>(</span>stat<span>==</span><span>&quot;hist&quot;</span><span>)</span> <span>%>%</span> 
  select<span>(</span>var<span>,</span>level<span>)</span> <span>%>%</span> 
  rename<span>(</span>histogram <span>=</span> level<span>)</span>

survey_description <span>=</span> valid_replies <span>%>%</span> 
  left_join<span>(</span>histograms<span>)</span> <span>%>%</span> 
  write_csv<span>(</span><span>&quot;2017-06-24_working_with_spss_data_in_r/survey_description.csv&quot;</span><span>)</span>

survey_description
</pre></div>
<div><pre><span></span># A tibble: 203 x 4
                 var          description pcent_valid  histogram
               &lt;chr>                &lt;chr>       &lt;chr>      &lt;chr>
 1        PONDERADOR           Ponderador        100% ▂▇▇▅▅▃▁▁▁▁
 2             Folio                Folio        100% ▇▇▇▇▇▇▇▇▇▇
 3            Región               Región        100% ▁▁▂▁▂▁▁▁▇▁
 4            Comuna               Comuna        100% ▁▁▂▁▁▂▁▁▇▁
 5             Fecha     Fecha entrevista        100%       &lt;NA>
 6  Sexo_Encuestador   Sexo Entrevistador         91% ▂▁▁▁▁▁▁▁▁▇
 7               GSE           GSE Visual        100% ▁▁▂▁▇▁▁▆▁▁
 8 Sexo_Entrevistado    Sexo Entrevistado        100% ▇▁▁▁▁▁▁▁▁▇
 9 Edad_Entrevistado    Edad Entrevistado        100% ▇▆▅▆▇▇▅▃▃▂
10       Hora_Inicio Hora Inicio Medición        100%       &lt;NA>
# ... with 193 more rows
</pre></div>
<p>Exploring the last tibble there are interesting questions. For example, P12 refers to <i>&quot;Apoyo a la democracia&quot;</i> that is Do you support democracy?.</p>