Skip to content

Instantly share code, notes, and snippets.

View brshallo's full-sized avatar

Bryan Shalloway brshallo

View GitHub Profile
View find_in_files.R
library(magrittr)
find_in_files <- function(path, pattern){
path %>%
fs::dir_ls(recurse = TRUE, type = "file", regexp = "(\\.[rR])$") %>%
purrr::map(~grep(pattern, readLines(.x, warn = FALSE), value = TRUE)) %>%
purrr::keep(~length(.x) > 0)
}
@brshallo
brshallo / permits-issued.md
Created July 26, 2023 20:16
Before finding Seattle's API data source I'd at first pulled their permits issued data via their excel sheets. This was kind of hassle... See related gist here: https://gist.github.com/brshallo/7a14235134f8e10139f71c3369f8d50f
View permits-issued.md
library(tidyverse)

urls <- tibble(month = month.name, month_num = 1:12) %>% 
  cross_join(tibble(year = 2019:2023)) %>% 
  arrange(year) %>% 
  mutate(year_month = make_date(year = year, month = month_num)) %>% 
  filter(year_month < floor_date(today(), "months")) %>% 
  mutate(urls = paste0("https://www.seattle.gov/documents/Departments/SDCI/Resources/Stats/", year, month, "Summary", ".xlsx"))
@brshallo
brshallo / seattle-units-added-new-permits.md
Created July 26, 2023 19:56
Housing units added, new permits
View seattle-units-added-new-permits.md
library(tidyverse)
library(httr)
library(jsonlite)

# downloaded data from: https://data.seattle.gov/Permitting/Building-Permits/76t5-zqzr
data_permits <- read_csv("Building_Permits.csv")

data_permits %>% 
 filter(PermitTypeDesc == "New") %&gt;% 
View rolling-mean-conditioned-date.R
# This example only includes a value in the rolling mean() if the close date on
# the historical dates comes after the snapshot date for row of interest
### CREATE SAMPLE DATA
library(tidyverse)
library(slider)
library(lubridate)
sample_size <- 5000
@brshallo
brshallo / rolling-mean-conditioned-on-iteration-date.R
Created June 1, 2023 06:15
Example of calculating a rolling mean but conditioning that upon each observations date being less than the date in the index for the row.
View rolling-mean-conditioned-on-iteration-date.R
# This example only includes a value in the rolling mean() if the close date on
# the historical dates comes after the snapshot date for row of interest
### CREATE SAMPLE DATA
library(tidyverse)
library(slider)
sample_size <- 5000
obs_per_day <- 100
@brshallo
brshallo / lag-multiple-across.md
Last active December 6, 2022 18:04
is a riff on https://gist.github.com/brshallo/cadaa40cef6387e28924b6c3756627c9 but allows for use across multiple columns. Could make minor tweak to allow arbitrary function inputs, could also just use dplyover.
View lag-multiple-across.md
library(tidyverse)

lag_multiple <- function(x, n_vec){
  map(n_vec, lag, x = x) %>% 
    set_names(paste0("lag", n_vec)) %>% 
    as_tibble()
}

df <- tibble(a = 1:10, b = 11:20, c= 21:30)
View log-log-transform-example.md
library(dplyr)
library(ggplot2)
library(modeldata)

data(ames)

set.seed(123)
ames %>% 
  sample_n(200) %>%
@brshallo
brshallo / convert-currencies.R
Last active September 7, 2022 19:10
Improved and functionalized version of code pulled mostly from post on pricing -- can see this gist sourced in Appendix of post: https://www.bryanshalloway.com/2022/06/16/converting-between-currencies-using-pricer/#
View convert-currencies.R
library(priceR)
library(dplyr)
library(tidyr)
library(purrr)
library(lubridate)
# Create rates lookup table
pminmax <- function(x, y) {
paste(pmin.int(x, y), pmax.int(x, y), sep = ".")
View unique-set-speed-test.R
library(dplyr)
c_sort_collapse <- function(...){
c(...) |>
sort() |>
paste(collapse = ".")
}
unique_set <- function(...){
list(...) |>
View unique-combinations.R
library(dplyr)
c_sort_collapse <- function(...){
c(...) %>%
sort() %>%
paste(collapse = ".")
}
unique_combs <- function(...){
list(...) %>%