# How to select multiple items using DataFramesMeta?

**URL:** <https://discourse.julialang.org/t/how-to-select-multiple-items-using-dataframesmeta/16231>\
**Category:** General Usage\
**Tags:** question\
**Created:** [October 12, 2018, 1:53pm UTC](https://discourse.julialang.org/t/how-to-select-multiple-items-using-dataframesmeta/16231 "2018-10-12T13:53:32Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![youngjae.woo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/youngjae.woo/32/4750_2.png) [@youngjae.woo](https://discourse.julialang.org/u/youngjae.woo)\
**Post date:** [October 12, 2018, 1:53pm UTC](https://discourse.julialang.org/t/how-to-select-multiple-items-using-dataframesmeta/16231/1 "2018-10-12T13:53:32Z")

</div>

```julia
using DataFrames, DataFramesMeta
x = ["a", "b", "c", "a", "c", "d"]
df = DataFrame(x=x, y = 1:length(x))
z = ["a", "c"]

```

How do I query the items in z in df using DataFramesMeta?

`@where(df, in(z, :x))` didn’t work.

It’s a similar to [@where how to select mulit-rows. · Issue #77 · JuliaData/DataFramesMeta.jl · GitHub](https://github.com/JuliaData/DataFramesMeta.jl/issues/77)

---

<div class="post-metadata">

**Author:** ![kdyrhage](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kdyrhage/32/2326_2.png) [@kdyrhage](https://discourse.julialang.org/u/kdyrhage)\
**Post date:** [October 12, 2018, 1:57pm UTC](https://discourse.julialang.org/t/how-to-select-multiple-items-using-dataframesmeta/16231/2 "2018-10-12T13:57:09Z")

</div>

```julia
@where(df, map(c -> c in z, :x))
# or
@where(df, in.(:x, Ref{Array}(z)))

```

---

<div class="post-metadata">

**Author:** ![youngjae.woo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/youngjae.woo/32/4750_2.png) [@youngjae.woo](https://discourse.julialang.org/u/youngjae.woo)\
**Post date:** [October 12, 2018, 2:42pm UTC](https://discourse.julialang.org/t/how-to-select-multiple-items-using-dataframesmeta/16231/3 "2018-10-12T14:42:58Z")

</div>

thanks, can you enlighten me why you used `Ref{Array}` in the second example? Much appreciated.

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [October 12, 2018, 2:55pm UTC](https://discourse.julialang.org/t/how-to-select-multiple-items-using-dataframesmeta/16231/4 "2018-10-12T14:55:12Z")

</div>

The `{Array}` part is not necessary. `Ref(z)` would work just as well.

The reason we use `Ref` is because when we broadcast functions across elements of an array, like we are doing above, if one of the arguments, say,`x`, is broadcastable, like an array, as opposed to a scalar, it tries to apply the function to each element of `x`. If the length of `x` doesn’t match the length of the other arguments, we throw an error.

This is _not_ the case with scalars. `Ref` acts as a way to treat a non-scalar argument like a scalar so we can tell Julia not to try and broadcast the function across it.

---

<div class="post-metadata">

**Author:** ![youngjae.woo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/youngjae.woo/32/4750_2.png) [@youngjae.woo](https://discourse.julialang.org/u/youngjae.woo)\
**Post date:** [October 12, 2018, 3:03pm UTC](https://discourse.julialang.org/t/how-to-select-multiple-items-using-dataframesmeta/16231/5 "2018-10-12T15:03:54Z")

</div>

Awesome! Thanks 😀

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [October 12, 2018, 3:10pm UTC](https://discourse.julialang.org/t/how-to-select-multiple-items-using-dataframesmeta/16231/6 "2018-10-12T15:10:27Z")

</div>

This has come up a bit lately, so I updated an issue comment [here](https://github.com/JuliaLang/julia/issues/18379#issuecomment-429358886). a `Scalar` function has been discussed before to avoid this confusion.

---

<div class="post-metadata">

**Author:** ![piever](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/piever/32/1815_2.png) [@piever](https://discourse.julialang.org/u/piever)\
**Post date:** [October 12, 2018, 6:03pm UTC](https://discourse.julialang.org/t/how-to-select-multiple-items-using-dataframesmeta/16231/7 "2018-10-12T18:03:50Z")

</div>

In Julia 1.0 there’s a curried version of `in`, meaning that for a vector `v` `in(v)` is a function:

```julia
julia> in([1,2])(1)
true

julia> in([1,2])(3)
false

```

and this can be broadcasted:

```julia
julia> in([1,2]).(1:3)
3-element BitArray{1}:
  true
  true
 false

```

So I imagine you could do:

```julia
@where(df, in(z).(:x))

```

---

<div class="post-metadata">

**Author:** ![davidanthoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davidanthoff/32/223493_2.png) [@davidanthoff](https://discourse.julialang.org/u/davidanthoff)\
**Post date:** [October 13, 2018, 8:06pm UTC](https://discourse.julialang.org/t/how-to-select-multiple-items-using-dataframesmeta/16231/8 "2018-10-13T20:06:30Z")

</div>

Here is the [Query.jl](https://github.com/queryverse/Query.jl) way of doing this:

```julia
df |> @filter(_.x in z) |> DataFrame

```
