How to 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

This tells us that the value 30 appears 2 times in the ‘points’ column.

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.

Additional Resources

How to Sum Specific Columns in R
How to Calculate the Mean of Multiple Columns in R
How to Find the Max Value Across Multiple Columns in R

Leave a Reply

Your email address will not be published.