- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to find the frequency based on intervals in R data frame?
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) table(cut(df1$x,seq(15,35,5)))
Output
If you execute all the above given snippets as a single program, it generates the following Output −
(15,20] (20,25] (25,30] (30,35] 6 10 3 1
Example 2
Following snippet creates a sample data frame −
y<-sample(0:100,20) df2<-data.frame(y) df2
The following dataframe is created
y 1 65 2 36 3 45 4 52 5 74 6 44 7 14 8 71 9 33 10 18 11 76 12 72 13 4 14 57 15 64 16 34 17 59 18 86 19 47 20 26
To find the frequency based on intervals for column y in df2 on the above created data frame, add the following code to the above snippet −
y<-sample(0:100,20) df2<-data.frame(y) table(cut(df2$y,seq(0,100,20)))
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