title: dplyr and intervals: count observations and sum data without using loops
body: <p>I would like to use the R dplyr package to calculate the following interval related questions without using loops:</p>
  
  <ol>
  <li>I would like to count the observations in each interval (absolute and relative interval endpoints)</li>
  <li>I would like to sum the observation data in each interval (absolute and relative interval endpoints)</li>
  </ol>
  
  <p>The interval endpoints are from the columns df_abs$interval and df_rel$interval . e.g.</p>
  
  <ol>
  <li>interval: (-inf,-60] </li>
  <li>interval: (-60,-30]</li>
  <li>interval: (-30,0]</li>
  </ol>
  
  <p>The data frames with the data and intervals look like this:</p>
  
  <pre><code>library(dplyr)
  
  # ----------{ data and interval ----------
  df_data &lt;- data.frame(varA = NA,
                        varB = NA, 
                        varC = c(-81.0, -14.3, 29.6, 42.7, 46.4, 57.7, 15.3, 256.3,  20.3, -25.1, -23.1, -17.5))
  
  df_abs &lt;- data.frame(interval = c(-Inf, -60, -30, 0, 30, 60, 100, 200, Inf),
                       count = NA,
                       sum = NA)
  
  df_rel &lt;- data.frame(interval = c(0,5,15,50,75,95,100),
                       count = NA,
                       sum = NA)
  # ---------- data and interval }----------
  
  
  # ----------{ calculation ----------    
  # absolute data frame
  for (i in 1 : nrow(df_abs)-1) {  
    # count observation between interval
    df_abs$count[i+1]  &lt;- summarise(df_data, sum(df_abs$interval[i] &lt; varC &amp;   varC &lt;= df_abs$interval[i+1]))
  
    # sum between interval
    df_abs$sum[i+1]  &lt;- sum(df_data$varC[df_abs$interval[i] &lt; df_data$varC &amp;   df_data$varC &lt;= df_abs$interval[i+1]])  
  }
  
  
  # relative data frame
  df_data_arranged &lt;- df_data %&gt;%
                        arrange(varC) %&gt;%
                        mutate(observationPercent = c(1:nrow(df_data)) * 100/length(df_data$varC))
  
  
  for (i in 1 : nrow(df_rel)-1) {  
    # count observation between interval
    df_rel$count[i+1]  &lt;- summarise(df_data_arranged, sum(df_rel$interval[i] &lt; observationPercent &amp;   observationPercent &lt;= df_rel$interval[i+1])) 
  
    # sum between interval
    df_rel$sum[i+1]  &lt;- sum(df_data_arranged$varC[df_rel$interval[i] &lt; df_data_arranged$observationPercent &amp; df_data_arranged$observationPercent &lt;= df_rel$interval[i+1]]) 
  }    
  # ---------- calculation }----------
  </code></pre>
  
  <p>The answer should look like this:</p>
  
  <pre><code>df_abs &lt;- data.frame(interval = c(-Inf, -60, -30, 0, 30, 60, 100, 200, Inf),
                       count = c(0,1,0,4,3,3,0,0,1),
                       sum = c(0,-81,0,-80,65.2,146.8,0,0,256.3))    
  
  df_rel &lt;- data.frame(interval = c(0,5,15,50,75,95,100),
                       count = c(0,0,1,4,3,2,1),
                       sum = c(0,0,-81,-39.6,92.6,104.1,256.3))
  </code></pre>
  
  <p>As far as I understand the dplyr package, there should be a rather short and straight forward solution for each of the two problems without having to use loops at all.</p>