Skip to content

Data Science Tutorials

  • Home
  • R
  • Statistics
  • Course
  • Machine Learning
  • Guest Blog
  • Contact
  • About Us
  • Toggle search form
  • How to compare the performance of different algorithms in R
    How to compare the performance of different algorithms in R? R
  • Descriptive statistics vs Inferential statistics
    Descriptive statistics vs Inferential statistics: Guide Statistics
  • Two-Way ANOVA Example in R
    How to perform One-Sample Wilcoxon Signed Rank Test in R? R
  • Get the first value in each group in R
    Get the first value in each group in R? R
  • How do augmented analytics work
    How do augmented analytics work? R
  • droplevels in R with examples
    droplevels in R with examples R
  • Is Data Science a Dying Profession
    Is Data Science a Dying Profession? R
  • Change ggplot2 Theme Color in R
    Change ggplot2 Theme Color in R ggthemr Package R
Filtering for Unique Values

Filtering for Unique Values in R- Using the dplyr

Posted on June 12June 11 By Jim No Comments on Filtering for Unique Values in R- Using the dplyr
Tweet
Share
Share
Pin

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.

Tweet
Share
Share
Pin
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

  • Error in solve.default(mat)  Lapack routine dgesv system is exactly singular
    Error in solve.default(mat) :  Lapack routine dgesv: system is exactly singular: U[2,2] = 0 R
  • test for normal distribution in r
    Test for Normal Distribution in R-Quick Guide R
  • How to Perform Bootstrapping in R
    How to Perform Bootstrapping in R R
  • Get the first value in each group in R
    Get the first value in each group in R? R
  • How to Create a Frequency Table by Group in R
    How to Create a Frequency Table by Group in R? R
  • Extract patterns in R
    Extract patterns in R? 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
  • Top 7 Skills Required to Become a Data Scientist
  • Learn Hadoop for Data Science
  • How Do Online Criminals Acquire Sensitive Data
  • Top Reasons To Learn R in 2023
  • Linear Interpolation in R-approx

Check your inbox or spam folder to confirm your subscription.

 https://www.r-bloggers.com
  • pheatmap function in R
    The pheatmap function in R R
  • Credit Card Fraud detection in R
    Credit Card Fraud Detection in R R
  • How to Calculate Ratios in R
    How to Calculate Ratios in R R
  • How to use image function in R
    How to use the image function in R R
  • Find the Maximum Value by Group in R
    Find the Maximum Value by Group in R R
  • How to add columns to a data frame in R
    How to add columns to a data frame in R R
  • How to perform MANOVA test in R
    How to perform the MANOVA test in R? R
  • gganatogram Plot in R
    How to create Anatogram plot in R R

Copyright © 2023 Data Science Tutorials.

Powered by PressBook News WordPress theme