title: Using value-labels in R with sjlabelled package
body: <p>Recently I have switched from STATA to R. </p>
  
  <p>In STATA, you have something called value label. Using the command <code>encode</code> for example allows you to turn a string variable into a numeric, with a string label attached to each number. Since string variables contain names (which repeat themselves most of the time), using value labels allows you to save a lot of space when dealing with large dataset.</p>
  
  <p>Unfortunately, I did not manage to find a similar command in R. The only package I have found that could attach labels to my values vector is <code>sjlabelled</code>. It does the attachment but when I’m trying to merge attached numeric vector to another dataframe, the labels seems to “fall of”.</p>
  
  <p>Example: Start with a string variable. </p>
  
  <pre><code>paragraph &lt;- "Melanija Knavs was born in Novo Mesto, and grew up in Sevnica, in the Yugoslav republic of Slovenia. She worked as a fashion model through agencies in Milan and Paris, later moving to New York City in 1996. Her modeling career was associated with Irene Marie Models and Trump Model Management"
  install.packages("sjlabelled")
  library(sjlabelled)
  sentences &lt;- strsplit(paragraph, " ")
  sentences &lt;- unlist(sentences, use.names = FALSE)
            # Now we have a vector to string values.
  sentrnces_df &lt;- as.data.frame(sentences)
  sentences       &lt;- unique(sentrnces_df$sentences)
  group_sentences &lt;- c(1:length(sentences))
  sentences       &lt;- as.data.frame(sentences)
  group_sentences &lt;- as.data.frame(group_sentences)
  z &lt;- cbind(sentences,group_sentences)
  z$group_sentences &lt;- set_labels(z$group_sentences, labels = (z$sentences))
  sentrnces_df &lt;- merge(sentrnces_df, z, by = c('sentences'))
  get_labels(z$group_sentences)       # the labels I was attaching using set labels
  get_labels(sentrnces_df$group_sentences) # the output is just “NULL”
  </code></pre>
  
  <p>Thanks!</p>
  
  <p>P.S. Sorry about the inelegant code, as I said before, I'm pretty new in R.</p>