Skip to content

Instantly share code, notes, and snippets.

View davidclarance's full-sized avatar
🐦
birding

David Clarance davidclarance

🐦
birding
  • M-KOPA
  • Nairobi, Kenya
View GitHub Profile
WITH customers AS (
SELECT 'Customer1' AS customer_id UNION ALL
SELECT 'Customer2' AS customer_id UNION ALL
SELECT 'Customer3' AS customer_id UNION ALL
SELECT 'Customer4' AS customer_id
)
SELECT
c.customer_id,
CONVERT(BIGINT, SUBSTRING(HASHBYTES('MD5', c.customer_id), 1, 5)) % 100 AS assigned_bucket
FROM customers c;
CREATE TABLE IF NOT EXISTS customers AS
SELECT 'Customer1' AS customer_id UNION ALL
SELECT 'Customer2' AS customer_id UNION ALL
SELECT 'Customer3' AS customer_id UNION ALL
SELECT 'Customer4' AS customer_id;
SELECT
c.customer_id,
('0x' || md5(c.customer_id)[:10])::int64 % 100 as assigned_bucket
import hashlib
probability_assignments = {"Control": 50, "Variant 1": 30, "Variant 2": 20}
random_customer_ids = ["Customer1", "Customer2", "Customer3", "Customer4"]
def get_hash(customer_id):
hash_object = hashlib.md5(customer_id.encode())
return hash_object.hexdigest()[:10]
@davidclarance
davidclarance / get_multiple_species_data.R
Last active April 20, 2022 06:12
Get occurrence (presence) data for multiple species (including adhoc records) from the africabirdmap API
# Function adapted from https://github.com/AfricaBirdData/ABAP/
# Only pulls presence data
library("tidyverse")
library("purrr")
jsonToTibble <- function(jsonfile) {
out <- jsonfile %>%
lapply(function(x) {
x[sapply(x, is.null)] <- NA
@davidclarance
davidclarance / reporting_rate_distributions.R
Created April 19, 2020 08:54
Create animated maps to show changes in reporting rates across the year in Kenya
# 0. set up ---------------------------------------------------------------
# libraries
library(rabm)
library(tidyverse)
library(geojsonio)
library(broom)
library(ggplot2)
library(gganimate)
@davidclarance
davidclarance / read_all_csv_purrr.r
Last active September 20, 2018 15:16 — forked from ibombonato/read_all_csv_purrr.r
R - Read all csv in folder with purrr
library(tidyverse)
files <- list.files(path = "./data/", pattern = "*.csv")
df <- files %>%
map(function(x) {
read_csv(paste0("./data/", x))
}) %>%
reduce(rbind)