1.2.3-SNAPSHOT Arrow_down_16x16

$rollup

incanter.core

  • ($rollup summary-fun col-name group-by)
  • ($rollup summary-fun col-name group-by data)
Returns a dataset that uses the given summary function (or function identifier keyword)
to rollup the given column based on a set of group-by columns. The summary function
should accept a single sequence of values and return a single summary value. Alternatively,
you can provide a keyword identifer of a set of built-in functions including:

:max -- the maximum value of the data in each group
:min -- the minimum value of the data in each group
:sum -- the sum of the data in each group
:count -- the number of elements in each group
:mean -- the mean of the data in each group


Like the other '$' dataset functions, $rollup will use the dataset bound to $data
(see the with-data macro) if a dataset is not provided as an argument.

Examples:

(use '(incanter core datasets))

(def iris (get-dataset :iris))
($rollup :mean :Sepal.Length :Species iris)
($rollup :count :Sepal.Length :Species iris)
($rollup :max :Sepal.Length :Species iris)
($rollup :min :Sepal.Length :Species iris)

;; The following is an example using a custom function, but since all the
;; iris measurements are positive, the built-in mean function could have
;; been used instead.

(use 'incanter.stats)
($rollup #(mean (abs %)) :Sepal.Width :Species iris)

($rollup sd :Sepal.Length :Species iris)
($rollup variance :Sepal.Length :Species iris)
($rollup median :Sepal.Length :Species iris)

(def hair-eye-color (get-dataset :hair-eye-color))
($rollup :mean :count [:hair :eye] hair-eye-color)

(use 'incanter.charts)
(with-data ($rollup :mean :Sepal.Length :Species iris)
(view (bar-chart :Species :Sepal.Length)))

;; the following exaples use the built-in data set called hair-eye-color.

(with-data ($rollup :mean :count [:hair :eye] hair-eye-color)
(view (bar-chart :hair :count :group-by :eye :legend true)))

(with-data (->> (get-dataset :hair-eye-color)
($where {:hair {:in #{"brown" "blond"}}})
($rollup :sum :count [:hair :eye])
($order :count :desc))
(view $data)
(view (bar-chart :hair :count :group-by :eye :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:1633 top

(defn $rollup
"Returns a dataset that uses the given summary function (or function identifier keyword)
 to rollup the given column based on a set of group-by columns. The summary function
 should accept a single sequence of values and return a single summary value. Alternatively,
 you can provide a keyword identifer of a set of built-in functions including:
   
   :max -- the maximum value of the data in each group
   :min -- the minimum value of the data in each group
   :sum -- the sum of the data in each group
   :count -- the number of elements in each group
   :mean -- the mean of the data in each group
   

  Like the other '$' dataset functions, $rollup will use the dataset bound to $data
  (see the with-data macro) if a dataset is not provided as an argument.

  Examples:

    (use '(incanter core datasets))

    (def iris (get-dataset :iris))
    ($rollup :mean :Sepal.Length :Species iris)
    ($rollup :count :Sepal.Length :Species iris)
    ($rollup :max :Sepal.Length :Species iris)
    ($rollup :min :Sepal.Length :Species iris)
    
    ;; The following is an example using a custom function, but since all the 
    ;; iris measurements are positive, the built-in mean function could have 
    ;; been used instead.

    (use 'incanter.stats)
    ($rollup #(mean (abs %)) :Sepal.Width :Species iris)

    ($rollup sd :Sepal.Length :Species iris)
    ($rollup variance :Sepal.Length :Species iris)
    ($rollup median :Sepal.Length :Species iris)

    (def hair-eye-color (get-dataset :hair-eye-color))
    ($rollup :mean :count [:hair :eye] hair-eye-color)

    (use 'incanter.charts)
    (with-data ($rollup :mean :Sepal.Length :Species iris)
      (view (bar-chart :Species :Sepal.Length)))

     ;; the following exaples use the built-in data set called hair-eye-color.

     (with-data ($rollup :mean :count [:hair :eye] hair-eye-color)
       (view (bar-chart :hair :count :group-by :eye :legend true)))

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


"
  ([summary-fun col-name group-by]
     ($rollup summary-fun col-name group-by $data))
  ([summary-fun col-name group-by data]
     (let [key-fn (if (coll? col-name) 
		    (fn [row] 
		      (into [] (map #(map-get row %) col-name)))
		    (fn [row] 
		      (map-get row col-name)))
	   rows (:rows data)
	   rollup-fns {:max (fn [col-data] (apply max col-data))
		       :min (fn [col-data] (apply min col-data))
		       :sum (fn [col-data] (apply + col-data))
		       :count count
		       :mean (fn [col-data] (/ (apply + col-data) (count col-data)))}
	   rollup-fn (if (keyword? summary-fun)
		       (rollup-fns summary-fun)
		       summary-fun)]
       (loop [cur rows reduced-rows {}]
	 (if (empty? cur)
	   (let [group-cols (to-dataset (keys reduced-rows))
		 res (conj-cols group-cols (map rollup-fn (vals reduced-rows)))]
	     (col-names res (concat (col-names group-cols)
				    (if (coll? col-name) col-name [col-name]))))
	   (recur (next cur)
		  (let [row (first cur)
			k (submap row group-by)
			a (reduced-rows k)
			b (key-fn row)]
		    (assoc reduced-rows k (if a (conj a b) [b])))))))))
Vars in incanter.core/$rollup: assoc coll? concat conj defn empty? group-by let loop next
Used in 0 other vars

Comments top

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