title: Multiple condition if-else using dplyr, custom function, or purrr
body: <p>I have a data frame that has similar structure to the following:</p>
  
  <pre><code>set.seed(123)  
  df&lt;-data_frame(SectionName = rep(letters[1:2], 50),
                 TimeSpentSeconds = sample(0:360, 100, replace = TRUE),
                 Correct = sample(0:1, 100, replace = TRUE))
  </code></pre>
  
  <p>I want summarise this data frame by taking all values of TimeSpentSeconds that fall into certain ranges (less than 30, between 30-60, between 60-90, ..., greater than 180), label the times as those ranges, group them by SectionName, and find the sum of the Correct column so that the resulting data frame looks (something) like this:</p>
  
  <pre><code>    TimeGroup             SectionName Correct
     &lt;fct&gt;                 &lt;chr&gt;         &lt;int&gt;
   1 LessThan30Secs        a                 2
   2 LessThan30Secs        b                 3
   3 30-60 Seconds         a                 4
   4 30-60 Seconds         b                 3
   5 60-90 Seconds         a                 2
   6 60-90 Seconds         b                 3
   7 90-120 Seconds        a                 4
   8 90-120 Seconds        b                 0
   9 120-150 Seconds       a                 4
  10 120-150 Seconds       b                 0
  11 150-180 Seconds       a                 1
  12 150-180 Seconds       b                 2
  13 GreaterThan180Seconds a                11
  14 GreaterThan180Seconds b                11
  </code></pre>
  
  <p>I was able to successfully do this with the following if-else code where I mutated all of the times into a new column with the appropriate label, grouped, and summarised:</p>
  
  <pre><code>x &lt;- c("LessThan30Secs", "30-60 Seconds", "60-90 Seconds","90-120 Seconds", 
             "120-150 Seconds", "150-180 Seconds", "GreaterThan180Seconds") 
  
  df %&gt;% 
  mutate(TimeGroup = if_else(TimeSpentSeconds &gt;= 0 &amp; TimeSpentSeconds &lt;= 30, "LessThan30Secs",
                  if_else(TimeSpentSeconds &gt; 30 &amp; TimeSpentSeconds &lt;= 60, "30-60 Seconds",
                  if_else(TimeSpentSeconds &gt; 60 &amp; TimeSpentSeconds &lt;= 90, "60-90 Seconds",
                  if_else(TimeSpentSeconds &gt; 90 &amp; TimeSpentSeconds &lt;= 120, "90-120 Seconds",
                  if_else(TimeSpentSeconds &gt; 120 &amp; TimeSpentSeconds &lt;= 150, "120-150 Seconds", 
                  if_else(TimeSpentSeconds &gt; 150 &amp; TimeSpentSeconds &lt;= 180, "150-180 Seconds",
                  if_else(TimeSpentSeconds &gt; 180, "GreaterThan180Seconds", "")))))))) %&gt;%
      mutate(TimeGroup = factor(TimeGroup, levels = x)) %&gt;%
      arrange(TimeGroup) %&gt;%
      group_by(TimeGroup, SectionName) %&gt;%
      summarise(Correct = sum(Correct))
  </code></pre>
  
  <p>But, there just has to be a better way to do this. I considered writing a function, but didn't get very far as I'm not great at function writing.</p>
  
  <p>Does anyone have any ideas on a more elegant way to accomplish this same output through a dplyr method I didn't think of, writing a custom function maybe utilizing the purrr package at some point, or some other r function?</p>