Count the Frequency of elements in a Numeric Vector – tabulate() Function

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

# R program to count frequency 
# of elements in a vector
  
# Creating a vector
x1 <- c(3, 5, 3, 7, 9, 4, 6)
x2 <- c(-1, -4, 2.4, 6, -7)
  
# Calling tabulate() function
tabulate(x1)
tabulate(x2)

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

# R program to count frequency 
# of elements in a vector
  
# Creating a vector
x1 <- c(3, 5, 3, 7, 9, 4, 6)
x2 <- c(-1, -4, 2.6, 6, -7, 35)
  
# Calling tabulate() function
tabulate(x1, nbins = 4)
tabulate(x2, nbins = 8)

chevron_right


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.

My Personal Notes arrow_drop_up

Recommended Posts:


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.