- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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.

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)))

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

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)))

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

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)))

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

- Related Questions & Answers
- How to find the sum based on a categorical variable in an R data frame?
- How to find the mean based on single group value in an R data frame?
- How to find the ID wise frequency in an R data frame?
- How to remove rows from an R data frame based on frequency of values in grouping column?
- Find the column and row names in the R data frame based on condition.
- How to find the frequency of each value in an R data frame?
- How to subset an R data frame based on small letters?
- How to find the frequency for all columns based on a condition in R?
- How to select data frame columns based on their class in R?
- How to find the minimum for each row based on few columns in an R data frame?
- How to find the row-wise frequency of zeros in an R data frame?
- How to find the frequency table for factor columns in an R data frame?
- How to find the frequency of a particular string in a column based on another column in an R data frame using dplyr package?
- Find the mean of multiple columns based on multiple grouping columns in R data frame.
- How to subset a data frame based on a vector values in R?

Advertisements