1.2.3-SNAPSHOT Arrow_down_16x16

group-on

incanter.core

  • (group-on mat on-cols & options)
Groups the given matrix by the values in the columns indicated by the
'on-cols' argument, returning a sequence of matrices. The returned
matrices are sorted by the value of the group column ONLY when there
is only a single (non-vector) on-col argument.

Examples:

(use '(incanter core datasets))
(def plant-growth (to-matrix (get-dataset :plant-growth)))
(group-on plant-growth 1)
;; only return the first column
(group-on plant-growth 1 :cols 0)
;; don't return the second column
(group-on plant-growth 1 :except-cols 1)

(def plant-growth-dummies (to-matrix (get-dataset :plant-growth) :dummies true))
(group-on plant-growth-dummies [1 2])
;; return only the first column
(group-on plant-growth-dummies [1 2] :cols 0)
;; don't return the last two columns
(group-on plant-growth-dummies [1 2] :except-cols [1 2])

;; plot the plant groups
(use 'incanter.charts)
;; can use destructuring if you know the number of groups
;; groups are sorted only if the group is based on a single column value
(let [[ctrl trt1 trt2] (group-on plant-growth 1 :cols 0)]
(doto (box-plot ctrl)
(add-box-plot trt1)
(add-box-plot trt2)
view))

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

(defn group-on
" Groups the given matrix by the values in the columns indicated by the
  'on-cols' argument, returning a sequence of matrices. The returned
  matrices are sorted by the value of the group column ONLY when there
  is only a single (non-vector) on-col argument.

  Examples:

    (use '(incanter core datasets))
    (def plant-growth (to-matrix (get-dataset :plant-growth)))
    (group-on plant-growth 1)
    ;; only return the first column
    (group-on plant-growth 1 :cols 0)
    ;; don't return the second column
    (group-on plant-growth 1 :except-cols 1)

    (def plant-growth-dummies (to-matrix (get-dataset :plant-growth) :dummies true))
    (group-on plant-growth-dummies [1 2])
    ;; return only the first column
    (group-on plant-growth-dummies [1 2] :cols 0)
    ;; don't return the last two columns
    (group-on plant-growth-dummies [1 2] :except-cols [1 2])

    ;; plot the plant groups
    (use 'incanter.charts)
    ;; can use destructuring if you know the number of groups
    ;; groups are sorted only if the group is based on a single column value
    (let [[ctrl trt1 trt2] (group-on plant-growth 1 :cols 0)]
      (doto (box-plot ctrl)
            (add-box-plot trt1)
            (add-box-plot trt2)
            view))

"
  ([mat on-cols & options]
    (let [opts (when options (apply assoc {} options))
          cols (:cols opts)
          except-cols (:except-cols opts)
          groups (if (coll? on-cols)
                   (into #{} (to-list (sel mat :cols on-cols)))
                   (sort (into #{} (to-list (sel mat :cols on-cols)))))
          filter-fn (fn [group]
                      (cond
                        (and (coll? on-cols) (> (count on-cols) 1))
                          (fn [row]
                            (reduce #(and %1 %2)
                                    (map (fn [i g] (= (nth row i) g)) on-cols group)))
                        (and (coll? on-cols) (= (count on-cols) 1))
                          (fn [row]
                            (= (nth row (first on-cols)) group))
                        :else
                          (fn [row]
                            (= (nth row on-cols) group))))
         ]
      (cond
        cols
          (map #(sel mat :cols cols :filter (filter-fn %)) groups)
        except-cols
          (map #(sel mat :except-cols except-cols :filter (filter-fn %)) groups)
        :else
          (map #(sel mat :filter (filter-fn %)) groups)))))
Vars in incanter.core/group-on: cond defn let map
Used in 0 other vars

Comments top

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