title: R: Change character encoding of columns in data frame
body: <p>I'm investigating how the character encoding affects sorting. My question here is:</p>
  
  <p><strong>How I can change a single column of a data frame to a different character encoding?</strong></p>
  
  <p>For context, I will include several extra steps at the bottom.</p>
  
  <p>1) Create the data frame:</p>
  
  <pre><code>d.enc &lt;- data.frame( utf8 = c(" ", "_ ", " _"), 
                       mac = c(" ", "_ ", " _"), 
                       label = c("space", "underscore space", "space underscore") )
  </code></pre>
  
  <p>2) Convert to character vectors and attempt to set encoding:</p>
  
  <pre><code>d.enc2$utf8 &lt;- as.character(d.enc$utf8)
  d.enc2$mac &lt;- as.character(d.enc$mac)
  d.enc2$label &lt;- as.character(d.enc$label)
  
  Encoding(d.enc2$utf8) &lt;- "UTF-8"
  Encoding(d.enc2$mac) &lt;- "MACINTOSH"
  Encoding(d.enc2$utf8)
  # [1] "unknown" "unknown" "unknown"
  Encoding(d.enc2$mac)
  # [1] "unknown" "unknown" "unknown"
  </code></pre>
  
  <p>3) That's not what I was hoping for. I would have expected:</p>
  
  <pre><code># [1] "UTF-8" "UTF-8" "UTF-8" and
  # [1] "MACINTOSH" "MACINTOSH" "MACINTOSH"
  </code></pre>
  
  <p>4) Are my desired encodings supported? (Running on a mac)</p>
  
  <pre><code>temp &lt;- iconvlist()
  temp[399]
  # [1] "UTF-8"
  temp[338]
  # [1] "MACINTOSH"
  </code></pre>
  
  <p>Seems that they are supported.</p>
  
  <p>5) Once I can change the encodings, I would like to do the following to see how the sorting order changes:</p>
  
  <pre><code>library(dplyr)
  arrange(d.enc2, desc(utf8))
  arrange(d.enc2, desc(mac))
  </code></pre>
  
  <p>6) I expect the output will look something like this but in a different order depending on which column is used for the sorting:</p>
  
  <pre><code>  utf8 mac            label
  1   _   _  underscore space
  2    _   _ space underscore
  3                     space
  </code></pre>
  
  <p>Thanks for any tips!</p>