DD chen
DD chen

Reputation: 189

Count interval using function cut

I want to count numbers in function of intervals pre-defined. for example:

Vect<-c(2,5,9,11)
interval<-cut(Vect,c(0,3,5,7,12))
interval<-as.data.frame(interval)
count_interval<-interval%>%group_by(interval)%>%summarise(n=n())

count_interval shows me :

  interval     n
  <fct>    <int>
1 (0,3]        1
2 (3,5]        1
3 (7,12]       2

but i want to my code detect that there is an interval missing and add automatically a row like following dataframe:

  interval     n
  <fct>    <int>
1 (0,3]        1
2 (3,5]        1
3 (5,7]        0
4 (7,12]       2

How could I get that? Thanks for you help.

Upvotes: 0

Views: 843

Answers (2)

GKi
GKi

Reputation: 39647

You can use table to count the number in each group.

table(interval)
#interval
# (0,3]  (3,5]  (5,7] (7,12] 
#     1      1      0      2 

and in case you need it as a data.frame cast it to it using as.data.frame.

as.data.frame(table(interval))
#  interval Freq
#1    (0,3]    1
#2    (3,5]    1
#3    (5,7]    0
#4   (7,12]    2

In case you want the maximum per group you can use aggregate:

data<-data.frame(x1=Vect,x2=cut(Vect,c(0,3,5,7,12)))
aggregate(x1~., data, max, drop = FALSE)
#      x2 x1
#1  (0,3]  2
#2  (3,5]  5
#3  (5,7] NA
#4 (7,12] 11

Upvotes: 3

Ronak Shah
Ronak Shah

Reputation: 388807

You can use .drop = FALSE to include factor levels which are empty.

library(dplyr)
interval%>% group_by(interval, .drop = FALSE) %>% summarise(n=n())

# A tibble: 4 x 2
#  interval     n
#  <fct>    <int>
#1 (0,3]        1
#2 (3,5]        1
#3 (5,7]        0
#4 (7,12]       2

Alternately, you can also use count

interval%>% count(interval, .drop = FALSE)

Note that some of these functions are also present in plyr library, so if you have that library loaded these functions might mask them. In such case, restart R and load only dplyr library or explicitly mention dplyr::summarise and dplyr::count.

Upvotes: 1

Related Questions