title: For loop in R - Error: $ operator is invalid for atomic vectors
body: <p>I am attempting to find the significance of factor exposures, from a 5x60 matrix of <code>df_exposures</code> - 5 factors &amp; 60 portfolios</p>
  <p>The <code>df_exposures</code> matrix was initially calculated by regressing the monthly returns on 60 portfolios on certain &quot;macroeconomic shocks,&quot; - TM2R, IPR, InfR, UnR, OilR in the following loop</p>
  <pre><code>
      # Assign variable names
      TM2R &lt;- data[,2]
      IPR &lt;- data[,3]
      InfR &lt;- data[,4]
      UnR &lt;- data[,5]
      OilR &lt;- data[,6]
      
      # Run regression
      for (i in 7:69){
        model_name  &lt;- paste0(&quot;model_&quot;, i, &quot;.csv&quot;)
        model &lt;- lm(data = data, data[[i]]~TM2R+IPR+InfR+UnR+OilR)
        #Model coefficients
        tidy1 &lt;- tidy(model)
        #Standard Errors in regression results
        tidy1$vcov &lt;- vcovHC(model, type = &quot;HC1&quot;) 
        write.csv(tidy1, file = model_name)
      }
  </code></pre>
  <p>The loop which I am using to find how many of these exposures/&quot;beta-hats,&quot; are significant is as follows</p>
  <pre><code>
      alpha &lt;- 0.05
      for (i in 1:ncol(df_exposure)){
        for (j in 1:nrow(df_exposure)){
          # Store Beta_hat of asset &quot;i&quot; to shock &quot;j&quot;
          beta_hat &lt;- model[[i]]$coefficients[[j+1]]
          df_exposure[j,i] &lt;- beta_hat
          
          #Store beta_hat if significant in df_significance
          pval &lt;- coef(summary(model[[i]]))[j+1, &quot;pr(&gt;|t|)&quot;]
          if(pval &gt; alpha){
            df_significance[j,i] &lt;- 0
          }else{
            df_significance[j,i] &lt;- beta_hat
          }
        }
      }
  </code></pre>
  <p>However, R returns the error <code>Error: $ operator is invalid for atomic vectors</code>.</p>
  <p>I am unable to find a way to convert <code>model[[i]]</code> from an atomic vector to something else within a loop.</p>
  <p>It may be important to note that R returns
  <code>is.atomic(model[[i]]) = TRUE</code>
  <code>is.atomic(model) = FALSE</code></p>
  <p>Any help on the matter would be appreciated, there may also be a way to find how many of the model coefficients are significant within the first loop?</p>
  <p>Thanks :)</p>