title: For-loop working but not as axpected
body: <p>I'm working on making a loop to compute three columns: the min, the max and the mean of the mesurement of several plots. I am working with forest inventories of several measurements at thousands of plots.
  What I want to do is to compute the min, max and mean of the basal area (a measurement) for each species (153 species total) at specific plots that differ between the species.</p>
  
  <p>First,I have to select, for each species, all the plots matching the criteria (plots are purs== at least 80 % of the plots is composed by only one species) from purs 80.</p>
  
  <pre><code>head(purs80[,1:10])
          02         03 04 05 06 07 08S  09       10 11
  27    0.000000   0.000000  0  0  0  0   0   0  0.00000  0
  41    0.000000   0.000000  0  0  0  0   0   0  0.00000  0
  47    6.369376   8.824162  0  0  0  0   0   0 84.80646  0
  54    0.000000 100.000000  0  0  0  0   0   0  0.00000  0
  83  100.000000   0.000000  0  0  0  0   0   0  0.00000  0
  101   0.000000   0.000000  0  0  0  0   0 100  0.00000  0
  
  
  
  #list of all the purs plots by species
    listplotspur80&lt;-apply(purs80, 2,function(v) which(v &gt; 80))
  </code></pre>
  
  <p>This is working. Listplotspur is a list of 153 element each of them composed by the number of plots where the criteria is met. Just the head of a summary of it as well as the last element.</p>
  
  <blockquote>
  <pre><code>  head(summary( listplotspur80))
    Length Class    Mode     
  02 "1422" "-none-" "numeric"
  03 "1479" "-none-" "numeric"
  04 "  50" "-none-" "numeric"
  05 "1836" "-none-" "numeric"
  06 " 689" "-none-" "numeric"
  07 "  51" "-none-" "numeric"
  </code></pre>
    
    <p>So you can see the number of elements vary for each element of the list.</p>
  </blockquote>
  
  <pre><code>&gt; listplotspur80[[153]]
   22455 505927 516264 524860 545205 639576 
   1345  15389  15738  16029  16711  19410 
  </code></pre>
  
  <p>This give me the plotID as names that Ican extract with the function names as below</p>
  
  <pre><code>&gt; names(listplotspur80[[153]])
  [1] "22455"  "505927" "516264" "524860" "545205" "639576"
  </code></pre>
  
  <p>Now that I'm able to extract the list of plots for each species, I need to associate to each plot, its value of the basal area BA which are stocked into a data frame called BA.</p>
  
  <pre><code> &gt;       head(BA)
            BA plotID
  19 41.72365     19
  23 13.37109     23
  27 55.92989     27
  41 25.50725     41
  45 34.86734     45
  47 30.63582     47
  
  &gt;       dim(BA)
  [1] 44065     2
  </code></pre>
  
  <p>So from this list of element where I have the list of plots for each species and the data frame BA where I have the BA associated to each plot I want to calculate min,max and mean from these plot for every species and stock this into a new data frame.  </p>
  
  <pre><code>#Create a loop that does the job!
    outG80&lt;-matrix(nrow=153, ncol=3, NA)
    for (i in 1:153 ){
      outG80[i,1]&lt;-min(BA[which(BA$plotID==as.numeric(names(listplotspur80[[i]]))),1])
  </code></pre>
  
  <p>For each species, I am selecting the rows corresponding to the plotIDs I have according to the list and I'm applying the function to the all the BA corresponding (column 1 of BA).</p>
  
  <pre><code>   outG80[i,2]&lt;-max(BA[which(BA$plotID==as.numeric(names(listplotspur80[[i]]))),1])
      outG80[i,3]&lt;-mean(BA[which(BA$plotID==as.numeric(names(listplotspur80[[i]]))),1])
      }
    outG80&lt;-as.data.frame(outG80)
    names(outG80)&lt;-c("Gmin","Gmax","Gmean")
    outG80
  </code></pre>
  
  <p>So the loop work and I am able to get a data frame as I want...BUT it just not the good results and I can't find why. See the min and max are the same whereas I know that I have 1422 different plots for the first species for example with different value for the BA.</p>
  
  <pre><code>  Gmin     Gmax    Gmean
  1 33.23970 33.23970 33.23970
  2 29.89472 29.89472 29.89472
  3 13.90947 43.33606 28.62277
  4 17.91288 17.91288 17.91288
  5      Inf     -Inf      NaN
  6 11.42602 11.42602 11.42602
  </code></pre>
  
  <p>If you have any idea of the mistake in my loop please let me know.</p>
  
  <p>Thanks a lot for your help.</p>
  
  <p>I have been trying to do some code that you could use to replicate the problem but I end up with huge data frames. Sorry for this inconvenient.</p>