Skip to content

Instantly share code, notes, and snippets.

View keberwein's full-sized avatar

Kris Eberwein keberwein

View GitHub Profile
@keberwein
keberwein / oeu_scrape.r
Last active January 9, 2020 18:57
BLS Occupation Code FTP Scrape Appleton, WI
library(blscrapeR)
library(dplyr)
library(stringr)
library(data.table)
# Using fread from data.table here becuse it's much faster and the first file is pretty huge.
doc <- data.table::fread("https://download.bls.gov/pub/time.series/oe/oe.data.0.Current")
titles <- data.table::fread("https://download.bls.gov/pub/time.series/oe/oe.occupation")
data_type <- data.table::fread("https://download.bls.gov/pub/time.series/oe/oe.datatype")
@keberwein
keberwein / bb8_with_ggplot2.R
Created May 10, 2018 12:54 — forked from pvictor/bb8_with_ggplot2.R
A Star Wars BB-8 with ggplot2 !
# BB-8 --------------------------------------------------------------------
# Inspired by Brian Hough in http://codepen.io/bhough/pen/wawrPL
# Packages ----------------------------------------------------------------
library("dplyr")
library("ggplot2")
@keberwein
keberwein / woba.sql
Created December 6, 2016 18:50
A MySQL adaptation of Tom Tango’s calculation for wOBA using the Lahman database.
CREATE TABLE PrimPos AS
SELECT playerID
, yearID
, teamID
, MAX(G) AS G
, POS
FROM (SELECT * from fielding
WHERE IF(yearID>1995 AND POS = "OF",1,0) != 1 ORDER BY G Desc) f
GROUP BY playerID, yearID, teamID, POS;
@keberwein
keberwein / woba.R
Created December 6, 2016 18:48
An R adaptation of Tom Tango’s calculation for wOBA using the Lahman database.
library(Lahman); library(dplyr)
# Find primary positions
fielding <- Lahman::Fielding
# The "postf" field below is to filter out Natl. League players who may have
# played as DH in inter-leauge games, and may have multiple entries at diff. positions.
PrimPos <- dplyr::mutate(fielding, postf=ifelse(POS=="OF" & yearID>1995, 1,0)) %>%
subset(postf==0, select=c("playerID", "yearID", "teamID", "lgID","G", "POS")) %>%
group_by(playerID, yearID, teamID, lgID, POS) %>%
summarise(G = sum(G))
@keberwein
keberwein / projections.R
Last active July 4, 2021 20:38
Map projections for the USA
# Makes sure the required packs are loaed, if not, intalles them.
pkgs <-c ('sp','ggplot2','rgdal','broom','maptools','tigris', 'blscrapeR')
for(p in pkgs) if(p %in% rownames(installed.packages()) == FALSE) {install.packages(p, repos='http://cran.us.r-project.org')}
for(p in pkgs) suppressPackageStartupMessages(library(p, quietly=TRUE, character.only=TRUE))
# Before we get into the projections, let's download some unemployment data to map.
county_dat <- get_bls_county()
# We also need a shapefile with the appropriate FIPS codes.
state <- counties(cb = TRUE, year = 2015)
@keberwein
keberwein / countyMap_ggplot.R
Last active October 28, 2016 15:43
Map County Unemployment with ggplot2
library(blscrapeR)
library(ggplot2)
library(dplyr)
# Get the current county data from blscrapeR.
df <- get_bls_county(stateName = "Ohio")
# Grap the internal FIPS codes data set from blscrapeR so we can match on counties.
fips <- blscrapeR::county_fips
# Subset fips list to only Ohio counties.
fips <- subset(fips, state=="OH")
@keberwein
keberwein / restart_nginx.bat
Created September 2, 2016 16:28
Restart Nginx Windows
@ECHO OFF
cd /nginx
taskkill /f /IM nginx.exe
start nginx
EXIT
@keberwein
keberwein / storm_trackr.R
Last active October 4, 2016 15:11
Track Unisys data available tropical storms
library(plyr); library(dplyr); library(leaflet); library(stringi);
library(htmltools); library(RColorBrewer); library(rvest)
# Parse and read storm track data.
html <- read_html('http://weather.unisys.com/hurricane/atlantic/2016/index.php')
links <- html_attr(html_nodes(html, "a"), "href")
links <- links[grep('track.dat', links)]
track <- select.list(links, title="Select storm:", graphics = FALSE)
#track <- "MATTHEW/track.dat"
@keberwein
keberwein / countyfips_leaflet.R
Last active February 23, 2023 01:34
A leaflet map for R built on a shape file of US counties
library(sp)
library(rgeos)
library(rgdal)
library(maptools)
library(dplyr)
library(leaflet)
library(scales)
### Begin data prep
# Grab air/water quality data from the EPA
url = "https://data.cdc.gov/api/views/cjae-szjv/rows.csv?accessType=DOWNLOAD"
@keberwein
keberwein / countystr_map.R
Created June 15, 2016 18:13
Create a map of US counties by string matching county and state name.
# Grab air/water quality data from the EPA
url = "https://data.cdc.gov/api/views/cjae-szjv/rows.csv?accessType=DOWNLOAD"
dat <- read.csv(url, stringsAsFactors = FALSE)
# Colnames tolower
names(dat) <- tolower(names(dat))
dat$countyname <- tolower(dat$countyname)
# Wide data set, subset only what we need.
county_dat <- subset(dat, measureid == "296",
select = c("countyfips","statename", "countyname", "value", "unitname"))
# Rename columns to make for a clean df merge later.