TidierOrg / TidierData.jl

Tidier data transformations in Julia, modeled after the dplyr/tidyr R packages.
MIT License
86 stars 7 forks source link

Cannot use variable name in @filter. #82

Closed curioustolearn closed 9 months ago

curioustolearn commented 9 months ago

Suppose I have a variable called sname = "ab12". If I do

using Tidier
@chain df @filter(colX = "ab12")

things work as expected.

However, if I do:

using Tidier
@chain df @filter(colX = sname)

I get the following error. I understand that it is looking for a column called sname. Is there a way to specify the value for filter stored in a variable name?

Thank you.

ERROR: ArgumentError: column name :sname not found in the data frame
Stacktrace:
  [1] lookupname
    @ ~/.julia/packages/DataFrames/58MUJ/src/other/index.jl:413 [inlined]
  [2] getindex
    @ ~/.julia/packages/DataFrames/58MUJ/src/other/index.jl:422 [inlined]
  [3] (::DataFrames.var"#555#556"{DataFrames.Index})(n::Symbol)
    @ DataFrames ./none:0
  [4] iterate(g::Base.Generator, s::Vararg{Any})
    @ Base ./generator.jl:47 [inlined]
  [5] collect_to!(dest::AbstractArray{T}, itr::Any, offs::Any, st::Any) where T
    @ Base ./array.jl:892 [inlined]
  [6] collect_to_with_first!(dest::AbstractArray, v1::Any, itr::Any, st::Any)
    @ Base ./array.jl:870 [inlined]
  [7] collect(itr::Base.Generator{Vector{Symbol}, DataFrames.var"#555#556"{DataFrames.Index}})
    @ Base ./array.jl:844
  [8] normalize_selection(idx::DataFrames.Index, sel::Pair{<:Any, <:Pair{<:Union{…}, <:Union{…}}}, renamecols::Bool)
    @ DataFrames ~/.julia/compiled/v1.10/DataFrames/AR9oZ_FK9Ta.dylib:-1
  [9] manipulate(df::DataFrame, cs::Any; copycols::Bool, keeprows::Bool, renamecols::Bool)
    @ DataFrames ~/.julia/packages/DataFrames/58MUJ/src/abstractdataframe/selection.jl:1697
 [10] select(df::DataFrame, args::Any; copycols::Bool, renamecols::Bool, threads::Bool)
    @ DataFrames ~/.julia/compiled/v1.10/DataFrames/AR9oZ_FK9Ta.dylib:-1
 [11] _get_subset_conditions(df::DataFrame, ::Base.RefValue{Any}, skipmissing::Bool, threads::Bool)
    @ DataFrames ~/.julia/compiled/v1.10/DataFrames/AR9oZ_FK9Ta.dylib:-1
 [12] subset(df::DataFrame, args::Any; skipmissing::Bool, view::Bool, threads::Bool)
    @ DataFrames ~/.julia/packages/DataFrames/58MUJ/src/abstractdataframe/subset.jl:284
 [13] top-level scope
    @ ~/.julia/packages/Tidier/pvXDO/src/Tidier.jl:411
 [14] top-level scope
    @ /Applications/Julia-1.10.app/Contents/Resources/julia/share/julia/stdlib/v1.10/REPL/src/REPL.jl:1428
Some type information was truncated. Use `show(err)` to see complete types.
drizk1 commented 9 months ago

Interpolation enables you to access variables outside the scope of the macro.

Interpolation is achieved with !!

Try the following code with the added !!

using Tidier
sname = "ab12"
@chain df @filter(colX = !!sname)
kdpsingh commented 9 months ago

Quick separate clarification I'll add in addition to the above answer. The purpose of @filter() is to subset rows. So in general, you would use == instead of = inside of @filter().

kdpsingh commented 9 months ago

Please feel to reply with whether this answered your question, or provide a minimal reproducible example if you can. Thanks for using the package!

kdpsingh commented 9 months ago

I'm going to close this issue. If you still have questions, please feel free to reply and we can re-open.