<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/how-to-read-stata-dta-files-into-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://www.displayr.com/how-to-read-stata-dta-files-into-r/?utm_medium=Feed&utm_source=Syndication"> R – Displayr</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>The <a rel="nofollow" href="http://www.britishelectionstudy.com/wp-content/uploads/2018/01/bes_f2f_2017_v1.2.dta" target="_blank">file</a> contains 2017 face-to-face post-election survey responses along with <a rel="nofollow" href="http://www.britishelectionstudy.com/wp-content/uploads/2018/01/GfK-BES2017-technical-report.pdf" target="_blank">explanatory notes</a>. Read the Stata DTA file into R with two these two lines:</p>
<pre> library(haven)
df &amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;lt;- read_dta(&quot;http://www.britishelectionstudy.com/wp-content/uploads/2018/01/bes_f2f_2017_v1.2.dta&quot;)
</pre>
<p>The data set is now stored as a dataframe df with 357 variables. To check the properties of the data set we type</p>
<pre>str(df)</pre>
<p>This gives the following output:</p>
<pre>Classes ‘tbl_df’, ‘tbl’ and 'data.frame':       2194 obs. of  357 variables:
$ finalserialno         : atomic  10115 10119 10125 10215 10216 ...
..- attr(*, &quot;label&quot;)= chr &quot;Final Serial Number&quot;
..- attr(*, &quot;format.stata&quot;)= chr &quot;%12.0g&quot;
$ serial                : atomic  000000399 000000398 000000400 000000347 ...
..- attr(*, &quot;label&quot;)= chr &quot;Respondent Serial Number&quot;
..- attr(*, &quot;format.stata&quot;)= chr &quot;%9s&quot;
$ a01                   : atomic  nhs brexit society immigration ...
..- attr(*, &quot;label&quot;)= chr &quot;A1: Most important issue&quot;
..- attr(*, &quot;format.stata&quot;)= chr &quot;%240s&quot;
$ a02                   :Class 'labelled'  atomic [1:2194] 1 0 -1 -1 1 -1 2 -1 2 2 ...
.. ..- attr(*, &quot;label&quot;)= chr &quot;Best party on most important issue&quot;
.. ..- attr(*, &quot;format.stata&quot;)= chr &quot;%8.0g&quot;
.. ..- attr(*, &quot;labels&quot;)= Named num [1:13] NA NA NA 0 1 2 3 4 5 6 ...
.. .. ..- attr(*, &quot;names&quot;)= chr [1:13] &quot;Not stated&quot; &quot;Refused&quot; &quot;Don`t know&quot; &quot;None/No party&quot; ...</pre>
<p>The above output shows that the variables are already set to the correct types. The first variable <span>finalserialno</span> is numeric (i.e., atomic), the third variable <span>a01</span> is character, and the fourth variable <span>a02</span> has a class of ‘labelled’ which can be converted to a factor or categorical variable (after we handle missing values).<br>
Each variable has an associated label attribute to help with interpretation. For example, without having to look up the explanatory notes, we can see that variable <span>a01</span> contains the responses to the question “A1: most important issue” and variable <span>a02</span> contains the responses to “Best party on most important issue”.</p>
<h2>Missing values</h2>
<p>Stata supports multiple types of missing values.  Read_dta automatically handles missing values in numeric and character variables. For categorical variables, missing values are typically encoded by negative numbers. Section 5.3 of the <a rel="nofollow" href="http://www.britishelectionstudy.com/wp-content/uploads/2018/01/GfK-BES2017-technical-report.pdf" target="_blank">explanatory notes</a> describes the encoding for this file: -1 (Don’t know), -2 (Refused) and -999 (Not stated). We now convert all three of these values to <span>NA</span>.</p>
<pre>for (i in 1:length(df))
{
    if (class(df[[i]] == &quot;labelled&quot;)
        df[[i]][df[[i]] &amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;lt; 0] &amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;lt;- NA
}</pre>
<h2>Encoding categorical variables</h2>
<p>The categorical variables of class “labelled” are stored as numeric vectors. Convert them into factors so they are correctly associated with the labels with only a single command:</p>
<pre> df &amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;lt;- as_factor(df)</pre>
<p>Note that we do this after converting the missing values to avoid spurious factor levels in the final dataset.</p>
<h2>Find out more</h2>
<p>You can find out about how to <a rel="nofollow" href="https://www.displayr.com/how-to-upload-read-excel-file-displayr/" target="_blank">import and read Excel files into Displayr</a> as well.</p>
	<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://www.displayr.com/how-to-read-stata-dta-files-into-r/?utm_medium=Feed&utm_source=Syndication"> R – Displayr</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 rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/f1iqBl7jV7Y" height="1"></a></div>