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)))
Comments top
No comments for $rollup. Log in to add a comment.