A Guide to Counting Elements in R

A guide to counting elements in R

This tutorial explains how to count the number of elements that are equal to specific values in vectors and data frames in R.

Counting Elements in a Vector

Suppose we have the following vector x:

x <- c(1, 1, 2, 2, 3, 4, 5, 8, 9, 12, 12, 13)

To find the total number of elements in this vector, we can use one of the following code:

length(x)

#[1] 12

To find the number of elements in this vector that are equal to 2, we can use one of the following two approaches:

length(which(x == 2))

#[1] 2

length(x[x == 2])

#[1] 2

We can also find the number of elements in the vector that are less than or greater than certain values:

length(which(x > 10)) 

#[1] 3

length(which(x <= 3))

#[1] 5

We can also find the number of elements in the vector that are between two values:

length(which(x > 2 & x < 10)) 

#[1] 5

We can also find the number of elements in the vector that are not equal to a certain value:

length(which(x != 2)) 
#[1] 10

We can also find the number of elements in the vector that are equal to NA:

length(which(is.na(x)))

#[1] 0

We could also write a custom function to find the number of elements in a vector that are equal to a certain value:

#define function
count_values <- function(vec, value) {
                  length((which(vec == value)))
                }

#run function to find how many elements in vector x are equal to 2
count_values(x, 2)

#[1] 2

We could also write a custom function to find the percentage of elements in a vector that are equal to a certain value:

#define function
percent_values <- function(vec, value) {
                    100*length((which(vec == value))) / length(vec)
                  }
#run function to find what percentage of elements in vector x are equal to 2 
percent_values(x, 2)

#[1] 16.66667

Counting Elements in a Data Frame

Similar to finding the number of elements in a vector that meet a certain criteria, we can also find the number of elements in a data frame that meet a certain criteria.

For the following examples we will use the built-in R dataset iris:

#view first six rows of iris
head(iris)

#  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#1          5.1         3.5          1.4         0.2  setosa
#2          4.9         3.0          1.4         0.2  setosa
#3          4.7         3.2          1.3         0.2  setosa
#4          4.6         3.1          1.5         0.2  setosa
#5          5.0         3.6          1.4         0.2  setosa
#6          5.4         3.9          1.7         0.4  setosa

We can use the following code to find out how many rows have a value of “virginica” for the Species column:

length(which(iris$Species == 'virginica'))

#[1] 50

We can use the following code to find the number of rows in the data frame that have a value of 5 or greater in each of the first four columns of the iris dataset:

apply(iris[ , 1:4], 2, function(x) (length(( which(x >= 5) ))))

#Sepal.Length  Sepal.Width Petal.Length  Petal.Width 
#         128            0           46            0 

We can use the following code to find the number of rows in the data frame that have a value of 5 or greater for Sepal.Length:

length(which(iris$Sepal.Length >= 5))

#[1] 128

Conclusion

Fortunately R makes it easy to count the number of elements equal to, not equal to, less than, and greater than certain values in both vectors and data frames.

Futher Reading:

An Easy Guide to Writing Functions in R (With Examples)
A Guide to apply(), lapply(), sapply(), and tapply() in R

Leave a Reply

Your email address will not be published. Required fields are marked *