Skip to content

Data Science Tutorials

  • Home
  • R
  • Statistics
  • Course
  • Contact
  • About Us
  • Toggle search form
  • how to draw heatmap in r
    How to draw heatmap in r: Quick and Easy way R
  • How to Calculate Lag by Group in R
    How to Calculate Lag by Group in R? R
  • Subset rows based on their integer locations
    Subset rows based on their integer locations-slice in R R
  • How to Join Multiple Data Frames in R
    How to Join Multiple Data Frames in R R
  • Error: Can't rename columns that don't exist
    Can’t rename columns that don’t exist R
  • Comparing group means in R
    One way ANOVA Example in R-Quick Guide R
  • Detecting and Dealing with Outliers
    Detecting and Dealing with Outliers: First Step R
  • How to perform MANOVA test in R
    How to perform the MANOVA test in R? 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 get the last value of each group in R
    How to get the last value of each group in R R
  • How to Join Data Frames for different column names in R
    How to Join Data Frames for different column names in R R
  • How to Use Gather Function in R
    How to Use Gather Function in R?-tidyr Part2 R
  • Add new calculated variables to a data frame and drop all existing variables
    Add new calculated variables to a data frame and drop all existing variables R
  • How to do Conditional Mutate in R
    How to do Conditional Mutate in R? R

Leave a Reply Cancel reply

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




  • About Us
  • Contact
  • Disclaimer
  • Privacy Policy
  • YouTube
  • Twitter
  • Facebook
  • Is Data Science a Dying Profession?
  • How to Label Outliers in Boxplots in ggplot2?
  • Best Books About Data Analytics
  • How to Scale Only Numeric Columns in R
  • Best Books to Learn Statistics for Data Science

Check your inbox or spam folder to confirm your subscription.




 https://www.r-bloggers.com
  • similarity measure between two populations
    Similarity Measure Between Two Populations-Brunner Munzel Test Statistics
  • best books about data analytics
    Best Books to learn Tensorflow Course
  • How to Use the Multinomial Distribution in R
    How to Use the Multinomial Distribution in R? R
  • How to Group and Summarize Data in R
    How to Group and Summarize Data in R R
  • Filter Using Multiple Conditions in R
    Filter Using Multiple Conditions in R R
  • Arrange the rows in a specific sequence in R
    Arrange the rows in a specific sequence in R R
  • Find the Maximum Value by Group in R
    Find the Maximum Value by Group in R R
  • How to perform MANOVA test in R
    How to perform the MANOVA test in R? R

Copyright © 2022 Data Science Tutorials.

Powered by PressBook News WordPress theme