Get Number of Missing Values by Group in R (Example Code)

In this tutorial, I’ll show how to get the number of missing values by group in the R programming language.

Creation of Example Data

data(iris)                           # Load & modify iris data set
iris_NA <- iris[c(1:3, 51:53, 101:103), c(1, 5)]
iris_NA$Sepal.Length[c(2, 4, 6, 7)] <- NA
iris_NA
#     Sepal.Length    Species
# 1            5.1     setosa
# 2             NA     setosa
# 3            4.7     setosa
# 51            NA versicolor
# 52           6.4 versicolor
# 53            NA versicolor
# 101           NA  virginica
# 102          5.8  virginica
# 103          7.1  virginica

Example: Counting Number of NA Values by Group Using aggregate() Function

aggregate(Sepal.Length ~ Species,    # Returning number of NAs by group
          data = iris_NA,
          function(x) {sum(is.na(x))},
          na.action = NULL)
#      Species Sepal.Length
# 1     setosa            1
# 2 versicolor            2
# 3  virginica            1

Related Articles

In the following, you may find some further resources on topics such as data inspection, time objects, and counting.

Leave a Reply

Your email address will not be published.

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top