1.2.3-SNAPSHOT Arrow_down_16x16
  • ($join [left-keys right-keys] left-data)
  • ($join [left-keys right-keys] left-data right-data)

Returns a dataset created by right-joining two datasets.
The join is based on one or more columns in the datasets.
If used within the body of the with-data macro, the second
dataset is optional, defaulting the the dataset bound to $data.


Examples:
(use '(incanter core stats datasets charts))
(def iris (get-dataset :iris))



(def lookup (dataset [:species :species-key] [["setosa" :setosa]
["versicolor" :versicolor]
["virginica" :virginica]]))
(view ($join [:species :Species] lookup iris))

(def hair-eye-color (get-dataset :hair-eye-color))
(def lookup2 (conj-cols ($ [:hair :eye :gender] hair-eye-color) (range (nrow hair-eye-color))))
(view ($join [[:col-0 :col-1 :col-2] [:hair :eye :gender]] lookup2 hair-eye-color))

(with-data hair-eye-color
(view ($join [[:col-0 :col-1 :col-2] [:hair :eye :gender]] lookup2)))


(def lookup3 (dataset [:gender :hair :hair-gender] [["male" "black" :male-black]
["male" "brown" :male-brown]
["male" "red" :male-red]
["male" "blond" :male-blond]
["female" "black" :female-black]
["female" "brown" :female-brown]
["female" "red" :female-red]
["female" "blond" :female-blond]]))

(view ($join [[:gender :hair] [:gender :hair]] lookup3 hair-eye-color))

(use 'incanter.charts)
(with-data (->> (get-dataset :hair-eye-color)
($where {:hair {:in #{"brown" "blond"}}})
($rollup :sum :count [:hair :gender])
($join [[:gender :hair] [:gender :hair]] lookup3)
($order :count :desc))
(view $data)
(view (bar-chart :hair :count :group-by :gender :legend true)))


0 Examples top

Log in to add / edit an example.

See Also top

Log in to add a see also.

Plus_12x12 Minus_12x12 Source incanter/core.clj:1901 top

(defn $join
" 
  Returns a dataset created by right-joining two datasets.
  The join is based on one or more columns in the datasets. 
  If used within the body of the with-data macro, the second
  dataset is optional, defaulting the the dataset bound to $data.


  Examples:
    (use '(incanter core stats datasets charts))
    (def iris (get-dataset :iris))



    (def lookup (dataset [:species :species-key] [[\"setosa\" :setosa] 
                                                  [\"versicolor\" :versicolor] 
                                                  [\"virginica\" :virginica]]))
    (view ($join [:species :Species] lookup iris))
   
   (def hair-eye-color (get-dataset :hair-eye-color))
   (def lookup2 (conj-cols ($ [:hair :eye :gender] hair-eye-color) (range (nrow hair-eye-color))))
   (view ($join [[:col-0 :col-1 :col-2] [:hair :eye :gender]] lookup2 hair-eye-color))

   (with-data hair-eye-color
     (view ($join [[:col-0 :col-1 :col-2] [:hair :eye :gender]] lookup2)))


   (def lookup3 (dataset [:gender :hair :hair-gender] [[\"male\" \"black\" :male-black]
                                                       [\"male\" \"brown\" :male-brown]
                                                       [\"male\" \"red\" :male-red]
                                                       [\"male\" \"blond\" :male-blond]
                                                       [\"female\" \"black\" :female-black]
                                                       [\"female\" \"brown\" :female-brown]
                                                       [\"female\" \"red\" :female-red]
                                                       [\"female\" \"blond\" :female-blond]]))

   (view ($join [[:gender :hair] [:gender :hair]] lookup3 hair-eye-color))

   (use 'incanter.charts)
   (with-data (->>  (get-dataset :hair-eye-color)
                    ($where {:hair {:in #{\"brown\" \"blond\"}}})
                    ($rollup :sum :count [:hair :gender])
                    ($join [[:gender :hair] [:gender :hair]] lookup3)
                    ($order :count :desc))
       (view $data)
       (view (bar-chart :hair :count :group-by :gender :legend true)))


"
  ([[left-keys right-keys] left-data]
     ($join [left-keys right-keys] left-data $data))
  ([[left-keys right-keys] left-data right-data]
     (let [left-keys (if (coll? left-keys) left-keys [left-keys])
	   right-keys (if (coll? right-keys) right-keys [right-keys])
	   index (apply hash-map 
			(interleave 
			 (map (fn [row] 
				(apply hash-map 
				       (interleave right-keys 
						   (map #(map-get (submap row left-keys) %) 
							left-keys)))) 
			      (:rows left-data))
			 (map #(reduce dissoc % left-keys) (:rows left-data))))
	   rows (map #(merge (index (submap % right-keys)) %) (:rows right-data))]
       (to-dataset rows))))
Vars in incanter.core/$join: defn let
Used in 0 other vars

Comments top

No comments for $join. Log in to add a comment.