2.4 C
London
Friday, December 20, 2024
HomeRDescriptive Statistics in RHow to Use tabulate() Function in R to Count Integer Occurrences

How to Use tabulate() Function in R to Count Integer Occurrences

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

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

Additional Resources

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

How to Use summary() Function in R
How to Use the replicate() Function in R
How to Use match() Function in R

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories