Skip to content

Data Science Tutorials

  • Home
  • R
  • Statistics
  • Course
  • Machine Learning
  • Guest Blog
  • Contact
  • About Us
  • Toggle search form
  • Filter Using Multiple Conditions in R
    Filter Using Multiple Conditions in R R
  • How to Use Spread Function in R
    How to Use Spread Function in R?-tidyr Part1 R
  • Dealing Missing values in R
    Dealing With Missing values in R R
  • How to Recode Values in R
    How to Recode Values in R R
  • Predictive Modeling and Data Science
    Predictive Modeling and Data Science Machine Learning
  • How to Use Bold Font in
    How to Use Bold Font in R with Examples R
  • Autocorrelation and Partial Autocorrelation in Time Series
    Autocorrelation and Partial Autocorrelation in Time Series Statistics
  • Error in sum(List) : invalid 'type' (list) of argument
    Error in sum(List) : invalid ‘type’ (list) of argument R
How to Count Distinct Values in R

How to Count Distinct Values in R

Posted on June 7June 4 By Jim No Comments on How to Count Distinct Values in R
Tweet
Share
Share
Pin

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.

Check your inbox or spam folder to confirm your subscription.

Tweet
Share
Share
Pin
R Tags:dplyr

Post navigation

Previous Post: Count Observations by Group in R
Next Post: Cumulative Sum calculation in R

Related Posts

  • Replace NA with Zero in R
    Replace NA with Zero in R R
  • How to Scale Only Numeric Columns in R
    How to Scale Only Numeric Columns in R R
  • How to Find Quartiles in R
    How to Find Quartiles in R? R
  • droplevels in R with examples
    droplevels in R with examples R
  • Error: Can't rename columns that don't exist
    Can’t rename columns that don’t exist R
  • How to Use Spread Function in R
    How to Use Spread Function in R?-tidyr Part1 R

Leave a Reply Cancel reply

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

  • About Us
  • Contact
  • Disclaimer
  • Guest Blog
  • Privacy Policy
  • YouTube
  • Twitter
  • Facebook
  • Defensive Programming Strategies in R
  • Plot categorical data in R
  • Top Data Modeling Tools for 2023
  • Ogive Graph in R
  • Is R or Python Better for Data Science in Bangalore

Check your inbox or spam folder to confirm your subscription.

  • Data Scientist Career Path Map in Finance
  • Is Python the ideal language for machine learning
  • Convert character string to name class object
  • How to play sound at end of R Script
  • Pattern Searching in R
  • How to Standardize Data in R
    How to Standardize Data in R? R
  • R Percentage by Group Calculation
    R Percentage by Group Calculation R
  • Boosting in Machine Learning
    Boosting in Machine Learning:-A Brief Overview Machine Learning
  • Dealing Missing values in R
    Dealing With Missing values in R R
  • Best Online Course For Statistics
    Free Best Online Course For Statistics Course
  • How to create a ggalluvial plot in r
    How to create a ggalluvial plot in R? R
  • Best GGPlot Themes
    Best GGPlot Themes You Should Know R
  • OLS Regression in R
    OLS Regression in R R

Copyright © 2023 Data Science Tutorials.

Powered by PressBook News WordPress theme