How can I count the number of occurrences in columns in R?

How can I count the number of occurrences in columns in R?

Counting the number of occurrences in columns is a common task in data analysis and can be easily done in R. To do so, one can use the “table()” function, which creates a frequency table for categorical data. This function takes in a vector or list of values and returns a table with the number of occurrences for each unique value. By applying this function to each column of the data frame, one can easily count the number of occurrences in each column. Additionally, the “sum()” function can be used to calculate the total number of occurrences in a column by summing up the values in the frequency table. Overall, these functions provide a simple and efficient way to count the number of occurrences in columns in R.

Count Number of Occurrences in Columns in R


You can use the following syntax in R to count the number of occurrences of certain values in columns of a data frame:

#count number of occurrences of each value in column
table(df$column_name)

#count number of occurrences of each value (including NA values) in column
table(df$column_name, useNA = 'always')

#count number of occurrences of specific value
length(which(df$column_name==value))

The following examples show how to use this syntax in practice with the following data frame:

#create data frame
df <- data.frame(player=c('A', 'B', 'C', 'D', 'E', 'F'),
                 team=c('Mavs', 'Mavs', 'Suns', 'Nets', 'Nets', 'Nets'),
                 points=c(20, 22, 26, 30, 30, NA))

#view data frame
df

  player team points
1      A Mavs     20
2      B Mavs     22
3      C Suns     26
4      D Nets     30
5      E Nets     30
6      F Nets     NA

Example 1: Count Occurrences of Values in Column

The following code shows how to count the number of occurrences of each value in the ‘team’ column:

#count number of occurrences of each team
table(df$team)

Mavs Nets Suns 
   2    3    1 

This tells us:

  • The team name ‘Mavs’ appears 2 times.
  • The team name ‘Nets’ appears 3 times.
  • The team name ‘Suns’ appears 1 time.

Example 2: Count Occurrences of Values in Column (Including NA Values)

The following code shows how to count the number of occurrences of each value (including NA values) in the ‘points’ column:

#count number of occurrences of each value in 'points', including NA occurrences
table(df$points, useNA = 'always')

  20   22   26   30 <NA>
   1    1    1    2    1 

This tells us:

  • The value 20 appears 1 time.
  • The value 22 appears 1 time.
  • The value 26 appears 1 time.
  • The value 30 appears 2 times.
  • The value NA (missing value) appears 1 time.

Example 3: Count Occurrences of Specific Value in Column

The following code shows how to count the number of occurrences of the value 30 in the ‘points’ column:

#count number of occurrences of the value 30 in 'points' column
length(which(df$points == 30))

[1] 2

You can also use the following syntax to count the number of occurrences of several different values in the ‘points’ column:

#count number of occurrences of the value 30 or 26 in 'points' column
length(which(df$points == 30 | df$points == 26))

[1] 3

This tells us that the value 30 or 26 appear a total of 3 times in the ‘points’ column.

Cite this article

stats writer (2024). How can I count the number of occurrences in columns in R?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-count-the-number-of-occurrences-in-columns-in-r/

stats writer. "How can I count the number of occurrences in columns in R?." PSYCHOLOGICAL SCALES, 2 May. 2024, https://scales.arabpsychology.com/stats/how-can-i-count-the-number-of-occurrences-in-columns-in-r/.

stats writer. "How can I count the number of occurrences in columns in R?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-count-the-number-of-occurrences-in-columns-in-r/.

stats writer (2024) 'How can I count the number of occurrences in columns in R?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-count-the-number-of-occurrences-in-columns-in-r/.

[1] stats writer, "How can I count the number of occurrences in columns in R?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.

stats writer. How can I count the number of occurrences in columns in R?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
PDF
Scroll to Top