Skip to content
Related Articles

Related Articles

Frequency Table With Intervals in R

Improve Article
Save Article
  • Last Updated : 19 Sep, 2022
Improve Article
Save Article

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. 

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 :

Syntax: cut ( vec , bins-to-divide)

Arguments :

  • vec – The vector to divide the bins of 
  • bins-to-divide – The number of classes to create the vector.

The in-built seq() method is used to generate a sequence starting from the first argument until the second argument each with a difference from the third argument.

Syntax: seq( st, end, diff )

Here,

st – The starting integer of the sequence
end – The ending integer of the sequence 
diff – The difference between each bin value

The vector specified is then cut into specified bins and the respective counts of each of the intervals are returned by the table() method in R.

R




#creating a data frame
 
data_frame <- data.frame(col1 = c(1,3,5,6,23,6,2,5,7,
                                  16,8,9,36,7,12,1,
                                  6,4,14,23,19,18,
                                   14,2,20,30))
print("Original Data")
print(data_frame)
 
# creating intervals between 1 to 30 with a gap of 5 each
interval_table <- table(cut(data_frame$col1,seq(1,30,5)))
print("Data in Intervals")
print(interval_table)


Output:

Frequency table with intervals in R

 

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:

 

Get the frequency based on intervals in R on vector data

Functions can also be used to generate the vector of integers or string values. The rpois() method in R is used to draw randomly computed poisson density. The rpois method has the following syntax :

Syntax: rpois(num-of-observations, rate=rate )

Arguments: 

  • num-of-observations – Number of observations
  • rate – The rate of events for the distribution

However, since the values are drawn randomly using just the number of observations, instead of manually deciding the bins, we can extract the minimum and maximum values of the returned values in the density vector using the in-built min() and max() methods. 
A sequence can then be generated between these intervals to return accurate results. A frequency table is then drawn using the table() method in R.

R




#using a func to generate values
vec <- rpois(10,30)
 
#creating a data frame
data_frame <- data.frame(col1 = vec)
print("Original Data")
print(data_frame)
 
#getting the min and max intervals in the vec
min<- min(vec)
max <- max(vec)
 
#creating intervals between 10 to 50 with a gap of 10 each
interval_table <- table(cut(data_frame$col1,seq(min,max,2)))
print("Data in Intervals")
print(interval_table)


Output:

 


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!