title: Object not found error with ddply inside a function
body: <p>This has really challenged my ability to debug R code. </p>
  
  <p>I want to use <code>ddply()</code> to apply the same functions to different columns that are sequentially named; eg. a, b, c. To do this I intend to repeatedly pass the column name as a string and use the <code>eval(parse(text=ColName))</code> to allow the function to reference it. I grabbed this technique from another answer.</p>
  
  <p>And this works well, until I put <code>ddply()</code> inside another function. Here is the sample code:</p>
  
  <pre><code># Required packages:
  library(plyr)
  
  myFunction &lt;- function(x, y){
      NewColName = "a"
      z = ddply(x, y, summarize,
              Ave = mean(eval(parse(text=NewColName)), na.rm=TRUE)
      )
      return(z)
  }
  
  a = c(1,2,3,4)
  b = c(0,0,1,1)
  c = c(5,6,7,8)
  df = data.frame(a,b,c)
  sv = c("b")
  
  #This works.
  ColName = "a"
  ddply(df, sv, summarize,
          Ave = mean(eval(parse(text=ColName)), na.rm=TRUE)
  )
  
  #This doesn't work
  #Produces error: "Error in parse(text = NewColName) : object 'NewColName' not found"
  myFunction(df,sv)
  
  #Output in both cases should be
  #  b Ave
  #1 0 1.5
  #2 1 3.5
  </code></pre>
  
  <p>Any ideas? NewColName is even defined inside the function!</p>
  
  <p>I thought the answer to this question, <a href="https://stackoverflow.com/questions/5873922/loops-to-create-new-variables-in-ddply">loops-to-create-new-variables-in-ddply</a>, might help me but I've done enough head banging for today and it's time to raise my hand and ask for help.</p>