title: R error in '[<-.data.frame 'C' *temp*',… replacement has # items, need #
body: <p>I have a function that I've executed many times but is now throwing an error, which I do not understand. I'm trying to apply the function over a list.
  I did not write the function and I have limited experience with functions. So, I'm not sure how to troubleshoot the code.</p>
  
  <p><strong>function:</strong></p>
  
  <pre><code>myfun&lt;-function(Year, SampleID, Species, Abundance, resamps) {
  
    library(vegan)
    counter&lt;-1
    simbaseline&lt;-data.frame(array(NA,dim=c(length(unique(Year)),5)))
    names(simbaseline)&lt;-c("Year", "Jaccard","Horn","Bray","Pearson")
  
    simnext&lt;-data.frame(array(NA,dim=c(length(unique(Year)),5)))
    names(simnext)&lt;-c("Year", "Jaccard","Horn","Bray","Pearson")
  
    simhind&lt;-data.frame(array(NA,dim=c(length(unique(Year)),5)))
    names(simhind)&lt;-c("Year", "Jaccard","Horn","Bray","Pearson")
  
    counter2&lt;-1
  
    # getting vector with number of samples per year
    nsamples&lt;-c()
    for(y in unique(Year)){
      nsamples&lt;-c(nsamples, length(unique(SampleID[Year==y])))
    }
    t&lt;-1
    minsample&lt;-min(nsamples)
    for(repeats in 1:resamps){
      raref&lt;-data.frame(array(NA,dim=c(1,3)))
      names(raref)&lt;-c("Year","Species","Abundance")
      for(y in unique(Year)){
        #getting samples for this year
        samps&lt;-unique(SampleID[Year==y])
        # re-sampling samples to equalize number of samples
        sam&lt;-as.character(sample(samps,minsample,replace=T))
        # getting data that belongs to bootstraped samples
        rarefyear&lt;-data.frame(SampleID[which(SampleID %in% sam &amp; Year == y)],
                              Species[which(SampleID %in% sam &amp; Year == y)],
                              Abundance[which(SampleID %in% sam &amp; Year == y)])
        names(rarefyear)&lt;-c("SampleID", "Species", "Abundance")
        # calculating pooled abundances of eahc species to store
        spabun&lt;-tapply(as.numeric(rarefyear[,3]),rarefyear[,2],sum)
        spar&lt;-data.frame(rep(y, length(spabun)),names(spabun),spabun, row.names=NULL)
        names(spar)&lt;-c("Year","Species","Abundance")
        raref&lt;-rbind(raref,spar)
        counter&lt;-counter+1
      }
      # calculating year by species table of abundance
      rareftabtemp&lt;-with(raref,tapply(Abundance,list(Year,Species),function(x)x))
      rareftabtemp[is.na(rareftabtemp)]&lt;-0
      Pearsoncor&lt;-cor(t(log(rareftabtemp+1)), method="pearson")
      # calculating between year similarities (NOT DISTANCE!) with Jaccard, Morisita-Horn, Bray and Pearson correlations
      Jacsim&lt;-as.matrix(1-vegdist(rareftabtemp, method="jaccard"))
      Hornsim&lt;-as.matrix(1-vegdist(rareftabtemp, method="horn"))
      Braysim&lt;-as.matrix(1-vegdist(rareftabtemp, method="bray"))
      n&lt;-length(unique(Year))
      simbaseline[counter2:(counter2+n-2),]&lt;-
           cbind(unique(Year)[2:n],Jacsim[2:n],Hornsim[2:n],Braysim[2:n],Pearsoncor[2:n])
      simnext[counter2:(counter2+n-2),]&lt;-
           cbind(unique(Year)[2:n],Jacsim[row(Jacsim)-col(Jacsim)==1],
                 Hornsim[row(Hornsim)-col(Hornsim)==1],
                 Braysim[row(Braysim)-col(Braysim)==1],
                 Pearsoncor[row(Pearsoncor)-col(Pearsoncor)==1])
      # added hindcasting 
      simhind[counter2:(counter2+n-2),]&lt;-
            cbind(unique(Year)[1:(n-1)],
                  Jacsim[row(Jacsim) %in% 1:(max(row(Jacsim))-1) &amp; 
                         col(Jacsim)==max(col(Jacsim))],
                  Hornsim[row(Hornsim)%in%1:(max(row(Hornsim))-1) &amp; 
                        col(Hornsim)==max(col(Hornsim))],
                  Braysim[row(Braysim)%in%1:(max(row(Braysim))-1) &amp;
                        col(Braysim)==max(col(Braysim))], 
                  Pearsoncor[row(Pearsoncor)%in%1:(max(row(Pearsoncor))-1) &amp; 
                        col(Pearsoncor)==max(col(Pearsoncor))]) 
      counter2&lt;-counter2+n
    }
  
  
    baselinesim&lt;-data.frame(unique(Year)[2:n],
         tapply(simbaseline$Jaccard,simbaseline$Year,mean),
         tapply(simbaseline$Horn,simbaseline$Year,mean),
         tapply(simbaseline$Bray,simbaseline$Year,mean),
         tapply(simbaseline$Pearson,simbaseline$Year,mean))
    names(baselinesim)&lt;-c("Year", "Jaccard","Horn","Bray","Pearson")
    nextsim&lt;-data.frame(unique(Year)[2:n],
         tapply(simnext$Jaccard,simnext$Year,mean),
         tapply(simnext$Horn,simnext$Year,mean),
         tapply(simnext$Bray,simnext$Year,mean),
         tapply(simnext$Pearson,simnext$Year,mean))
    names(nextsim)&lt;-c("Year", "Jaccard","Horn","Bray","Pearson")
    hindcastsim&lt;-data.frame(unique(Year)[1:(n-1)],
         tapply(simhind$Jaccard,simhind$Year,mean),
         tapply(simhind$Horn,simhind$Year,mean),
         tapply(simhind$Bray,simhind$Year,mean),
         tapply(simhind$Pearson,simhind$Year,mean))
    names(hindcastsim)&lt;-c("Year", "Jaccard","Horn","Bray","Pearson")
    a&lt;-list(baselinesim,nextsim,hindcastsim)
  
    return(a)
  }
  </code></pre>
  
  <p><strong>error:</strong></p>
  
  <blockquote>
    <p>Error in <code>[&lt;-.data.frame</code>(<code>*tmp*</code>, counter2:(counter2 + n - 2), , value = c(NA,  : replacement has 2 items, need 5</p>
  </blockquote>
  
  <p><strong><em>Traceback</em></strong></p>
  
  <pre><code>6.
  stop(sprintf(ngettext(m, "replacement has %d item, need %d", 
      "replacement has %d items, need %d"), m, n * p), domain = NA) 
  5.
  `[&lt;-.data.frame`(`*tmp*`, counter2:(counter2 + n - 2), , value = structure(c(NA, 
  2009), .Dim = 2:1)) 
  4.
  `[&lt;-`(`*tmp*`, counter2:(counter2 + n - 2), , value = structure(c(NA, 
  2009), .Dim = 2:1)) 
  3.
  myfun(x$Year, x$Bay, x$Species, x$Abundance, 20) 
  2.
  FUN(X[[i]], ...) 
  1.
  lapply(summer.split, function(x) myfun(x$Year, x$Bay, 
      x$Species, x$Abundance, 20)) 
  </code></pre>
  
  <p>Again, the function worked 
  Someone seems to have asked a similar question before and was answered by @Marat Talipov but I'm not experienced enough to make sense of what the solution was. </p>
  
  <p>The answer was:</p>
  
  <p><strong>This error pops up when you're unlucky and i &lt;- runif(n) &lt; 1/2 consists only of FALSE, i.e. no permutations happen. You need to add a check in the swap function to fix this problem.</strong></p>
  
  <p><a href="https://stackoverflow.com/questions/28614199/r-error-in-data-frame-replacement-has-items-need">R error in &#39;[&lt;-.data.frame&#39;... replacement has # items, need #</a></p>
  
  <p>A subset of my data can be found here:</p>
  
  <p><a href="https://fil.email/sI4Kyhaj" rel="nofollow noreferrer">https://fil.email/sI4Kyhaj</a></p>
  
  <p>The data was split by "Bay" to generate the list</p>
  
  <p>Note that the function may not throw an error on a different machine because it seems to occur periodically.</p>