Skip to content

Data Science Tutorials

For Data Science Learners

  • How to Turn Off Scientific Notation in R
    How to Turn Off Scientific Notation in R? R
  • Best AI and Machine Learning Courses
    Best AI and Machine Learning Courses Machine Learning
  • Control Chart in Quality Control
    Control Chart in Quality Control-Quick Guide Statistics
  • Subset rows based on their integer locations
    Subset rows based on their integer locations-slice in R R
  • How to Group and Summarize Data in R
    How to Group and Summarize Data in R R
  • Return the corresponding value of Cauchy density in R
    Return the corresponding value of Cauchy density in R R
  • best books about data analytics
    Best Books About Data Analytics Course
  • Cross-validation in Machine Learning
    Cross-validation in Machine Learning Statistics
Filtering for Unique Values

Filtering for Unique Values in R- Using the dplyr

Posted on June 12June 11 By Admin No Comments on Filtering for Unique Values in R- Using the dplyr

Filtering for Unique Values in R, Using the dplyr package in R, you may filter for unique values in a data frame using the following methods.

Method 1: In one column, filter for unique values.

df %>% distinct(var1)

Method 2: Filtering for Unique Values in Multiple Columns

df %>% distinct(var1, var2)

Method 3: In all columns, filter for unique values.

df %>% distinct()

With the following data frame in R, the following examples explain how to utilize each method in practice.

Arrange Data by Month in R with example – Data Science Tutorials

create a data frame

df <- data.frame(team=c('X', 'X', 'X', 'X', 'Y', 'Y', 'Y', 'Y'),
                 rebounds =c('8', '6', '5', '4', '3', '8', '9', '5'),
                 points=c(107, 207, 208, 211, 213, 215, 219, 313))

Now we can view the data frame

df
   team rebounds points
1    X        8    107
2    X        6    207
3    X        5    208
4    X        4    211
5    Y        3    213
6    Y        8    215
7    Y        9    219
8    Y        5    313

Example 1: Column Filter for Unique Values

To filter for unique values in just the team column, we can use the following code.

Rejection Region in Hypothesis Testing – Data Science Tutorials

library(dplyr)

In the team column, only unique values should be selected.

df %>% distinct(team)
  team
1    X
2    Y

It’s worth noting that just the team column’s unique values are returned.

Example 2: Find Unique Values in Multiple Columns Using a Filter

To filter for unique values in the team and points columns, we can use the following code:

library(dplyr)

in the team and points columns, select unique values

df %>% distinct(team, points)
  team points
1    X    107
2    X    207
3    X    208
4    X    211
5    Y    213
6    Y    215
7    Y    219
8    Y    313

It’s worth noting that just the team and points columns’ unique values are returned.

Best Books to Learn R Programming – Data Science Tutorials

Example 3: Filter all columns for unique values

To filter for unique values across all columns in the data frame, we can use the following code.

library(dplyr)

choose unique values in all columns

df %>% distinct()
   team rebounds points
1    X        8    107
2    X        6    207
3    X        5    208
4    X        4    211
5    Y        3    213
6    Y        8    215
7    Y        9    219
8    Y        5    313

It’s worth noting that the unique values from each of the three columns are returned.

Check your inbox or spam folder to confirm your subscription.

R Tags:dplyr

Post navigation

Previous Post: Crosstab calculation in R
Next Post: What Is the Best Way to Filter by Date in R?

Related Posts

  • Arrange Data by Month in R
    Arrange Data by Month in R with example R
  • sorting in r
    Sorting in r: sort, order & rank R Functions R
  • How to convert characters from upper to lower case in R
    How to convert characters from upper to lower case in R? R
  • Two Sample Proportions test in R
    Two Sample Proportions test in R-Complete Guide R
  • Confidence Intervals in R
    Confidence Intervals in R R
  • Extract certain rows of data set in R R

Leave a Reply Cancel reply

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

  • Best Prompt Engineering Books
  • Understanding Machine Learning and Data Science
  • Best Git Books
  • Top 5 Books to Learn Data Engineering
  • Mastering R Programming for Data Science: Tips and Tricks
  • About Us
  • Contact
  • Disclaimer
  • Privacy Policy

https://www.r-bloggers.com

  • YouTube
  • Twitter
  • Facebook
  • Course
  • Excel
  • Machine Learning
  • Opensesame
  • R
  • Statistics

Check your inbox or spam folder to confirm your subscription.

  • Detecting and Dealing with Outliers
    Detecting and Dealing with Outliers: First Step R
  • Two Sample Proportions test in R
    Two Sample Proportions test in R-Complete Guide R
  • How to create contingency tables in R
    How to create contingency tables in R? R
  • Load Multiple Packages in R
    Load Multiple Packages in R R
  • Multiple Plots to PDF in R R
  • Linear Interpolation in R
    Linear Interpolation in R-approx R
  • learn Hadoop for Data Science
    Learn Hadoop for Data Science Machine Learning
  • How to Visualize PCA Results in R
    How to Visualize PCA Results in R R

Privacy Policy

Copyright © 2025 Data Science Tutorials.

Powered by PressBook News WordPress theme