How can a COUNTIF function be performed in R?

The COUNTIF function in R is a useful tool for counting the number of cells in a data set that meet a specific criteria. To perform this function, the user must first specify the range of cells to be evaluated and then provide the criteria to be matched. This can be done using the “countif” function, which takes two arguments: the range and the criteria. The output of the function is the number of cells that meet the specified criteria within the given range. This function is commonly used in data analysis and can help users quickly and accurately determine the frequency of certain values in a dataset.

Perform a COUNTIF Function in R


Often you may be interested in only counting the number of rows in an R data frame that meet some criteria. Fortunately this is easy to do using the following basic syntax:

sum(df$column == value, na.rm=TRUE)

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

#create data frame
data <- data.frame(team=c('Mavs', 'Mavs', 'Spurs', 'Spurs', 'Lakers'),
                   points=c(14, NA, 8, 17, 22),
                   rebounds=c(8, 5, 5, 9, 12))

#view data frame
data

    team points rebounds
1   Mavs     14        8
2   Mavs     NA        5
3  Spurs      8        5
4  Spurs     17        9
5 Lakers     22       12

Example 1: Count Rows Equal to Some Value

The following code shows how to count the number of rows where the team name is equal to “Mavs”:

sum(data$team == 'Mavs')

[1] 2

The following code shows how to count the number of rows where the team name is equal to “Mavs” or “Lakers”:

sum(data$team == 'Mavs' | data$team == 'Lakers')

[1] 3

The following code shows how to count the number of rows where the team name is not equal to “Lakers”:

sum(data$team != 'Lakers')

[1] 4

Example 2: Count Rows Greater or Equal to Some Value

The following code shows how to count the number of rows where points is greater than 10:

sum(data$points > 10, na.rm=TRUE)

[1] 3

The following code shows how to count the number of rows where rebounds is less than or equal to 9:

sum(data$rebounds <= 9, na.rm=TRUE)

[1] 4

Example 3: Count Rows Between Two Values

sum(data$points > 10 & data$points < 20, na.rm=TRUE)

[1] 2

The following code shows how to count the number of rows where rebounds is between 8 and 10:

sum(data$rebounds > 8 & data$rebounds < 10, na.rm=TRUE)

[1] 1

Additional Resources

How to Count Observations by Group in R
How to Group & Summarize Data in R

x