title: Function for label variable before plotting in R
body: <p>I hope, this question is not too easy for this forum (actually, I'm almost a bit embarrassed to ask this question here, but I'm struggeling with this small issue the whole day...)</p>
  
  <p>I have dataframes look like the following:</p>
  
  <pre><code>df &lt;- data.frame(runif(4),
               c("po", "pr", "po", "pr"),
               c("Control 1","Control 1", "Treatment 1", "Treatment 1"))
  
  names(df) &lt;- list("values", "test_type", "group")
  </code></pre>
  
  <p>Now, I want easliy re-label the variables "test_type" and "group" for the plot afterwards. (it's nicer to read "pretest" instead of "pr" in a presentation :-) )
  I could do it manually with:</p>
  
  <pre><code>df$test_type &lt;- factor(df$test_type,
                       levels = c("pr", "po"),
                       labels = c("pretest", "posttest"))
  df$group &lt;- factor(df$group,
                   levels = c("Control 1", "Treatment 1"),
                   labels = c("control", "EST"))
  </code></pre>
  
  <p>In this case, I would have to repeat this for a lot more dataframes, which lead me to write a function:</p>
  
  <pre><code>var_label &lt;- function(df, test, groups){
  
  # Create labels
  df$test_type &lt;- factor(df$test,
                       levels = c("pr", "po"),
                       labels = c("pretest", "posttest"))
  df$group &lt;- factor(df$groups,
                   levels = c("Control 1", "Treatment 1"),
                   labels = c("control", "EST"))
  
  return(list(df$test_type, df$group))
  }
  </code></pre>
  
  <p>Unfortunately, this doesn't work. I tried a lot slight different versions and also different command from the Hmisc package, but none of these worked. I know, I can solve this problem in another way, but I try to write more efficient and shorter codes and would be really interested, what I have to change to make this function work. Or even better do you have a suggestion for a more efficient way?</p>
  
  <p>Thank you a lot in advance!!</p>