Skip to content

Instantly share code, notes, and snippets.

@guilespi
Created October 29, 2012 02:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guilespi/3971236 to your computer and use it in GitHub Desktop.
Save guilespi/3971236 to your computer and use it in GitHub Desktop.
Iterating over a dataset to apply specific functions to each row
(defmacro apply-filtered
"Given two sequences, apply a function to each pair of elements when condition is met
anaphoras n and m exists for each indexed element
e.g. (apply-filtered / [1 2 3] [1 0 3] when (> m 0)) => (1 nil 1)
"
[op a b & condition]
`(for [x# (range (count ~a))]
(let [n# (nth ~a x#)
m# (nth ~b x#)]
(when (~(second condition) n# m#)
(~op n# m#)))))
(defmacro apply-rows
"Apply an operation to each row of the dataset excluding :Date column
A start row and a condition must be given
.e.g Divide each row by vector [1 0 3] starting from 0 validating divide by zero
(apply-rows ds (/ [1 2 3]) 0 (fn [n m] (> m 0))"
[data operation start cond]
`(let [raw-data# (incanter.core/$ :all [:not :Date] ~data)
raw-cols# (incanter.core/col-names raw-data#)
dates# (incanter.core/$ :all :Date ~data)]
(incanter.core/col-names
(incanter.core/conj-cols
(for [~'i (range ~start (incanter.core/nrow raw-data#))]
(apply-filtered
~(first operation)
(vec (incanter.core/$ ~'i [:not :Date] raw-data#))
~(second operation)
:when ~cond))
dates#) (conj raw-cols# :Date))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment