# How to find the frequency based on intervals in R data frame?

R ProgrammingServer Side ProgrammingProgramming

To create intervals, we can use cut function with seq function and if we want to find the frequency based on these intervals then we just need to use table function along with cut function. We need to properly define the values for interval inside cut function. To understand how it can be done, check out the below Examples.

## Example 1

Following snippet creates a sample data frame −

x<-rpois(20,20)
df1<-data.frame(x)
df1

The following dataframe is created

    x
1  25
2  18
3  20
4  18
5  24
6  23
7  23
8  24
9  17
10 17
11 34
12 21
13 29
14 21
15 27
16 22
17 17
18 22
19 28
20 22

To find the frequency based on intervals for column x in df1 on the above created data frame, add the following code to the above snippet −

x<-rpois(20,20)
df1<-data.frame(x)

## Output

If you execute all the above given snippets as a single program, it generates the following Output −

(0,20] (20,40] (40,60] (60,80] (80,100]
3       4       6       6        1

## Example 3

Following snippet creates a sample data frame −

z<-sample(101:500,20)
df3<-data.frame(z)
df3

The following dataframe is created

    z
1  337
2  331
3  341
4  392
5  259
6  458
7  106
8  188
9  474
10 217
11 169
12 272
13 299
14 198
15 368
16 448
17 157
18 395
19 232
20 323

To find the frequency based on intervals for column z in df3 on the above created data frame, add the following code to the above snippet −

z<-sample(101:500,20)
df3<-data.frame(z)
table(cut(df3\$z,seq(100,600,100)))

## Output

If you execute all the above given snippets as a single program, it generates the following Output −

(100,200] (200,300] (300,400] (400,500] (500,600]
5         5         7         3         0
Published on 03-Nov-2021 05:52:56