# Pass Vector to @subset

**URL:** https://discourse.julialang.org/t/pass-vector-to-subset/73099
**Category:** New to Julia
**Tags:** question, dataframes, dataframesmeta
**Created:** [December 14, 2021, 9:25pm UTC](https://discourse.julialang.org/t/pass-vector-to-subset/73099 "2021-12-14T21:25:28Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![HiramTheHero](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hiramthehero/32/218391_2.png) [@HiramTheHero](https://discourse.julialang.org/u/HiramTheHero)
#### Post date: [December 14, 2021, 9:25pm UTC](https://discourse.julialang.org/t/pass-vector-to-subset/73099/1 "2021-12-14T21:25:29Z")

</div>

Long story short, how do I pass a vector to @subset.

```julia
# From this...
@subset(iris, VectorIPassToTheFunction) )

```

```julia
# To this...
@subset(iris, :SepalLength .> 6, :SepalWidth .> 3) )

```

Long story:  
In the DataFramesMeta package you can subset using the @subset macro. I would like to pass in a vector for the macro to read. However, I haven’t found a way to do so. See below:

```julia
# Example Data
using RDatasets
rds = RDatasets
iris = rds.dataset("datasets", "iris");

using DataFramesMeta
# Normal Method
julia> @subset(iris, :SepalLength .> 5, :SepalLength .> 4) 
118×5 DataFrame
 Row │ SepalLength SepalWidth PetalLength PetalWidth Species   
     │ Float64 Float64 Float64 Float64 String    
─────┼─────────────────────────────────────────────────────────────
   1 │ 5.1 3.5 1.4 0.2 setosa
   2 │ 5.4 3.9 1.7 0.4 setosa
   3 │ 5.4 3.7 1.5 0.2 setosa
  ⋮ │ ⋮ ⋮ ⋮ ⋮ ⋮
 117 │ 6.2 3.4 5.4 2.3 virginica
 118 │ 5.9 3.0 5.1 1.8 virginica
                                                   113 rows omitted

```

When I try to create a vector of commands to pass in, I am met with the following:

```julia
julia> [:SepalLength .> 5, :SepalLength .> 4]
ERROR: MethodError: no method matching isless(::Int64, ::Symbol)
...

```

To me, this error makes sense because it is trying to evaluate the Symbol and integer comparison first. So, I tried the following:

```julia
# Make an Expression
julia> theSubVec = :[:SepalLength .> 5, :SepalLength .> 4]

# Get it's arguments
julia> theSubVec.args
2-element Vector{Any}:
 :(:SepalLength .> 5)
 :(:SepalLength .> 4)

# Try passing them in.
julia> @subset(iris, theSubVec.args)
ERROR: ArgumentError: length 2 of vector returned from function #516 is different from number of rows 150 of the source data frame.

# Try splatting the arguments in.
julia> @subset(iris, theSubVec.args...)
ERROR: syntax: "..." expression outside call around /Users/user/.julia/packages/DataFramesMeta/yzaoq/src/parsing.jl:216
Stacktrace:
 [1] top-level scope
   @ REPL[202]:1

# Try placing them in manually
julia> @subset(iris, theSubVec.args[1], theSubVec.args[2] )
ERROR: ArgumentError: functions passed to `subset` must return an AbstractVector.

# Try having them be evaluated in place.
julia> @subset(iris, eval(theSubVec.args[1]), eval(theSubVec.args[2]) )
ERROR: MethodError: no method matching isless(::Int64, ::Symbol)

```

Any help is appreciated. Thank you!

---

<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: [December 14, 2021, 10:16pm UTC](https://discourse.julialang.org/t/pass-vector-to-subset/73099/2 "2021-12-14T22:16:20Z")

</div>

You can’t pass arbitrary expressions to macros like that, unfortunately.

DataFramesMeta.jl is not the tool for this. Rather, you should use the underlying DataFrames.jl system that DataFramesMeta.jl builds off of.

To do these kinds of expressions programmatically, make a vector of pairs

```julia
julia> df = DataFrame(x = rand(10), y = rand(10));

julia> t = [:x => ByRow(>(.5)), :y => ByRow(>(.6))]
2-element Vector{Pair{Symbol, ByRow{Base.Fix2{typeof(>), Float64}}}}:
 :x => ByRow{Base.Fix2{typeof(>), Float64}}(Base.Fix2{typeof(>), Float64}(>, 0.5))
 :y => ByRow{Base.Fix2{typeof(>), Float64}}(Base.Fix2{typeof(>), Float64}(>, 0.6))

julia> subset(df, t)
3×2 DataFrame
 Row │ x y        
     │ Float64 Float64  
─────┼────────────────────
   1 │ 0.616501 0.70594
   2 │ 0.716532 0.604494
   3 │ 0.594781 0.930921

```

If you know what the operation _is_ but not what the column name is, you can work with name programmatically.

```julia
julia> x_name = :x; y_name = :y;

julia> @rsubset(df, $x_name > .5, $y_name > .6)
3×2 DataFrame
 Row │ x y        
     │ Float64 Float64  
─────┼────────────────────
   1 │ 0.616501 0.70594
   2 │ 0.716532 0.604494
   3 │ 0.594781 0.930921

```

---

<div class="post-metadata">

### Author: ![HiramTheHero](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hiramthehero/32/218391_2.png) [@HiramTheHero](https://discourse.julialang.org/u/HiramTheHero)
#### Post date: [December 14, 2021, 11:31pm UTC](https://discourse.julialang.org/t/pass-vector-to-subset/73099/3 "2021-12-14T23:31:45Z")

</div>

Thank you for confirming this for me.

It is very appreciated.
