Count the Frequency of elements in a Numeric Vector – tabulate() Function
Last Updated :
05 Jun, 2020
tabulate()
function in R Language is used to count the frequency of occurrence of a element in the vector. This function checks for each element in the vector and returns the number of times it occurs in the vector. It will create a vector of the length of the maximum element present in the vector.
Syntax: tabulate(x, nbins)
Parameters:
x: Numeric Vector
nbins: to control length of output vector
Example 1:
x1 < - c( 3 , 5 , 3 , 7 , 9 , 4 , 6 )
x2 < - c( - 1 , - 4 , 2.4 , 6 , - 7 )
tabulate(x1)
tabulate(x2)
|
Output:
[1] 0 0 2 1 1 1 1 0 1
[1] 0 1 0 0 0 1
Here, in the above code, the tabulate() function has ignored the negative values in the second vector, because it works only on numbers which are positive integers.
Example 2:
x1 < - c( 3 , 5 , 3 , 7 , 9 , 4 , 6 )
x2 < - c( - 1 , - 4 , 2.6 , 6 , - 7 , 35 )
tabulate(x1, nbins = 4 )
tabulate(x2, nbins = 8 )
|
Output:
[1] 0 0 2 1
[1] 0 1 0 0 0 1 0 0
Here, in the above code, the length of the output vector is limited by the nbins passed as argument.
Share your thoughts in the comments
Please Login to comment...