# Compound dataframe filtering with negated boolean expression

**URL:** https://discourse.julialang.org/t/compound-dataframe-filtering-with-negated-boolean-expression/24877
**Category:** General Usage
**Tags:** question, dataframes
**Created:** [June 3, 2019, 8:33am UTC](https://discourse.julialang.org/t/compound-dataframe-filtering-with-negated-boolean-expression/24877 "2019-06-03T08:33:07Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![notpeerreviewed](https://avatars.discourse-cdn.com/v4/letter/n/a9adbd/32.png) [@notpeerreviewed](https://discourse.julialang.org/u/notpeerreviewed)
#### Post date: [June 3, 2019, 8:33am UTC](https://discourse.julialang.org/t/compound-dataframe-filtering-with-negated-boolean-expression/24877/1 "2019-06-03T08:33:07Z")

</div>

I’m trying to reproduce a compound filter but it involves negation of a two part specification.

I’d normally do something like this in R, using dplyr

`filter(!(Fuel == "Gas" & Field == "Maari"))`

In simple terms, I want to be able to filter my dataframe to exclude all instances where the combination of Fuel == Gas, and Field == Maari. I have been able to get a single negated boolean to work, but I can’t seem to get the negated compound expression to evaluate correctly.

I’ve tried this expression, and a few variations, but I keep getting an error saying there is no method matching !(::BitArray{1}). If I remove the ! then the expression returns the expected filtered result.

```julia
test[!((test.Fuel .== "Gas") .& (test.Field .== Symbol("Maari"))), :]

```

Can someone suggest how I might correctly negate the expression above?

Cheers

Jeff

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [June 3, 2019, 8:37am UTC](https://discourse.julialang.org/t/compound-dataframe-filtering-with-negated-boolean-expression/24877/2 "2019-06-03T08:37:32Z")

</div>

I think you’re looking for `.! ` (I asked a similar question a while ago, search for negating boolean array)

---

<div class="post-metadata">

### Author: ![notpeerreviewed](https://avatars.discourse-cdn.com/v4/letter/n/a9adbd/32.png) [@notpeerreviewed](https://discourse.julialang.org/u/notpeerreviewed)
#### Post date: [June 3, 2019, 8:40am UTC](https://discourse.julialang.org/t/compound-dataframe-filtering-with-negated-boolean-expression/24877/3 "2019-06-03T08:40:37Z")

</div>

You’re a champion! I can’t believe I didn’t think to try that.

It is quite a change moving from R to Julia.

Thanks so much.

Jeff

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [June 3, 2019, 8:59am UTC](https://discourse.julialang.org/t/compound-dataframe-filtering-with-negated-boolean-expression/24877/4 "2019-06-03T08:59:37Z")

</div>

Glad that worked! It is and it isn’t to me - I’d say coming from Python is easier, but most often the issues you’ll experience are method errors down to using an inappropriate type. For me in R I seem to suffer from the same problems, albeit they are harder to diagnose as the type information is not great, different things have different indexing patterns eg.  
I guess coming to a new language error messages are always cryptic to some extent, but having used both R and Julia in parallel over the past few months to me there’s no comparison in what’s easier to debug

---

<div class="post-metadata">

### Author: ![Iulian.Cioarca](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iulian.cioarca/32/30166_2.png) [@Iulian.Cioarca](https://discourse.julialang.org/u/Iulian.Cioarca)
#### Post date: [July 29, 2022, 12:25pm UTC](https://discourse.julialang.org/t/compound-dataframe-filtering-with-negated-boolean-expression/24877/5 "2022-07-29T12:25:29Z")

</div>

I have a similar usecase. Let’s take:

```julia
df=DataFrame(x=[1,2,3])

```

I tried to use: `subset(df, "x"=>x -> x.==1)` in order to make a simple filter.  
How can I modify the logic to get as subset a dataframe containing the values 1 and 2?  
I tried `subset(df, "x"=>x -> x.==[1,2])` but it gives an error. I also have to mention that the number of values I want to filter to is arbitrary, not necessarily 2 as in this example.

---

<div class="post-metadata">

### Author: ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)
#### Post date: [July 29, 2022, 12:50pm UTC](https://discourse.julialang.org/t/compound-dataframe-filtering-with-negated-boolean-expression/24877/6 "2022-07-29T12:50:47Z")

</div>

try with this

```julia

df=DataFrame(x=rand(1:10,10))

this=Set([1,2,3])

subset(df, :x=>ByRow(x->x∈this))
subset(df, :x=>x->x .∈ Ref(this))

```
