I can filter a group based on a condition:
using DataPipes
a = [1,2,5,5,3,9,8]
large = @p filter(_>4, a)
# 5,5,9,8
small = @p filter(_<=4, a)
# 1,2,3
But is there a way to achieve the same in a single action?
# I would like to divide 'a' into two groups like this
large, small = @p divide(_>4, a)
If it exists, I do not find the name of the function (I tried ?divide
, ?split
, ?group
, ?separate
…).
You could use group
from SplitApplyCombine.jl:
julia> using DataPipes, SplitApplyCombine
julia> a = [1, 2, 5, 5, 3, 9, 8]
7-element Vector{Int64}:
1
2
5
5
3
9
8
julia> d = @p group(_ > 4, a)
2-element Dictionaries.Dictionary{Bool, Vector{Int64}}:
false │ [1, 2, 3]
true │ [5, 5, 9, 8]
julia> d[true] # large
4-element Vector{Int64}:
5
5
9
8
julia> d[false] # small
3-element Vector{Int64}:
1
2
3
EDIT: Or getting a bit fancier:
julia> d = @p group(_ > 4 ? "large" : "small", a)
2-element Dictionaries.Dictionary{String, Vector{Int64}}:
"small" │ [1, 2, 3]
"large" │ [5, 5, 9, 8]
julia> d["large"]
4-element Vector{Int64}:
5
5
9
8
julia> d["small"]
3-element Vector{Int64}:
1
2
3
4 Likes