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
(require '[clara.rules :as r]) | |
;;;; Define 3 rules, where the "priority" order is r1, r2, r3, where the highest priority is first | |
;;;; and the rest is in descending order of priority. | |
;;;; :type :rule/result "syntetic" fact is used to hold the final changes that can be queried out | |
;;;; from a session after `r/fire-rules` via `r/query` on the `find-results` query. | |
;;;; A namespace qualified keyword is used to avoid collision with externally given :type of | |
;;;; "real" facts. |
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: "Beer example" | |
#' author: "Matt Pettis ([email protected])" | |
#' date: "`r Sys.Date()`" | |
#' output: | |
#' html_document: | |
#' toc: true | |
#' toc_depth: 3 | |
#' code_folding: show | |
#' editor_options: |
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(lubridate) | |
library(tidyverse) | |
seq( | |
from=as.POSIXct("2012-1-1 0:00", tz="UTC"), | |
to=as.POSIXct("2012-1-3 23:00", tz="UTC"), | |
by="hour" | |
) -> | |
dt |
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(broom) | |
US <- read_csv("https://raw.githubusercontent.com/nytimes/covid-19-data/master/us.csv") %>% | |
mutate(new_deaths = deaths - lag(deaths)) %>% | |
filter(date >= "2020-02-26") | |
models <- tibble(degrees = 2:4) %>% | |
mutate(model = map(degrees, ~ lm(log(new_deaths + 1) ~ poly(date, .), data = US))) |
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
## Using run-length-encoding, create groups of identical values and put that | |
## common grouping identifier into a `grp` column. | |
library(tidyverse) | |
set.seed(42) | |
df <- tibble(x = sample(c(0,1), size=20, replace=TRUE, prob = c(0.2, 0.8))) | |
df %>% | |
mutate(grp = rle(x)$lengths %>% {rep(seq(length(.)), .)}) |
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: "spread-group-percent" | |
author: "Matt Pettis" | |
date: "March 22, 2019" | |
output: html_document | |
--- | |
```{r setup, include=FALSE} | |
knitr::opts_chunk$set(echo = TRUE) | |
``` |
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(geosphere) | |
library(tidyverse) | |
# Simple dataframe and use case: | |
ref_ll <- c(-95.0, 45.0) | |
df__ <- tribble( | |
~lon, ~lat, | |
-95.0, 45.0, | |
-96.0, 45.0, |
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
################################################################################ | |
# Make a function that does what I want | |
# Take a dataframe, identify the target column with runs, ID the index column, | |
# which is the column that I want to capture the first and last values of for | |
# runs. | |
#' rle_as_df | |
#' | |
#' Turn run-length encoding into a dataframe so it can be used with ggplot/geom_area. |
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
# Dealing with NULLs as list values | |
library(tidyverse) | |
# As advertised on the tin: | |
is.null(NULL) | |
#> [1] TRUE | |
# is.null() is not vectorized? | |
is.null(list(NULL)) |
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
``` r | |
## 'or'ing together same-length vectors of booleans. | |
## use case is to set up sets of booleans that have the same length and should | |
## be all 'or'ed together to form a condition. | |
# Base R way: | |
lstt <- list( | |
a = c(T, F, F, F) | |
, b = c(F, T, F, F) | |
, c = c(F, F, T, F) |