title: How do I create a loop to change the text encoding of the labels in labelled variables in R
body: <p>I have imported a stata file that is giving me some encoding problems in the value labels. On import, using <code>labelled::lookfor</code> for any keyword returns this error:</p>
  <pre><code>Error in structure(as.character(x), names = names(x)) : 
    invalid multibyte string at '&lt;e9&gt;bec Solidaire'
  </code></pre>
  <p>Knowing the data-set, that is almost certainly a value label in there.</p>
  <p>How to I loop through the data-set fixing the encoding problem in the names of the value labels and then reset them. I have found a solution, I think, to fix the problematic characters, but I don't know how to replace the original names.</p>
  <pre><code>v &lt;- labelled(c(1,2,2,2,3,9,1,3,2,NA), c(yes = 1, &quot;Bloc Qu\xe9b\xe9cois&quot; = 3, &quot;don't know&quot; = 9))
  x&lt;- labelled(c(1,2,2,2,3,9,1,3,2,NA), c(&quot;Bloc Qu\xe9b\xe9cois&quot; = 1, no = 3, &quot;don't know&quot; = 9))
  
  mydat&lt;-data.frame(v=v, x=x)
  
  glimpse(mydat)
  mydat %&gt;% 
    map(., val_labels)
  #This works individually
  iconv(names(val_labels(x)), from=&quot;latin1&quot;, to=&quot;UTF-8&quot;)
  #And this seems to work looping over each variable, but how to I store it?
  mydat %&gt;% 
    map(., function(x) iconv(names(val_labels(x)), from=&quot;latin1&quot;, to=&quot;UTF-8&quot;))
  </code></pre>