Julia 按满足条件的列的值聚合数据帧

Julia 按满足条件的列的值聚合数据帧,julia,aggregate,Julia,Aggregate,假设Julia中有以下数据帧,名为A: │ 一行│ x1│ x2│ ├──────┼─────┼─────────┤ │ 1.│ 1│ 5.78341│ │ 2.│ 2│ 5.05401│ │ 3.│ 3│ 4.79754│ │ 4.│ 4│ 4.4126│ │ 5.│ 5│ 4.29433│ │ 6.│ 6│ 4.14306│ │ 7.│ 1│ 5.94811│ │ 8.│ 2│ 5.0432│ │ 9│ 3│ 4.78697│ │ 10│ 4│ 4.40384│ │ 11│ 5│ 4.29

假设Julia中有以下数据帧,名为A:

│ 一行│ x1│ x2│
├──────┼─────┼─────────┤
│ 1.│ 1│ 5.78341│
│ 2.│ 2│ 5.05401│
│ 3.│ 3│ 4.79754│
│ 4.│ 4│ 4.4126│
│ 5.│ 5│ 4.29433│
│ 6.│ 6│ 4.14306│
│ 7.│ 1│ 5.94811│
│ 8.│ 2│ 5.0432│
│ 9│ 3│ 4.78697│
│ 10│ 4│ 4.40384│
│ 11│ 5│ 4.29901│
?
│ 3933│ 2│ 4.90528│
│ 3934│ 3│ 4.57429│
│ 3935│ 4│ 4.3988│
│ 3936│ 5│ 4.19076│
│ 3937│ 6│ 4.09517│
│ 3938│ 7│ 3.96192│
│ 3939│ 1│ 5.88878│
│ 3940│ 2│ 5.87492│
│ 3941│ 3│ 4.9453│
│ 3942│ 4│ 4.39047│
│ 3943│ 5│ 4.28096│
│ 3944│ 6│ 4.13686│

例如,仅当x1值的重复次数小于或等于500时,我想用x1值计算x2值的平均值。我尝试了以下代码,但无效:


aggregate(A,length(:x1)。我在这里使用DataFramesMeta.jl,因为它比仅使用DataFrames.jl功能更简洁(我给出了两种获得所需结果的方法作为示例):


通过
x1
聚合长度为
x1
。通过
x1
连接到原始数据帧,再次过滤和聚合。
using DataFramesMeta
# I generate a smaller DataFrame with cutoff of 15 for the example
df = DataFrame(x1=repeat([1,1,2,2,3], inner=10), x2=rand(50))

# first way to do it
@linq df |>
    groupby(:x1) |>
    where(length(:x1)>15) |>
    @based_on(x2=mean(:x2))

# other way to do the same
@linq df |>
    by(:x1, x2=mean(:x2), n=length(:x2)) |>
    where(:n.>15) |>
    select(:x1, :x2)