Flatlay of a business analytics report, keyboard, pen, and smartphone on a wooden desk.

dplyr: Learn how to Utility a “not in” Filter out

Posted on
banner 336x280

You’ll significance please see unadorned syntax in dplyr to clear out for rows in an information body that aren’t in an inventory of values:

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

Refer to examples display how one can significance this syntax in observe.

banner 468x60

Instance 1: Filter out for Rows that Do Now not Include Worth in One Column

Think we have now please see information body in R:

#assemble information body
df <- information.body(staff=c('A', 'A', 'B', 'B', 'C', 'C', 'D', 'D'),
                 place=c('G', 'G', 'F', 'G', 'F', 'C', 'C', 'C'),
                 issues=c(12, 14, 19, 24, 36, 41, 18, 29))

#view information body
df

  staff place issues
1    A        G     12
2    A        G     14
3    B        F     19
4    B        G     24
5    C        F     36
6    C        C     41
7    D        C     18
8    D        C     29

Refer to syntax presentations how one can clear out for rows the place the staff identify isn’t equivalent to ‘A’ or ‘B’:

#clear out for rows the place staff identify isn't 'A' or 'B'
df %>%
  clear out(!staff %in% c('A', 'B'))

  staff place issues
1    C        F     36
2    C        C     41
3    D        C     18
4    D        C     29

Instance 2: Filter out for Rows that Do Now not Include Worth in A couple of Columns

Think we have now please see information body in R:

#assemble information body
df <- information.body(staff=c('A', 'A', 'B', 'B', 'C', 'C', 'D', 'D'),
                 place=c('G', 'G', 'F', 'G', 'F', 'C', 'C', 'C'),
                 issues=c(12, 14, 19, 24, 36, 41, 18, 29))

#view information body
df

  staff place issues
1    A        G     12
2    A        G     14
3    B        F     19
4    B        G     24
5    C        F     36
6    C        C     41
7    D        C     18
8    D        C     29

Refer to syntax presentations how one can clear out for rows the place the staff identify isn’t equivalent to ‘A’ and the place the placement isn’t equivalent to ‘C’:

#clear out for rows the place staff identify isn't 'A' and place isn't 'C'
df %>%
  clear out(!staff %in% c('A') & !place %in% c('C'))

  staff place issues
1    B        F     19
2    B        G     24
3    C        F     36

Spare Assets

Refer to tutorials give an explanation for how one can carry out alternative familiar purposes in dplyr:

Learn how to Take away Rows The use of dplyr
Learn how to Make a selection Columns by means of Index The use of dplyr
Learn how to Filter out Rows that Include a Sure Yarn The use of dplyr

banner 336x280

Leave a Reply

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