Skip to content

Instantly share code, notes, and snippets.

library(tidyverse)
library(rgdal)
library(plotKML)
#Set input and output directories
os50_contours_dir <- here::here("~/Downloads/contours/")
output_dir <- here::here("~/Downloads/kml/")
#Load all shapefiles in input directory
shape_filenames <- list.files(os50_contours_dir, "*line.shp")
@Torvaney
Torvaney / coursera-lsh-nn-comparison.R
Created May 3, 2018 13:38
Comparison of multiple tables vs single table in NN search
# CF: www.coursera.org/learn/ml-clustering-and-retrieval/lecture/aZCHX/optional-improving-efficiency-through-multiple-tables
tibble(x = seq(0, 0.20, 0.001)) %>%
crossing(h = 1:4) %>%
mutate(single = 1 - (1-x)^h -h*x*(1-x)^(h-1),
multiple = (1 - (1-x)^h)^(h+1),
n_lines = h + 1) %>%
gather(key, value, -x, -h, -n_lines) %>%
ggplot(aes(x = x, y = value,
colour = key, group = key)) +
geom_path() +
tau <- function(hg, ag, home_rates, away_rates, rho) {
case_when(
(hg == 0) && (ag == 0) ~ 1 - home_rates * away_rates * rho,
(hg == 0) && (ag == 1) ~ 1 + home_rates * rho,
(hg == 1) && (ag == 0) ~ 1 + away_rates * rho,
(hg == 1) && (ag == 1) ~ 1 - rho,
TRUE ~ 1
)
}
@Torvaney
Torvaney / debug-by-print.clj
Last active October 31, 2018 12:35
this is a crutch
(defn print-log
[x]
(do
(if (not (nil? x)) (println x))
x))
library(rvest)
library(stringr)
webpage <- read_html("https://en.wikipedia.org/wiki/Magic_8-Ball")
# Find the answers
answers <-
webpage %>%
html_nodes("dd") %>%
html_text() %>%
@Torvaney
Torvaney / tools-and-stuff.md
Last active May 26, 2018 13:57
Tools and stuff I use regularly but would forget about and need to install if/when I get a new machine.
@Torvaney
Torvaney / compound-interest.Rmd
Last active May 28, 2018 17:43
Notebook exploring compound interest
---
title: "Compound interest calculator"
---
Terms are defined as:
* Principal - Initial amount invested
* Contribution - How much is contributed in each period
* Interest - Interest rate
* Period - How frequently interest is compounded
@Torvaney
Torvaney / R-commands.md
Last active May 30, 2018 18:30
Adding special "commands" to an R session.

I recently tried hacking some custom "commands" into the R REPL. I thought the method was fun and interesting enough to be worth sharing.

Background

In other language REPLs/interactive prompts, there are often a couple of special commands. Some of the more common ones are exit (to exit the session) and clear (to clear the buffer).

I often find myself instinctively attempting to use these in R. Of course, they do not exist in R. To terminate the R session, one can use quit(...) or q(...), but this requires typing brackets, as well as a "no" if you want to avoid that annoying "save workspace?" prompt.

exit

@Torvaney
Torvaney / infix.clj
Created June 5, 2018 11:26
Infix functions in clojure
(defmacro $
([x] x)
([f x] `(~f ~x))
([x f & args] `(~f ($ ~x) ($ ~@args))))
;; Examples
($ 1 + 5)
; 6
($ 1 + 5 + 2)