Count Number of Observations in R

Manav Narula Feb 25, 2021 Jan 05, 2021
Count Number of Observations in R

In real-life situations, we deal with large sets of data. This may exceed hundreds of observations, and sometimes there may be a need to extract some specific data from the whole.

For such situations, we have a few methods in R, which can aid in counting the total observations of this filtered data. We will work on the following DataFrame in this tutorial.

df <- data.frame( gender = c("M","F","M","M"),
                  age = c(18,19,14,22),
                  stream = c("Arts","Science","Arts","Commerce"))
print(df)

Output:

  gender age   stream
1      M  18     Arts
2      F  19  Science
3      M  14     Arts
4      M  22 Commerce

The first method involves the with() and the sum() functions.

The with() function returns a logical vector based on some expression after applying it to the whole dataset, and the sum() function will return the sum of all the True observations.

The following code snippet will show how this works.

df <- data.frame( gender = c("M","F","M","M"),
                  age = c(18,19,14,22),
                  stream = c("Arts","Science","Arts","Commerce"))
                  
sum(with(df,gender == "M"))
[1] 3

We can also add multiple expressions using the & operator.

sum(with(df,gender == "M" & stream == "Commerce"))
[1] 1

Another method involves the use of the nrow() function, which returns the number of rows in a dataset. We can filter out the required observations from the DataFrame, as shown below:

nrow(df[df$gender == "M",])
[1] 3

Again, we can add multiple expressions as what we do in the with() function.

nrow(df[df$gender == "M" & df$stream == "Commerce",])
[1] 1

We can also use the filer() function provided in the dplyr library. This returns a subset of data based on some condition. The following example explains how:

library(dplyr)
nrow(filter(df,gender == "M"))
[1] 3
nrow(filter(df,gender == "M" & stream == "Commerce"))
[1] 1
Author: Manav Narula
Manav Narula avatar Manav Narula avatar

Manav is a IT Professional who has a lot of experience as a core developer in many live projects. He is an avid learner who enjoys learning new things and sharing his findings whenever possible.

LinkedIn

Related Article - R Data Frame