Open In App

dplyr::count() Function in R

Last Updated : 17 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The count() function in the dplyr package is used to count the number of occurrences of unique combinations of variables in a data frame. It is particularly useful for generating frequency tables or summarizing categorical data. Here’s a detailed explanation of how to use count() in R Programming Language.

Introduction to count() function in R

The count() function is part of the dplyr package, which is widely used for data manipulation in R. It provides a convenient way to count the occurrences of unique combinations of variables in a data frame.

The primary purpose of count() is to generate frequency tables or summary counts of categorical variables within a dataset. It simplifies the process of counting occurrences and provides a tidy output for further analysis or visualization.

count(data, ..., sort = FALSE)

data: The input data frame.

  • …One or more variables to count occurrences of. These can be unquoted variable names or expressions.
  • sort: A logical value indicating whether to sort the output by frequency. Defaults to FALSE.

Counting Occurrences of a Single Variable

Suppose you have a dataset containing information about students and their grades. You want to count the number of students in each grade level.

R
library(dplyr)
# Sample data frame
students <- data.frame(
  Name = c("Ali", "Boby", "Charles", "David", "Emma", "Frank"),
  Grade = c("A", "B", "B", "C", "A", "B")
)
students
# Count occurrences of each grade
count(students, Grade)

Output:

     Name Grade
1 Ali A
2 Boby B
3 Charles B
4 David C
5 Emma A
6 Frank B
Count occurrences of each grade

Grade n
1 A 2
2 B 3
3 C 1

Counting Occurrences of Multiple Variables

Consider a dataset with information about sales transactions, including the product category and the sales region. You want to count the number of transactions for each product category in each region.

R
# Sample data frame
sales <- data.frame(
  Region = c("East", "West", "East", "West", "East", "West"),
  Category = c("Electronics", "Clothing", "Electronics", "Electronics", "Clothing", 
                  "Electronics")
)
sales 
# Count occurrences of each product category in each region
count(sales, Region, Category)

Output:

  Region    Category
1 East Electronics
2 West Clothing
3 East Electronics
4 West Electronics
5 East Clothing
6 West Electronics
Count occurrences of each product category in each region

Region Category n
1 East Clothing 1
2 East Electronics 2
3 West Clothing 1
4 West Electronics 2

Sorting the Output by Frequency

In some cases, you may want to sort the output by frequency to identify the most common occurrences. Here’s an example of counting occurrences of unique values in a column and sorting the output by frequency.

R
# Sample data frame
letters <- data.frame(
  Letter = c("A", "B", "B", "C", "A", "A", "B", "C", "C", "C")
)
letters
# Count occurrences of each letter and sort by frequency
count(letters, Letter, sort = TRUE)

Output:

   Letter
1 A
2 B
3 B
4 C
5 A
6 A
7 B
8 C
9 C
10 C
Count occurrences of each letter and sort by frequency

Letter n
1 C 4
2 A 3
3 B 3

Counting Missing Values

You can also count missing values (NA) in a dataset using count().

R
# Sample data frame with missing values
data <- data.frame(
  Value1 = c(1, 2, NA, 4, NA),
  Value2 = c(NA, 2, 3, NA, 5)
)
data
# Count missing values in each column
count(data, sum(is.na(Value1)), sum(is.na(Value2)))

Output:

  sum(is.na(Value1)) sum(is.na(Value2)) 
1 2 2

Conclusion

The count() function in the dplyr package is a valuable tool for generating frequency tables and summarizing categorical data in R. By providing a simple and efficient way to count occurrences, count() streamlines the data analysis process and facilitates insights into the distribution of categorical variables within a dataset. Incorporating count() into your data manipulation workflow enhances your ability to explore and understand your data effectively.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads