title: How to summarize over multiple columns programatically using ddply?
body: <p>Is it possible to specify which columns to aggregate over with ddply from the arguments of a function, without using eval + parse? Heres what I have so far:</p>
  
  <pre><code>x &lt;- c(2,4,3,1,5,7)
  y &lt;- c(3,2,6,3,4,6)
  group1 &lt;- c("A","A","A","A","B","B")
  group2 &lt;- c("X","X","Y","Y","Z","X")
  
  data &lt;- data.frame(group1, group2, x, y)
  </code></pre>
  
  <p>Heres what I want the output to be:</p>
  
  <pre><code>aggFunction &lt;- function(dataframe, toAverage, toGroup) {
    out &lt;- ddply(dataframe, toGroup, summarise, 
                 x = mean(x),
                 y = mean(y))
    return(out)
  }
  
  aggFunction(data, c("x", "y"), c("group1", "group2"))
  
  # group1 group2 x   y
  # 1      A      X 3 2.5
  # 2      A      Y 2 4.5
  # 3      B      X 7 6.0
  # 4      B      Z 5 4.0
  </code></pre>
  
  <p>Here's my solution using parse(eval):</p>
  
  <pre><code>aggFunction &lt;- function(dataframe, toAverage, toGroup) {
  
    toAverageArgs &lt;- paste(toAverage, " = mean(", toAverage, ")", sep = "", collapse = ", ")
    out &lt;- eval(parse(text = paste("ddply(dataframe, toGroup, here(summarize),", toAverageArgs, ")")))
  
    return(out)
  }
  </code></pre>
  
  <p>This gives me the output that I want.</p>
  
  <p>I'm wondering if there's a better way to do this. I'm aware of using do.call() and get(), but none of my attempts with these have worked.</p>
  
  <p>Heres an attempt;
  get(string) didn't work, but here(summarize) let me get the string value. Unfortunately, this means ddply treats them as strings:</p>
  
  <pre><code>aggFunction &lt;- function(dataframe, toAverage, toGroup) {
  
    string &lt;- paste(toAverage, " = mean(", toAverage, ")", sep = "", collapse = ", ")
    out &lt;- ddply(dataframe, toGroup, here(summarise), string)
  
    return(out)
  }
  
  aggFunction(data, c("x", "y"), c("group1", "group2"))
  
  # group1 group2                      ..2
  # 1      A      X x = mean(x), y = mean(y)
  # 2      A      Y x = mean(x), y = mean(y)
  # 3      B      X x = mean(x), y = mean(y)
  # 4      B      Z x = mean(x), y = mean(y)
  </code></pre>
  
  <p>Also tried do.call, but they were still treated as strings:</p>
  
  <pre><code>aggFunction &lt;- function(dataframe, toAverage, toGroup) {
  
    string &lt;- paste(toAverage, " = mean(", toAverage, ")", sep = "", collapse = ", ")
    print(string)
  
    args &lt;- list(dataframe, toGroup, here(summarise), string)
    out &lt;- do.call(ddply, args)
  
    return(out)
  }
  aggFunction(data, c("x", "y"), c("group1", "group2"))
  
  # group1 group2 "x = mean(x), y = mean(y)"
  # 1      A      X   x = mean(x), y = mean(y)
  # 2      A      Y   x = mean(x), y = mean(y)
  # 3      B      X   x = mean(x), y = mean(y)
  # 4      B      Z   x = mean(x), y = mean(y)
  </code></pre>
  
  <p>Lastly I tried hardcoding in mean(), but then I couldnt set the column name. If I use get(testVar) = mean(get(testVar)) I get unexpected =.</p>
  
  <pre><code>aggFunction &lt;- function(dataframe, toAverage, toGroup) {
  
    testVar &lt;- "x"
  
    out &lt;- ddply(dataframe, toGroup, here(summarise), 
             get(testVar) = mean(get(testVar))
             ## 
    return(out)
  }
  </code></pre>