R and dplyr: group by value ranges

Viewed 778

hi everyone I have a dataframe like this:

    value     count
    <dbl>     <dbl>
 1     1         10
 2     2         20
 3     3         30
 4     4         40
 5     5         50
 6     6         60

I would like to be able to divide my observations into intervals. The first and last interval must include all the observations left out of the range (for example of 2)

      interval     count
         <???>     <dbl>
 1     [<1, 2]        30
 2      [3, 4]        50
 3     [5, >6]       110

Is it possible to do this with dplyr?

1 Answers

You can use cut() to create a grouping variable with which to summarise count.

library(dplyr)

df %>%
  group_by(grp = cut(value, c(-Inf, 2, 4, Inf))) %>%
  summarise(count = sum(count))

# A tibble: 3 x 2
  grp      count
  <fct>    <int>
1 (-Inf,2]    30
2 (2,4]       70
3 (4, Inf]   110
Related