title: R - dplyr - code to run a number of very similar queries...?
body: <p>Consider the following:</p>
  
  <pre><code>df &lt;- data.frame(
    Name = c("Alan", "Bob", "Christine", "David", "Erica"),
    Gender = c("M", "M", "F", "M", "F"),
    Star_Sign = c("Aquarius", "Capricorn", "Aquarius", "Libra", "Leo"),
    City = c("London", "Paris", "Berlin", "London", "Paris"),
    Blood_Group = c("A", "AB", "B", "O", "A"),
    Hours_Worked = c(2000, 1600, 0, 100, 200),
    Salary = c(100000, 20000, 0, 500, 4000)
  )
  
  Name_Summary &lt;-         df %&gt;% group_by(Name)        %&gt;% summarise(Hours_Worked = sum(Hours_Worked), Average_Salary = mean(Salary))
  Gender_Summary &lt;-       df %&gt;% group_by(Gender)      %&gt;% summarise(Hours_Worked = sum(Hours_Worked), Average_Salary = mean(Salary))
  Star_Sign_Summary &lt;-    df %&gt;% group_by(Star_Sign)   %&gt;% summarise(Hours_Worked = sum(Hours_Worked), Average_Salary = mean(Salary))
  City_Summary &lt;-         df %&gt;% group_by(City)        %&gt;% summarise(Hours_Worked = sum(Hours_Worked), Average_Salary = mean(Salary))
  Blood_Group_Summary &lt;-  df %&gt;% group_by(Blood_Group) %&gt;% summarise(Hours_Worked = sum(Hours_Worked), Average_Salary = mean(Salary))
  </code></pre>
  
  <p>Obviously this works fine for a small number of fields. If, however, I've got 100 different fields (say) to do this for, it becomes very unwieldy.</p>
  
  <p>I'd like to think that there is a way to loop through the list of fields and produce these summaries for each field, using some code to generate (and name the summaries), but I don't think I know how to do this. Can anyone help please?</p>
  
  <p>Thanks
  Alan</p>