1.2.3-SNAPSHOT Arrow_down_16x16

$group-by

incanter.core

  • ($group-by cols)
  • ($group-by cols data)
Returns a map of datasets keyed by a query-map corresponding the group.

Examples:

(use '(incanter core datasets))
($group-by :Species (get-dataset :iris))

($group-by [:hair :eye] (get-dataset :hair-eye-color))

(with-data (get-dataset :hair-eye-color)
($group-by [:hair :eye]))

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:1803 top

(defn $group-by
"Returns a map of datasets keyed by a query-map corresponding the group.

  Examples:

    (use '(incanter core datasets))
    ($group-by :Species (get-dataset :iris))

    ($group-by [:hair :eye] (get-dataset :hair-eye-color))

    (with-data (get-dataset :hair-eye-color)
      ($group-by [:hair :eye]))

"
  ([cols]
     ($group-by cols $data))
  ([cols data]
     (let [n (nrow data)
	   rows (:rows data)]
       (loop [r 0 grouped-rows {}]
	 (if (= r n) 
	   (let [group-cols (keys grouped-rows)
 		 res (apply assoc {} (interleave group-cols (map to-dataset (vals grouped-rows))))]
 	     res)
	   (recur (inc r) 
		  (let [row (nth rows r)
			k (submap row cols)
			k-rows (grouped-rows k)]
		    (assoc grouped-rows k (if k-rows (conj k-rows row) [row])))))))))
Vars in incanter.core/$group-by: = assoc conj defn inc let loop
Used in 0 other vars

Comments top

No comments for $group-by. Log in to add a comment.