我知道combine(groupby(df, :A), nrow=>:count)
可以用来计算不同:A
的行数。然而,如何正确实现以获取不同:A
下唯一值的数量呢?基本上,我正在寻找与R相应的方法:df %>% group_by(A) %>% summarize(n_unique = n_distinct(B))
。谢谢!
我认为您应该能够做到
combine(groupby(df, :A), :B => length ∘ unique => :n_distint_B)
就像这样:
julia> using DataFrames
julia> df = DataFrame(a = rand(["a", "b"], 20), b = rand(1:5, 20))
20×2 DataFrame
Row │ a b
│ String Int64
─────┼───────────────
1 │ a 3
2 │ b 4
3 │ a 1
4 │ a 1
5 │ b 1
6 │ a 2
7 │ b 4
8 │ a 2
9 │ b 2
10 │ b 1
11 │ b 3
12 │ b 3
13 │ a 4
14 │ a 4
15 │ b 3
16 │ b 2
17 │ a 5
18 │ a 5
19 │ b 5
20 │ a 1
julia> combine(groupby(df, :a), :b => length ∘ unique => :n_distinct_b)
2×2 DataFrame
Row │ a n_distinct_b
│ String Int64
─────┼──────────────────────
1 │ a 5
2 │ b 5
以下是使用双重groupby
-combine
策略的替代解决方案,速度略快:
julia> df = repeat(DataFrame(a = rand(["a", "b"], 20), b = rand(1:5, 20)), 10^6);
julia> @btime combine(groupby($df, :a), :b => length ∘ unique => :n_distinct_b);
650.915 ms (237 allocations: 866.37 MiB)
julia> @btime combine(groupby(combine(groupby($df, [:a, :b]), nrow), :a), nrow => :n_distinct_b);
457.884 ms (346 allocations: 561.20 MiB)