title: How to convert a string in a function into an object?
body: <p>I would like to convert a string that I pass in a function into an object (or column name).</p>
  
  <p>I know that this works:</p>
  
  <pre><code>df &lt;- data.frame(A = 1:10, B = 11:20)
  
  test.function &lt;- function(x)            
  {
    z &lt;- df[[x]]
    return(z)
  }
  test.function("A")
  </code></pre>
  
  <p>I don't want to use the [[.]] operator, because sometimes it is unpractical or even not applicable. I am interessted in a general method to convert a string into an "object". Therefore I tried the following:</p>
  
  <pre><code>df &lt;- data.frame(A = 1:10, B = 11:20)
  
  test.function &lt;- function(x)
  {
    z &lt;- get(paste("df$", x, sep = ""))
    return(z)
  }
  test.function("A")
  </code></pre>
  
  <p>or</p>
  
  <pre><code>df &lt;- data.frame(A = 1:10, B = 11:20)
  
  test.function &lt;- function(x)
  {
    z &lt;- as.name(paste("df$", x, sep = ""))
    return(z)
  }
  test.function("A")
  </code></pre>
  
  <p>or</p>
  
  <pre><code>df &lt;- data.frame(A = 1:10, B = 11:20)
  
  test.function &lt;- function(x)
  {
    z &lt;- df$as.name(x)
    return(z)
  }
  test.function("A")
  </code></pre>
  
  <p>I also tried to play around with the parse, do.call and eval functions. Unfortunatelly I failed</p>