簡體   English   中英

Clojure /數據集:按層次划分多個列?

[英]Clojure/dataset: group-by multiple columns hierarchically?

我想實現一個可以按層次划分多個列的功能。 我可以通過以下兩列的臨時實現來說明我的要求:

(defn group-by-two-columns-hierarchically
  [col1 col2 table]
  (let [data-by-col1 ($group-by col1 table)
        data-further-by-col2 (into {} (for [[k v] data-by-col1] [k ($group-by col2 v)]))
        ]
    data-further-by-col2
    ))

我正在尋求有關如何對任意數量的列進行概括的幫助。

(我了解Incanter支持多列分組依據,但它僅提供結構而不是層次結構,即多列復合鍵到數據集值的映射。)

謝謝你的幫助!

注意:要使Michał的解決方案適用於incanter數據集,只需稍作修改,將“ group-by”替換為“ incanter.core / $ group-by”,如以下實驗所示:

(defn group-by*
      "Similar to group-by, but takes a collection of functions and returns
      a hierarchically grouped result."
      [fs coll]
      (if-let [f (first fs)]
        (into {} (map (fn [[k vs]]
                        [k (group-by* (next fs) vs)])
                   (incanter.core/$group-by f coll)))
        coll))

(def table (incanter.core/dataset ["x1" "x2" "x3"]
                                      [[1 2 3]
                                       [1 2 30]
                                       [4 5 6]
                                       [4 5 60]
                                       [7 8 9]
                                       ]))


(group-by* [:x1 :x2] table)
=>
    {{:x1 1} {{:x2 2} 
        | x1 | x2 | x3 |
        |----+----+----|
        |  1 |  2 |  3 |
        |  1 |  2 | 30 |
        }, 
    {:x1 4} {{:x2 5} 
        | x1 | x2 | x3 |
        |----+----+----|
        |  4 |  5 |  6 |
        |  4 |  5 | 60 |
        }, 
    {:x1 7} {{:x2 8} 
        | x1 | x2 | x3 |
        |----+----+----|
        |  7 |  8 |  9 |
        }}
(defn group-by*
  "Similar to group-by, but takes a collection of functions and returns
  a hierarchically grouped result."
  [fs coll]
  (if-let [f (first fs)]
    (into {} (map (fn [[k vs]]
                    [k (group-by* (next fs) vs)])
               (group-by f coll)))
    coll))

例:

user> (group-by* [:foo :bar :quux]
        [{:foo 1 :bar 1 :quux 1 :asdf 1}
         {:foo 1 :bar 1 :quux 2 :asdf 2}
         {:foo 1 :bar 2 :quux 1 :asdf 3}
         {:foo 1 :bar 2 :quux 2 :asdf 4}
         {:foo 2 :bar 1 :quux 1 :asdf 5}
         {:foo 2 :bar 1 :quux 2 :asdf 6}
         {:foo 2 :bar 2 :quux 1 :asdf 7}
         {:foo 2 :bar 2 :quux 2 :asdf 8}
         {:foo 1 :bar 1 :quux 1 :asdf 9}
         {:foo 1 :bar 1 :quux 2 :asdf 10}
         {:foo 1 :bar 2 :quux 1 :asdf 11}
         {:foo 1 :bar 2 :quux 2 :asdf 12}
         {:foo 2 :bar 1 :quux 1 :asdf 13}
         {:foo 2 :bar 1 :quux 2 :asdf 14}
         {:foo 2 :bar 2 :quux 1 :asdf 15}
         {:foo 2 :bar 2 :quux 2 :asdf 16}])
{1 {1 {1 [{:asdf 1, :bar 1, :foo 1, :quux 1}
          {:asdf 9, :bar 1, :foo 1, :quux 1}],
       2 [{:asdf 2, :bar 1, :foo 1, :quux 2}
          {:asdf 10, :bar 1, :foo 1, :quux 2}]},
    2 {1 [{:asdf 3, :bar 2, :foo 1, :quux 1}
          {:asdf 11, :bar 2, :foo 1, :quux 1}],
       2 [{:asdf 4, :bar 2, :foo 1, :quux 2}
          {:asdf 12, :bar 2, :foo 1, :quux 2}]}},
 2 {1 {1 [{:asdf 5, :bar 1, :foo 2, :quux 1}
          {:asdf 13, :bar 1, :foo 2, :quux 1}],
       2 [{:asdf 6, :bar 1, :foo 2, :quux 2}
          {:asdf 14, :bar 1, :foo 2, :quux 2}]},
    2 {1 [{:asdf 7, :bar 2, :foo 2, :quux 1}
          {:asdf 15, :bar 2, :foo 2, :quux 1}],
       2 [{:asdf 8, :bar 2, :foo 2, :quux 2}
          {:asdf 16, :bar 2, :foo 2, :quux 2}]}}}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM