title: Looping in Dplyr
body: <p>My question refers to looping in Dplyr. I'm trying to determine the mean, count number and variance for DV1 for each unique combination of independent variables (IV1:IV5).  My data looks like this: </p>
  
  <pre><code>DV1     IV1     IV2     IV3     IV4  IV5
  506.2   Male    Canada  alpha   low  Orange
  418.7   Female  Canada  beta    mid  Orange
  380.3   Male    Canada  alpha   high Red
  347.3   Male    Canada  alpha   mid  Red
  241.6   Female  UStates alpha   mid  Blue
  223.7   Female  Canada  beta    low  Green
  220.9   Male    Canada  alpha   low  Orange
  201.1   Male    Canada  alpha   low  Red
  193.8   Female  Canada  beta    mid  Green
  179.7   Female  UStates beta    low  Orange
  170.7   Male    UStates beta    mid  Blue
  149.5   Male    UStates beta    low  Green
  146.2   Male    Canada  beta    high Green
  144.2   Female  UStates beta    mid  Red
  141.5   Male    Canada  beta    high Blue
  138.6   Male    Canada  beta    mid  Blue
  137     Male    Canada  beta    low  Red
  136     Female  UStates beta    mid  Orange
  135.9   Female  UStates beta    low  Red
  134.6   Male    UStates alpha   mid  Orange
  129     Female  UStates beta    mid  Green
  127.1   Female  UStates beta    low  Green
  120.4   Male    UStates beta    low  Blue
  119.3   Female  UStates beta    high Red
  118.6   Female  Canada  alpha   low  Blue
  116.2   Female  Canada  alpha   high Green
  113.7   Male    UStates beta    high Green
  112.9   Female  UStates beta    low  Green
  112.6   Male    Canada  alpha   mid  Green
  112.2   Male    Canada  alpha   mid  Orange
  109     Female  Canada  beta    high Orange
  108.1   Female  Canada  alpha   mid  Blue
  99.1    Female  Canada  alpha   high Blue
  95.6    Male    UStates beta    mid  Green
  88.1    Male    Canada  alpha   high Blue
  83.9    Female  Canada  beta    high Green
  83.7    Male    Canada  alpha   low  Green
  80.8    Male    Canada  alpha   high Orange
  79.9    Female  UStates alpha   high Blue
  78      Female  UStates alpha   mid  Red
  76.3    Female  UStates alpha   low  Blue
  74.1    Female  UStates beta    high Orange
  65.7    Female  UStates beta    high Red
  62.1    Male    UStates alpha   high Red
  54.8    Male    Canada  beta    low  Blue
  54      Male    UStates alpha   mid  Red
  42.8    Female  UStates alpha   low  Red
  39.6    Male    UStates alpha   high Orange
  19.5    Male    UStates alpha   low  Orange
  19.2    Female  Canada  alpha   mid  Green
  </code></pre>
  
  <p>Using Dplyr, I've been able to create this. I need the information available under descriptive statistics for every unique combination of IV1:IV5</p>
  
  <pre><code>mod1=data1 %&gt;%                                           
    group_by(IV1)%&gt;%  
    summarise(avg_banding=mean(DV1),             
              total.count=n(),                          
              variance=var(DV1)) %&gt;%                 
    print(n=50)     
  #Descriptive statistics for the entire group (required!)
  sum(mod1$avg_banding) 
  mean(mod1$avg_banding)
  mean(mod1$total.count)
  mod1_2=mod1[complete.cases(mod1),] 
  sum(mod1_2$variance) 
  </code></pre>
  
  <p>As you can probably tell, I have to do this for every possible combination...
  (IV1) (IV1,IV2) (IV1,IV2,IV3) (IV2, IV3) etc. There are a lot of combinations and I'm wondering if there's an easier way to do this instead of plugging away for each one! I have tried using a for loop without success. </p>
  
  <pre><code>mod2=data1 %&gt;%                                             
    group_by(IV2)%&gt;%  
    summarise(avg_banding=mean(DV1),             
              total.count=n(),                          
              variance=var(DV1)) %&gt;%                 
    print(n=50)     
  
  sum(mod2$avg_banding) 
  mean(mod2$avg_banding)
  mean(mod2$total.count)
  mod2_2=mod2[complete.cases(mod2),] 
  sum(mod2_2$variance)
  </code></pre>
  
  <p>Any help is greatly appreciated folks! thanks!</p>