title: Subscript out of bound error in R
body: <p>While Using <code>factanal</code> function from <strong>stats</strong> package for performing factor analysis. </p>
  
  <p>I tried following thing.</p>
  
  <pre><code>library(mirt)
  library(ltm)
  library(psych)
  library(stats)
  data(SAT12)
  data=SAT12
  cor_mat=polychoric(data,  ML=TRUE, global=F)
   fit &lt;- factanal(factors=2, n.obs=nrow(data), covmat=cor_mat$rho)
  
  
  Divide_item_Factor_Loading(fit)
  </code></pre>
  
  <p>when I am trying to run Divide_item_Factor_Loading(fit)  an error called </p>
  
  <pre><code>   Error in a[[i]][[2]] : subscript out of bounds 
  </code></pre>
  
  <p>pops up.</p>
  
  <p>my complete code of Divide_item_Factor_Loading:</p>
  
  <pre><code>Divide_item_Factor_Loading=function(fit)
  {
    a=list()
    items=NULL
    for(i in 1:nrow(fit$loadings)) ######corresponding to rows of loading matrix
    {
      k=which(fit$loadings[i,]==max(abs(fit$loadings[i,])))  
      a[[i]]=c(i,as.numeric(k))
    } 
    fact_item_mat=matrix(, nrow=nrow(fit$loadings), ncol=ncol(fit$loadings))
    for(j in 1:(ncol(fit$loadings)))
    {
      for(i in 1:(nrow(fit$loadings)))
      {
        if(a[[i]][[2]]==j) {fact_item_mat[i,j]=a[[i]][[1]]}
      }    
    }
    nam=names(fit$loadings[,1])
    factor=list()
    for(i in 1:ncol(fit$loadings))
    {
      factor[[i]]=sort(fact_item_mat[,i], decreasing = FALSE, na.last = NA)
      fac=factor[[i]]
      fac=nam[fac]
      factor[[i]]=fac
    }
    names(factor)=paste("factor", 1:ncol(fit$loadings), sep="")
    return(factor)
  }
  </code></pre>
  
  <p>What steps should I take now to avoid this error?</p>