title: Pass a variable name to a user written function that uses dyplr
body: <p>I am trying to write a function that index variables names.
  In particular, in my function, I use <code>mutate</code> to encode a variable that I have without changing its name. Does anyone knows how I can index a variable on the left end side of mutate?</p>
  <p>Here is an example</p>
  <pre><code>library(tydiverse)
  # first create relevant dataset
  
  iris &lt;- iris%&gt;% group_by(Species) %&gt;% mutate(mean_Length=mean(Sepal.Length))
  
  # second create my function
  userfunction &lt;- function(var){
    newdata &lt;- iris  %&gt;% 
      select(mean_Length,{var}) %&gt;% distinct() %&gt;% 
      mutate(get(var)= # this is what causes my function to fail. How can i refer to the `var` here?
  factor(get(var),get(var))) %&gt;% 
  arrange(get(var)) # 
    return(newdata)
  }
  # this function produces the following error # Error: unexpected '}' in &quot;}&quot;
  
  #note that if I change the reference to its original string the function works
  userfunction2 &lt;- function(var){
    newdata &lt;- iris  %&gt;% 
      select(mean_Length,{var}) %&gt;% distinct() %&gt;% 
      mutate(Species= # without reference it works, but I am unable to use the function for multiple variables. 
  factor(get(var),get(var))) %&gt;% 
  arrange(get(var)) # 
    return(newdata)
  }
  encodedata&lt;- userfunction2(&quot;Species&quot;)
  
  </code></pre>
  <p>Thanks a lot in advance for your help</p>
  <p>Best</p>