title: Use ddply within a function and include variable of interest as an argument
body: <p>I am relatively new to R, and trying to use ddply &amp; summarise from the plyr package. <a href="https://stackoverflow.com/questions/8218196/object-not-found-error-when-passing-model-formula-to-another-function">This post</a> almost, but not quite, answers my question. I could use some additional explanation/clarification.</p>
  
  <p>My problem:</p>
  
  <p>I want to create a simple function to summarize descriptive statistics, by group, for a given variable. Unlike the linked post, I would like to include the variable of interest as an argument to the function. As has already been discussed on this site, this works:</p>
  
  <pre><code>require(plyr)
  
  ddply(mtcars, ~ cyl, summarise,
    mean = mean(hp),
    sd   = sd(hp),
    min  = min(hp),
    max  = max(hp)
  )
  </code></pre>
  
  <p>But this doesn't:</p>
  
  <pre><code>descriptives_by_group &lt;- function(dataset, group, x)
  {
    ddply(dataset, ~ group, summarise,
      mean = mean(x),
      sd   = sd(x),
      min  = min(x),
      max  = max(x)
    )
  }
  
  descriptives_by_group(mtcars, cyl, hp)
  </code></pre>
  
  <p>Because of the volume of data with which I am working, I would like to be able to have a function that allows me to specify the variable of interest to me as well as the dataset and grouping variable. </p>
  
  <p>I have tried to edit the various solutions found <a href="https://stackoverflow.com/questions/8218196/object-not-found-error-when-passing-model-formula-to-another-function">here</a> to address my problem, but I don't understand the code well enough to do it successfully. </p>
  
  <p>The original poster used the following example dataset:</p>
  
  <pre><code>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")
  </code></pre>
  
  <p>With the desired output:</p>
  
  <pre><code>  b Ave
  1 0 1.5
  2 1 3.5
  </code></pre>
  
  <p>And the solution endorsed by Hadley was: </p>
  
  <pre><code>myFunction &lt;- function(x, y){
  NewColName &lt;- "a"
  z &lt;- ddply(x, y, .fun = function(xx,col){
                           c(Ave = mean(xx[,col],na.rm=TRUE))}, 
             NewColName)
  return(z)
  }
  </code></pre>
  
  <p>Where <code>myFunction(df, sv)</code> returns the desired output. </p>
  
  <p>I tried to break down the code piece-by-piece to see if, by getting a better understanding of the underlying mechanics, I could modify the code to include an argument to the function that would pass to what, in this example, is "NewColName" (the variable you want to get information about). But I am not having any success. My difficulty is that I do not understand what is happening with <code>(xx[,col])</code>. I know that mean(xx[,col]) should be taking the mean of the column with index <code>col</code> for the data frame <code>xx</code>. But I don't understand where the anonymous function is reading those values from. </p>
  
  <p>Could someone please help me parse this? I've wasted hours on a trivial task I could accomplish easily with very repetitive code and/or with subsetting, but I got hung up on trying to make my script more simple and elegant, and on understanding the "whys" of this problem and its solution(s). </p>
  
  <p>PS I have looked into the describeBy function from the psych package, but as far as I can tell, it does not let you specify the variable(s) you want to return values for, and consequently does not solve my problem.</p>