Group Selection by Condition in DataFramesMeta.jl

I am looking to select groups that follow a condition using DataFramesMeta.jl.

Reference example in R that I am looking to reproduce:

require(data.table)
df = data.table(id = c(rep(1, 10), rep(2, 10), rep(3, 10)),
                x1 = c(rep(NA, 10), rnorm(10), c(rnorm(5), rep(NA, 5))))
df[, if(any(!is.na(x1))) .SD, .(id)]
df[, if(all(!is.na(x1))) .SD, .(id)]

Thanks in advance!

Unfortunately currently this is not very easy and you have to write:

filter(:x1 => x-> any(!ismissing, x), groupby(df, :id)) |> DataFrame
filter(:x1 => x-> all(!ismissing, x), groupby(df, :id)) |> DataFrame

However, in the upcoming release of DataFrames.jl (after this PR is merged) you can write using DataFramesMeta.jl:

@chain df begin
    groupby(:id)
    @subset(any(!ismissing, :x1))
end
@chain df begin
    groupby(:id)
    @subset(all(!ismissing, :x1))
end
1 Like