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