title: paste variable names inside R function
body: <p>With the help of <a href="https://stackoverflow.com/users/1201032/flodel">flodel</a> I <a href="https://stackoverflow.com/questions/10002536/how-do-i-replace-numeric-codes-with-value-labels-from-a-lookup-table">found a way to replace numeric codes with value labels from a lookup table</a>.</p>
  
  <p>Ambitious as I am, I now want to put that into a function. Also, I have a lot of lookup tables I need to swoop onto my data so a function would be handy.</p>
  
  <p>But first some sample data, starting with a data fram,</p>
  
  <pre><code>df &lt;- data.frame(id = c(1:6),
                   profession = c(1, 5, 4, NA, 0, 5))
  
  df
  #  id profession
  #  1          1
  #  2          5
  #  3          4
  #  4         NA
  #  5          0
  #  6          5
  </code></pre>
  
  <p>and a lookup table with human readable information about the profession codes,</p>
  
  <pre><code>profession.lookuptable &lt;- c(Optometrists=1, Accountants=2, Veterinarians=3, 
                              `Financial analysts`=4,  Nurses=5)
  </code></pre>
  
  <p><a href="https://stackoverflow.com/users/1201032/flodel">flodel</a> showed me <a href="https://stackoverflow.com/questions/10002536/how-do-i-replace-numeric-codes-with-value-labels-from-a-lookup-table">how replace numeric codes with value labels from a lookup table</a>. Like this,</p>
  
  <pre><code>match.idx &lt;- match(df$profession, profession.lookuptable)
  df$profession &lt;- ifelse(is.na(match.idx), 
                   df$profession, names(profession.lookuptable)[match.idx])
  
  df
  #  id         profession
  #  1        Optometrists
  #  2              Nurses
  #  3  Financial analysts
  #  4                &lt;NA&gt;
  #  5                   0
  #  6              Nurses
  </code></pre>
  
  <p>I now want to put this into a function where I can state the data frame <code>df</code> and the name of the variable <code>profession</code> and have the function take care of the rest.</p>
  
  <p>I define my function like this,</p>
  
  <pre><code>ADDlookup &lt;- function(orginalDF, orginalVAR) {
     DF.VAR &lt;- paste(orginalDF, "$", orginalVAR, sep="")
     lookup.table &lt;- paste(orginalVAR, ".lookuptable")
     match.idx &lt;- match(DF.VAR, lookup.table)
     DF.VAR &lt;- ifelse(is.na(match.idx), DF.VAR, names(lookup.table)[match.idx])
  }
  </code></pre>
  
  <p>but apparently that is not working</p>
  
  <pre><code>ADDlookup(df, profession)
  </code></pre>
  
  <p>I get the errorer messes</p>
  
  <pre><code>Error in paste(orginalDF, "$", orginalVAR, sep = "") : 
    object 'profession' not found
  </code></pre>
  
  <p>Now, this is where I get stuck.</p>
  
  <p>Can anyone please tell what manual page I need to read or maybe a friendly hint on how to solve this?</p>
  
  <p>Thank you for reading.</p>