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
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") |
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
# 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() + |
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
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 | |
) | |
} |
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
(defn print-log | |
[x] | |
(do | |
(if (not (nil? x)) (println x)) | |
x)) |
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
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() %>% |
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
--- | |
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 |
I recently tried hacking some custom "commands" into the R REPL. I thought the method was fun and interesting enough to be worth sharing.
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.
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
(defmacro $ | |
([x] x) | |
([f x] `(~f ~x)) | |
([x f & args] `(~f ($ ~x) ($ ~@args)))) | |
;; Examples | |
($ 1 + 5) | |
; 6 | |
($ 1 + 5 + 2) |