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:
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.
Contact Us