Get the frequency based on intervals in R on sample data

A random sample can also be generated between a specified set of numbers with a fixed length associated with it. It saves us from creating the sample list manually. It returns a vector of values as the output.

Syntax: sample(range , length)

Arguments: 

  • range – The values within which the sample values will be taken 
  • length – The length of the sample vector

The intervals are then again created with the help of cut() method as illustrated earlier. It is user dependent to choose the size of bins. Small the size of bins for a large dataset is less preferable, however we can use it with ease with a larger data set.

R




#creating a sample vector of values
vec <- sample(11:50,20)
 
#creating a data frame
data_frame <- data.frame(col1 = vec)
print("Original Data")
print(data_frame)
 
#creating intervals between 10 to 50 with a gap of 10 each
interval_table <- table(cut(data_frame$col1,seq(10,50,10)))
print("Data in Intervals")
print(interval_table)


Output:

 

Frequency Table With Intervals in R

A data frame in R may contain discrete data in the form of integer or floating point values. The values may be distinct or repeating in nature. The frequency table for discrete data can be easily created with the help of the table() method in R, which returns the values along with their respective counts. 

Similar Reads

Get a frequency Table With Intervals in R

The interval table can be easily created using the cut() method. The cut method has the following syntax :...

Get the frequency based on intervals in R on sample data

...

Get the frequency based on intervals in R on vector data

A random sample can also be generated between a specified set of numbers with a fixed length associated with it. It saves us from creating the sample list manually. It returns a vector of values as the output....

Contact Us