title: How do I loop bootstrapping function and add results to a table?
body: <p>I have two tables of data - the first is a full dataset with replicates (density) for one factor (habitat):</p>
  
  <pre><code>Table1 &lt;- data.frame(
  Habitat = sample(c("Woodland", "Grassland"), 10, replace = TRUE),
  Density = sample(1:10)
  )
  </code></pre>
  
  <p>The second is a summarized version, with one row per habitat containing the median density. </p>
  
  <pre><code>library(dplyr)
  Table2 &lt;-ddply(Table1, "Habitat",summarise, Median = median(Density))
  </code></pre>
  
  <p>I've got the code below to get bootstrapped confidence intervals (using data in table 1)......</p>
  
  <pre><code>fun.boot &lt;- function(x, i) {median(x[i])}
  Wood.boot &lt;- boot(data = Table1$Density[Table1$Habitat=="Woodland"],statistic = fun.boot, R = 10000)
  boot.ci(boot.out = Wood.boot, conf = 0.95, type = c("perc"))
  </code></pre>
  
  <p>I would like to put this data into the corresponding (habitat = woodland) row of table 2, but can only figure out how to do it by hand using the following (same for upperCI).....</p>
  
  <pre><code>Table2$LowerCI &lt;- rep("NA",nrow(Table2))
  Table2$LowerCI[Table2$Habitat == "Woodland"] &lt;- 2
  </code></pre>
  
  <p>I have hundreds of runs to do (lots of habitats for lots of species), so i wonder if there is a way to automate this - i.e. </p>
  
  <ol>
  <li>generate the confidence intervals for each habitat using a for loop or something</li>
  <li>then read it into table 2?? </li>
  </ol>