Skip to content

Data Science Tutorials

  • Home
  • R
  • Statistics
  • Course
  • Machine Learning
  • Guest Blog
  • Contact
  • About Us
  • Toggle search form
  • How to get the last value of each group in R
    How to get the last value of each group in R R
  • Cross-validation in Machine Learning
    Cross-validation in Machine Learning Statistics
  • Arrange the rows in a specific sequence in R
    Arrange the rows in a specific sequence in R R
  • Calculate the p-Value from Z-Score in R
    Calculate the p-Value from Z-Score in R R
  • Data Scientist in 2023
    How to Become a Data Scientist in 2023 Machine Learning
  • Autocorrelation and Partial Autocorrelation in Time Series
    Autocorrelation and Partial Autocorrelation in Time Series Statistics
  • Applications of Data Science in Education
    Applications of Data Science in Education Machine Learning
  • How do augmented analytics work
    How do augmented analytics work? R
How to Use “not in” operator in Filter

How to Use “not in” operator in Filter

Posted on June 17June 11 By Jim 1 Comment on How to Use “not in” operator in Filter
Tweet
Share
Share
Pin

How to Use “not in” operator in Filter, To filter for rows in a data frame that is not in a list of values, use the following basic syntax in dplyr.

How to compare variances in R – Data Science Tutorials

df %>%
  filter(!col_name %in% c('value1', 'value2', 'value3', ...))

The examples below demonstrate how to utilize this syntax in practice.

Example 1: Rows that do not have a value in one column are filtered out.

Let’s say we have the following R data frame.

Two Sample Proportions test in R-Complete Guide – Data Science Tutorials

Let’s create a data frame

df <- data.frame(team=c('P1', 'P2', 'P3', 'P4', 'P5', 'P6', 'P7', 'P8'),
                 points=c(110, 120, 80, 16, 105, 185, 112, 112),
                 assists=c(133, 128, 131, 139, 134,55,66,135),
                 rebounds=c(18, 18, 14, 13, 12, 15, 17, 12))

Now we can view the data frame

df
    team points assists rebounds
1   P1    110     133       18
2   P2    120     128       18
3   P3     80     131       14
4   P4     16     139       13
5   P5    105     134       12
6   P6    185      55       15
7   P7    112      66       17
8   P8    112     135       12

The following syntax demonstrates how to search for rows where the team name is not ‘P1’ or ‘P2’.

Get the first value in each group in R? – Data Science Tutorials

Find rows where the team name isn’t ‘P1’ or ‘P2’.

df %>%
  filter(!team %in% c('P1', 'P2'))
   team points assists rebounds
1   P3     80     131       14
2   P4     16     139       13
3   P5    105     134       12
4   P6    185      55       15
5   P7    112      66       17
6   P8    112     135       12

Example 2: Filter for rows that don’t have a value in more than one column

The following syntax demonstrates how to filter for rows with a team name that does not equal ‘P1’ and a position that does not equal ‘P3’.

Change ggplot2 Theme Color in R- Data Science Tutorials

filter for rows with a team name other than ‘P1’ and a position other than ‘P3’.

df <- data.frame(team=c('P1', 'P2', 'P3', 'P4', 'P5', 'P6', 'P7', 'P8'),
                 points=c('A', 'A', 'B', 'B', 'C', 'C', 'C', 'D'),
                 assists=c(133, 128, 131, 139, 134,55,66,135),
                 rebounds=c(18, 18, 14, 13, 12, 15, 17, 12))
df
   team points assists rebounds
1   P1      A     133       18
2   P2      A     128       18
3   P3      B     131       14
4   P4      B     139       13
5   P5      C     134       12
6   P6      C      55       15
7   P7      C      66       17
8   P8      D     135       12
df %>%
  filter(!team %in% c('P1') & !points %in% c('D'))
   team points assists rebounds
1   P2      A     128       18
2   P3      B     131       14
3   P4      B     139       13
4   P5      C     134       12
5   P6      C      55       15
6   P7      C      66       17

Check your inbox or spam folder to confirm your subscription.

Tweet
Share
Share
Pin
R Tags:dplyr

Post navigation

Previous Post: Filter Using Multiple Conditions in R
Next Post: How to Join Data Frames for different column names in R

Related Posts

  • How to use image function in R
    How to use the image function in R R
  • droplevels in R with examples
    droplevels in R with examples R
  • How to compare the performance of different algorithms in R
    How to compare the performance of different algorithms in R? R
  • How to Use Mutate function in R
    How to Use Mutate function in R R
  • How to Use Bold Font in
    How to Use Bold Font in R with Examples R
  • How to Add Superscripts and Subscripts to Plots in R?, The basic syntax for adding superscripts or subscripts to charts in R is as follows:
    How to Add Superscripts and Subscripts to Plots in R? R

Comment (1) on “How to Use “not in” operator in Filter”

  1. Péter Jeszenszky says:
    October 16 at 3:53 am

    Another solution is to write a ‘not in’ operator, negating the %in% operator and then use it as you would use %in%

    `%!in%` <- Negate(`%in%`)

    Reply

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
  • Find the Maximum Value by Group in R
    Find the Maximum Value by Group in R R
  • Hypothesis Testing in R
    Hypothesis Testing in R R
  • Select the First Row by Group in R
    Select the First Row by Group in R R
  • Top Data Modeling Tools for 2023
    Top Data Modeling Tools for 2023 Machine Learning
  • How to Find Unmatched Records in R
    How to Find Unmatched Records in R R
  • How to Find Optimal Clusters in R, K-means clustering is one of the most widely used clustering techniques in machine learning.
    How to Find Optimal Clusters in R? R
  • Best Data Science YouTube Tutorials
    Best Data Science YouTube Tutorials Free to Learn Course
  • How to handle Imbalanced Data
    How to handle Imbalanced Data? R

Copyright © 2023 Data Science Tutorials.

Powered by PressBook News WordPress theme