How to Count Distinct Values in R?, using the n_distinct() function from dplyr, you can count the number of distinct values in an R data frame using one of the following methods.
With the given data frame, the following examples explain how to apply each of these approaches in practice.
Hypothesis Testing Examples-Quick Overview – Data Science Tutorials
How to Count Distinct Values in R
Let’s make a data frame
df <- data.frame(team=c('A', 'A', 'A', 'A', 'B', 'B', 'B', 'B'), Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â points=c(106, 106, 108, 110, 209, 209, 122, 212), Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â assists=c(203, 206, 204, 202, 24, 25, 125, 119)) df
team points assists 1Â Â Â AÂ Â Â 106Â Â Â Â 203 2Â Â Â AÂ Â Â 106Â Â Â Â 206 3Â Â Â AÂ Â Â 108Â Â Â Â 204 4Â Â Â AÂ Â Â 110Â Â Â Â 202 5Â Â Â BÂ Â Â 209Â Â Â Â Â 24 6Â Â Â BÂ Â Â 209Â Â Â Â Â 25 7Â Â Â BÂ Â Â 122Â Â Â Â 125 8Â Â Â BÂ Â Â 212Â Â Â Â 119
Approach 1: Count Distinct Values in One Column
The following code demonstrates how to count the number of distinct values in the ‘team’ column using n distinct().
What is Ad Hoc Analysis? – Data Science Tutorials
count the number of distinct values in the ‘team’ column
library(dplyr) n_distinct(df$team) [1] 2
In the ‘team’ column, there are two separate values.
Approach 2: Count Distinct Values in All Columns
The following code demonstrates how to count the number of unique values in each column of the data frame using the sapply() and n distinct() functions.
count the number of distinct values in each column
sapply(df, function(x) n_distinct(x))
team points assists      2      6      8
We can observe the following from the output:
In the ‘team’ column, there are two separate values.
Arrange the rows in a specific sequence in R – Data Science Tutorials
In the ‘points’ column, there are 6 different values.
The ‘assists’ column has 8 different values.
Approach 3: Count Distinct Values by Group
The following code demonstrates how to count the number of distinct values by group using the n distinct() function.
count the number of different ‘points’ values by ‘team’
df %>% Â group_by(team) %>% Â summarize(distinct_points = n_distinct(points))
team distinct_points  <chr>          <int> 1 A                  3 2 B                  3
We can observe the following from the output:
For team A, there are three different point values.
How to perform One-Sample Wilcoxon Signed Rank Test in R? – Data Science Tutorials
For team B, there are three different point values.