Subset using symbol from function input

Hi Guys,
I’m a newbie to use Julia for data manipulation. When I try to convert some of my Python Pandas code to Julia code, I got some problem.

function remove_outliers(data::DataFrame,variable::Symbol;lower=-Inf,upper=Inf)
      return subset(data, variable => x -> x .> lower, variable => x -> x .<= upper)

This function works perfect but when I try to use DataFramesMeta package to do it

function remove_outliers(data::DataFrame,variable::Symbol;lower=-Inf,upper=Inf)
      return @subset(data,  variable .> lower, variable .<= upper)

I got some error message. Could anyone help me!

You need to escape the Symbol with $

@subset(data,  $variable .> lower, $variable .<= upper)

See the docs here.

also, to avoid using . everywhere for broadcasting, there is @rsubset for row-wise operations. See docs here.

1 Like

Thanks! I like the community so much :smiling_face_with_three_hearts:

Or just write

remove_outliers(df, v; lo = -Inf, hi = Inf) = df[lo .< df[!, v] .< hi, :]

That’s dope!