title: Is there a way to assign parts of the variable labels to be the variable name in R
body: <p>Is there a way that I can grab parts of the variable label in a data frame and use that for the name of the variable? Something like this below works manually, but I'm wondering if there's a more automatic way to do this. Thank you.</p>
  <pre><code>library(tidyverse)
  library(labelled)
  
  var1&lt;-labelled(sample(c(0,1), replace=T, size=100), c(selected=1, `Not Selected`=0), label=&quot;Alpha Beta Omega&quot;)
  var2&lt;-labelled(sample(c(0,1), replace=T, size=100), c(selected=1, `Not Selected`=0), label=&quot;Delta Kappa Gamma&quot;)
  df&lt;-data.frame(var1, var2)
  
  var_label(df$var1)
  val_labels(df$var1)
  
  df %&gt;% 
    rename(Beta=var1, Kappa=var2)
  
  </code></pre>