title: Repeat entry in data.frame filled from for() loop
body: <p>I have a data set containing observations for individuals and years.  I want to isolate the data to each individual.  Within each individual, I want to fit a number of models for each year. I then want to compile the AIC score for each individual-year model into a <code>data.frame</code>, which are then held in a list.  </p>
  
  <p>I have created a reproducible example below using the <code>iris</code> data.  I am using a nested for loop to subset the data to individual (i) and then year (j).  </p>
  
  <p><strong>Problem</strong>:  Some species have data for three years (e.g. setosa and versicolor), while virginica only has data for a single year (2014).</p>
  
  <p>When the species <code>data.frames</code> are added to the list, species with fewer than three years contain repeat records from the previous years.  As seen in the results below, <code>list[[3]]</code> should contain a single row of data for the virginica species in 2014 and not also the repeat records of versicolor from 2011 and 2012.  </p>
  
  <p>Is it possible to have a list of <code>data.frames</code> with different number of rows?</p>
  
  <p>I welcome any suggestions to improve the nested for loop.  I suspect there are better ways to do this, maybe with <code>dplyr</code>  </p>
  
  <pre><code>head(iris)
  
  #Add year to iris to reproduce problem
  iris$Year &lt;- c(rep(c("2011", "2012", "2013"), len = 100), rep("2014", 50))
  
  #Make list of species of index and empty list
  Sp &lt;- unique(iris$Species)
  ResultsList &lt;- list()
  
  #make Results table
  AICTbl &lt;- data.frame(Species = as.character(), AIC_1 = numeric(), AIC_2 = numeric(), stringsAsFactors=FALSE)
  
  #run nested for loop
  for(i in 1:length(Sp)) {
    SpeciesDat &lt;- subset(iris, Species == Sp[i])
  
    for(j in 1:length(unique(SpeciesDat$Year))) {
      Years &lt;- unique(SpeciesDat$Year)
      datasub &lt;- SpeciesDat[SpeciesDat$Year == Years[j] , ]
  
      #Make the animal Year rowID
      AICTbl[j,1] = paste(Sp[i] , Years[j], sep = "_")
  
      mod.1 &lt;- lm(Sepal.Length ~ Sepal.Width, data = datasub)
      AICTbl[j,2] = AIC(mod.1)
  
      mod.2 &lt;- lm(Sepal.Length ~ Petal.Length, data = datasub)
      AICTbl[j,3] = AIC(mod.2)
  
            }
          ResultsList[[i]] &lt;- AICTbl
      }
  
  
  &gt; ResultsList
  [[1]]
        Species    AIC_1     AIC_2
  1 setosa_2011 3.649775 21.089619
  2 setosa_2012 4.512392  2.280987
  3 setosa_2013 4.638183 19.718576
  
  [[2]]
            Species    AIC_1    AIC_2
  1 versicolor_2013 25.30007 18.93356
  2 versicolor_2011 26.57170 15.22717
  3 versicolor_2012 21.62511 11.82291
  
  [[3]]
            Species    AIC_1    AIC_2
  1  virginica_2014 89.88263 32.91367
  2 versicolor_2011 26.57170 15.22717
  3 versicolor_2012 21.62511 11.82291
  </code></pre>
  
  <p><strong>Addition</strong>
  Thanks for your suggestions and improvements @jeremycg.  The following addition is a continuation, although you have answered the OP.  Any additional thoughts would be appreciated.</p>
  
  <p>A non-reproducible example of my real code is included below which shows one of 5 models that are fitted for each IndID-Year.</p>
  
  <pre><code>datasub %&gt;% group_by(IndID, Year) %&gt;% 
      do(m.1 = try(nls(NSD ~ (asym/(1+exp((t1 - J_day_new)/dur1)))+(-asym/(1+exp((t2 - J_day_new)/dur2))), 
            control=nls.control(maxiter = 1000, warnOnly=TRUE), algorithm="port",
                    start=c(asym=0.75*max(datasub$NSD), t1=135, t2=285, dur1=2, dur2=2),
                data = datasub),TRUE)
  
        ) %&gt;%
      mutate(m.1 = AIC(m.1))
  </code></pre>
  
  <p>If I replace <code>datasub</code> (the data frame) with <code>.</code> I get the following error.  </p>
  
  <pre><code>Error in UseMethod("logLik") : 
    no applicable method for 'logLik' applied to an object of class "try-error"
  </code></pre>