How to use tabulate() function in R to count integer occurrences?

The tabulate() function in R is used to count the number of occurrences of integers in a given vector. It takes the vector as an argument and returns a vector of the same length containing the counts for each integer in the original vector. This is useful for quickly summarizing data and can be used to produce a frequency table or histogram.


The tabulate() function in R can be used to count the occurrences of integer values in a vector.

This function uses the following basic syntax:

tabulate(bin, nbins=max(1, bin, na.rm=TRUE))

where:

  • bin: Name of the vector
  • nbins: The number of bins to be used

The following examples show how to use this function in practice.

Example 1: Count Integer Occurrences in Vector

The following code shows how to use the tabulate() function to count the occurrences of integers in a given vector:

#create vector of data values
data <- c(1, 1, 1, 2, 3, 3, 3, 4, 7, 8)

#count occurrences of integers in vector
tabulate(data)

[1] 3 1 3 1 0 0 1 1

By default, the tabulate() function uses 1 as the minimum integer value and displays the occurrences of each successive integer in the vector.

For example:

  • The integer 1 occurs 3 times in the vector.
  • The integer 2 occurs 1 time in the vector.
  • The integer 3 occurs 3 times in the vector.
  • The integer 4 occurs 1 time in the vector.
  • The integer 5 occurs 0 times in the vector.

And so on.

Note that if you use the nbins argument, you simply limit the number of integers that the tabulate() function counts:

#count occurrences of integers but limit output to 5
tabulate(data, nbins=5)

[1] 3 1 3 1 0

Example 2: Count Integer Occurrences in Vector with Decimals

If we use the tabulate() function with a vector that contains decimals, the function will simply tell us how often each integer value occurs:

#create vector of data values with decimals
data <- c(1.2, 1.4, 1.7, 2, 3.1, 3.5)

#count occurrences of integers
tabulate(data)

[1] 3 1 2

From the output we can see:

  • The integer value 1 occurred 3 times.
  • The integer value 2 occurred 1 time.
  • The integer value 3 occurred 2 times.

Example 3: Count Integer Occurrences in Vector with Negative Values

If we use the tabulate() function with a vector that contains negative values or zeros, the function will simply ignore the negative values and the zeros:

#create vector with some negative values and zeros
data <- c(-5, -5, -2, 0, 1, 1, 2, 4)

#count occurrences of integers
tabulate(data)

[1] 2 1 0 1

From the output we can see:

  • The integer value 1 occurred 2 times.
  • The integer value 2 occurred 1 time.
  • The integer value 3 occurred 0 times.
  • The integer value 4 occurred 1 time.

An Alternative to Tabulate: The table() Function

If you’d like to count the occurrence of every value in a vector, it’s better to use the table() function:

#create vector with a variety of numbers
data <- c(-5, -5, -2, 0, 1, 1, 2.5, 4)

#count occurrences of each unique value in vector
table(data)

data
 -5  -2   0   1 2.5   4 
  2   1   1   2   1   1 

From the output we can see:

  • The value -5 occurred 2 times.
  • The value -2 occurred 1 time.
  • The value 0 occurred 1 time.
  • The value 1 occurred 2 times.
  • The value 2.5 occurred 1 time.
  • The value 4 occurred 1 time.

Notice that the table() function counts the occurrence of every value, not just the integer values.

The following tutorials explain how to use other common functions in R:

x