D1W1TR15
D1W1TR15

Reputation: 99

Dplyr ~ select something, except

So I have the following code:

library (dplyr)
dataset1 <- filter(dataset0, dataset0$type == "black" | dataset0$type == "orange")

What this code does is to add in dataset1 every row of dataset0 that has type = "black" or type = "orange".

What if I want to take everything else EXCEPT orange and black. Is the following correct?

library (dplyr)
dataset1 <- filter(dataset0, dataset0$type != "black" | dataset0$type != "orange")

Thank you in advance.

Upvotes: 2

Views: 11199

Answers (2)

Jessica Beyer
Jessica Beyer

Reputation: 158

You'll have to change the 'or' to an 'and', like so

First, make some fake data

type <- c(rep("black", 5), rep("orange", 5), rep("green", 5))
dataset0 <- as.data.frame(type)

Now subset using & in your logical statement

dataset1 <- filter(dataset0, dataset0$type != "black" & dataset0$type != "orange")

If you don't change the | to &, you'll get back the original data frame (with orange and black both included), like so:

dataset1 <- filter(dataset0, dataset0$type != "black" | dataset0$type != "orange")
dataset1
     type
1   black
2   black
3   black
4   black
5   black
6  orange
7  orange
8  orange
9  orange
10 orange
11  green
12  green
13  green
14  green
15  green

Upvotes: 0

IRTFM
IRTFM

Reputation: 263342

One method could be be:

 dataset1 <- filter(dataset0, !(dataset0$type == "black" | dataset0$type == "orange") )

Your suggestion is incorrect. When you negate a compound expression you need to change the OR's to AND's if you dont negate the entire expression.

dataset1 <- filter(dataset0, dataset0$type != "black" & dataset0$type != "orange")

This has nothing to do with dplyr in particular. It's just basic logic. I also suspect that you should not be including the dataframe name with the "$" operator in your logical expressions. Try:

dataset1 <- filter(dataset0, !(type == "black" | type == "orange") )

Upvotes: 2

Related Questions