Created
June 28, 2010 23:07
-
-
Save markmfredrickson/456521 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn data->maps "Turns a vector of vectors into a seq of maps" | |
[keys data] | |
(map #(zipmap keys %) data)) | |
(defn data-merge | |
"Merge two data sets on the given key" | |
[merge-key a b] | |
(let [indexed-b (zipmap (map merge-key b) b)] | |
(map #(into % (indexed-b (merge-key %))) (filter #(contains? indexed-b (merge-key %)) a)))) | |
(def d1 (data->maps [:id :c1 :c2] [[:a 1 2] [:b 3 4] [:c 5 6]])) | |
(def d2 (data->maps [:id :c3 :c4] [[:a 7 8] [:b 9 10] [:d 11 12]])) | |
(data-merge :id d1 d2) | |
; Incanter's version is a right join, not an inner join | |
(def d3 (dataset [:id :c1 :c2] [[:a 1 2] [:b 3 4] [:c 5 6]])) | |
(def d4 (dataset [:id :c3 :c4] [[:a 7 8] [:b 9 10] [:d 11 12]]) | |
($join [:id :id] d3 d4) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment