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

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

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

Leave a Reply

Your email address will not be published.