title: Loops, knitr and xtable in rmarkdown to create unique tables in multiple reports
body: <p>I'm revising my question in full. I realized it was long, and my point was getting lost. </p>
  
  <p>Here's what I need to do: </p>
  
  <p>Create automatic reports for schools that contain tables that compare their data to the district that school is in as well as the whole state. The State is the entire data set. </p>
  
  <p>Here's what I understand:</p>
  
  <p>How to create the automated loop that transverses through the data and creates a unique PDF report for each school. <a href="https://stackoverflow.com/questions/15396755/using-loops-with-knitr-to-produce-multiple-pdf-reports-need-a-little-help-to">This post</a> was very helpful in setting up a framework to generate reports.</p>
  
  <p>Here's what I need help with:</p>
  
  <p>I need a table that contains the following columns: School, District, State
  I also need the first column of the table to contain a row for: Sample Size, Mean, Standard Deviation. </p>
  
  <p>I'm trying to create this in the context of a for loop because I need a unique table in each unique pdf that is created. If there is a better approach I'd love to hear about it. </p>
  
  <p>Anyways, here is a reproducible example that I tested. I haven't gotten far in creating the table. </p>
  
  <p>Any help would be very appreciated.</p>
  
  <p><code>driver.r</code>:</p>
  
  <pre><code># Create dataset
  set.seed(500)
  School &lt;- rep(seq(1:20), 2)
  District &lt;- rep(c(rep("East", 10), rep("West", 10)), 2)
  Score &lt;- rnorm(40, 100, 15)
  Student.ID &lt;- sample(1:1000,8,replace=T)
  school.data &lt;- data.frame(School, District, Score, Student.ID)
  
  #prepare for multicore processing 
  require(parallel)
  # generate the rmd files, one for each school in df
  library(knitr)
  mclapply(unique(school.data$School), function(x) 
    knit("F:/sample-auto/auto.Rmd", 
         output=paste('report_', x, '.Rmd', sep="")))
  
  # generate PDFs from the rmd files, one for each school in df
  mclapply(unique(school.data$School), function(x)
    rmarkdown::render(paste0("F:/sample-auto/", paste0('report_', x, '.Rmd'))))
  </code></pre>
  
  <p><code>auto.Rmd</code>:</p>
  
  <pre><code>---
  title: "Automated Report Generation for Data"
  author: "ME"
  date: "February 5, 2015"
  output: 
    pdf_document:
    toc: true
    number_sections: true
  ---
  
  ```{r, echo=FALSE}
  library(xtable)
  library(plyr)
  df &lt;- data.frame(school.data)
  subgroup &lt;- df[school.data$School == x,]
  ```
  
  # Start of attempt 
  
  ```{r results='asis', echo=FALSE}
   for(school in unique(subgroup$School))
  {
  subgroup2 &lt;- subgroup[subgroup$School == school,]
  savename &lt;- paste(x, school)
  df2&lt;- mean(subgroup2$Score, na.rm=TRUE)
  df2 &lt;- data.frame(df2)
  print(xtable(df2))
  }
  ```
  </code></pre>
  
  <p>I also tried replacing the loop with: </p>
  
  <pre><code>```{r results='asis', echo=FALSE}
  df2 &lt;- ddply(school.data, .(School), summarise, n = length(School), mean =      
  mean(Score), sd = sd(Score))
  print(xtable(df2))
  ```
  </code></pre>
  
  <p>This gives me what I don't want in that all of the schools get the data for every school vs. just their school.</p>