fix duplicated levels in factors after use of read_dta, haven package
<p>I am using survey data on farmers in Nigeria from the World Bank (LSMS-ISA), available on the web. The data is in .dta and I am importing using the haven package.</p>

<p>When I import the file, I am getting the error message that "duplicated levels in factors are deprecated". I understand the problem and I am able to fix it (see below) but my solution seems very inefficient. I would appreciate it if somebody could come up with a better solution as I will probably need to fix other data sets as well.</p>

<p>This is what I tried: </p>

<p>The data file: 
<a href="https://www.dropbox.com/s/gkuw01vlr68j2sw/secta1_harvestw1.dta?dl=0" rel="nofollow">https://www.dropbox.com/s/gkuw01vlr68j2sw/secta1_harvestw1.dta?dl=0</a></p>

<p>This gives the error</p>

<pre><code>loc &lt;-  read_dta(file.path(dataPath, "/secta1_harvestw1.dta")) %&gt;%
transmute(state = as_factor(state), region_lsms = as_factor(lga))
</code></pre>

<p>My solution:</p>

<pre><code>loc &lt;-  read_dta(file.path(dataPath, "secta1_harvestw1.dta")) %&gt;%
      transmute(state = as_factor(state), lga)

# Create label-number table
lga = attr(loc$lga, 'labels')
name_lga &lt;- names(lga)
link &lt;- data.frame(lga, name_lga)
count &lt;- as.data.frame(table(name_lga))
# Six label names with multiple labels in lga
loc2 &lt;- left_join(loc, link)
loc2$region_lsms &lt;- factor(loc2$name_lga)
</code></pre>

<p>Many Thanks!</p>