title: Loop over each factor in lm in r
body: <p>I have a database that has several factors. I want to iterate a regression over each factor for each variable, but for the whole database. Something like this:</p>
  
  <pre><code>for (i in unique(db$Product)) {
   for (j in unique(db$Super)) {
     for (c in unique(db$Category)) {
       db$prueba &lt;- lm(prueba ~ 0 + as.factor(Super)[j] * 
              (as.factor(Time) + as.factor(Product)[i]) 
            +  as.factor(Category)[c] * as.factor(Time)
            , db)$residuals
     }
   }
  } 
  </code></pre>
  
  <p>Three of the previous variables are to be treated like factors: Category, Super, and Product. I want to pick just one of them in each iteration. That is why I try to call each time <code>Super[j]</code> in the previous example. If I use just as.factor(Category), then R will create one dummy variable for each value in Category, I want to pick just one per iteration.</p>
  
  <p>When I run the previous code I have the following error:</p>
  
  <p>Error in model.frame.default(formula = prueba ~ 0 + as.factor(Super)[j] *  : 
    variable lengths differ (found for 'as.factor(Super)[j]')</p>
  
  <p>Any help will be appreciated.</p>