filter/sum values, not sure how to implement clojure -
i trying "pivot" below data, although trying in excel doesn't seem work. not sure on if have logic right. need convert dataset dataset b.
a
date medal value1 value2 value3 value4
2013-03-08 silver 0 6052 0 0
2013-03-04 bronze 0 0 0 0
2013-03-04 bronze 0 0 0 0
2013-03-04 bronze 0 0 0 0
2013-03-04 bronze 0 1437 0 0
2013-03-01 bronze 0 0 240 0
2013-03-01 silver 0 0 0 0
2013-03-01 bronze 0 0 0 0
2013-03-01 bronze 0 173 0 0
2013-03-08 silver 0 0 0 271
2013-03-04 silver 0 2547 0 0
2013-03-04 bronze 0 495 0 0
2013-03-04 gold 279 0 0 0
2013-03-02 silver 0 0 0 0
2013-03-01 bronze 0 0 0 0
2013-03-01 silver 0 0 0 0
2013-03-01 bronze 0 0 0 0
2013-03-01 bronze 0 20 0 0
2013-03-03 silver 0 0 0 0
b
medal value1 value2 value3 value4
bronze 0 2125 240 0
gold 279 0 0 0
silver 0 8599 0 271
so i'm trying group-by medal , sum each value column based on medal filter (medal variable, purple, blue etc). below attempt wrong, reusing previous code round peg square hole territory, not sure how go logically confusing me now... :(
any or advice appreciated
(defn- create-summary [data] (->> data (group-by :medal) vals (map (fn [recs] (let [a (fn [b] (+ (filter #(= b(:medal%)) recs)recs))] {:medal (:medal (first recs)) :value1sum (a :medal) :value2sum (a :medal) :value3sum (a :medal) :value4sum (a :medal) }))) ))
(def data [{:medal "silver" :v1 1 :v2 2 :v3 3} {:medal "gold" :v1 1 :v2 2 :v3 3} {:medal "silver" :v1 1 :v2 2 :v3 3} {:medal "bronze" :v1 1 :v2 2 :v3 3} {:medal "bronze" :v1 1 :v2 2 :v3 3} {:medal "gold" :v1 1 :v2 2 :v3 3}]) (->> data (group-by :medal) (map (fn [[k v]] [k (apply merge-with + (map #(dissoc % :medal) v))])) (into {})) ==> {silver {:v1 2, :v2 4, :v3 6}, gold {:v1 2, :v2 4, :v3 6}, bronze {:v1 2, :v2 4, :v3 6}}
Comments
Post a Comment