Last active
August 2, 2021 19:20
-
-
Save chamaeleon/3e5d3975b854761c211b84c107873c32 to your computer and use it in GitHub Desktop.
Basic use of the Clojure tablecloth library
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns weather.core | |
(:require [tablecloth.api :as api]) | |
(:gen-class)) | |
(defonce weather | |
(api/dataset "https://vega.github.io/vega-lite/examples/data/seattle-weather.csv" | |
{:key-fn keyword})) | |
(defn avg [data] | |
(double (/ (reduce + data) (count data)))) | |
(comment | |
;; Count rows in a dataset | |
(api/row-count weather) | |
;; Count columns in a dataset | |
(api/column-count weather) | |
;; Get the shape of the dataset (rows and columns) | |
(api/shape weather) | |
;; Get the column names | |
(api/column-names weather) | |
;; Get a summary of the data over the columns | |
(api/info weather) | |
;; Group the data by weather condition (sun, rain, snow, ...) | |
(api/group-by weather [:weather]) | |
;; Group the data by weather condition, and print it | |
(-> (api/group-by weather [:weather]) | |
(api/print-dataset {:print-line-policy :repl})) | |
;; Group the data into two groups: | |
;; days with a min temp less than zero | |
;; days with a positive min temp | |
(-> (api/group-by weather #(< (:temp_min %) 0)) | |
(api/rows :as-maps)) | |
;; Group the data by weather condition, and add two columns | |
;; average min temp for the group | |
;; average max temp for the group | |
(-> (api/group-by weather [:weather]) | |
(api/aggregate {:avg_min #(avg (:temp_min %)) | |
:avg_max #(avg (:temp_max %))})) | |
;; Save a new dataset that has had columns added to a csv text file | |
(-> (api/group-by weather [:weather]) | |
(api/aggregate {:avg_min #(avg (:temp_min %)) | |
:avg_max #(avg (:temp_max %))}) | |
(api/write! "my-weather-update.csv")) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment