Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subset with regex #331

Closed
arnold-c opened this issue Jun 18, 2022 · 2 comments
Closed

Subset with regex #331

arnold-c opened this issue Jun 18, 2022 · 2 comments

Comments

@arnold-c
Copy link

arnold-c commented Jun 18, 2022

I was wondering if it's possible (or possible to add as a feature) to use @rsubset with regex? It would be incredibly helpful for my workflow where I often stack DataFrames with similar variable names i.e. they share characters and I want to subset them all based on a pattern. I appreciate that it is possible to do this with @select before stacking, but there are times where it would be useful to apply this directly to the long dataframe.

MWE below.

using DataFrames, DataFramesMeta

wide_df = DataFrame(
    S_L = rand(100),
    S_M = rand(100),
    S_H = rand(100),
    I_L = rand(100),
    I_M = rand(100),
    I_H = rand(100),
    R_L = rand(100),
    R_M = rand(100),
    R_H = rand(100)
)

long_df = stack(wide_df, variable_name = :state, value_name = :proportion)

let
    y = $(r"R")       #or y = Set([$(r"R")])
    @rsubset long_df :state in y         #or :state == $(r"R")
end
@bkamins
Copy link
Member

bkamins commented Jun 18, 2022

@rsubset long_df contains(:state, r"R")

@arnold-c
Copy link
Author

That's very helpful. Thanks @bkamins!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants