Skip to content

Instantly share code, notes, and snippets.

View almartin82's full-sized avatar

Andrew Martin almartin82

  • Princeton, NJ
  • 02:07 (UTC -04:00)
View GitHub Profile
@endolith
endolith / Has weird right-to-left characters.txt
Last active June 1, 2024 10:58
Unicode kaomoji smileys emoticons emoji
ּ_בּ
בּ_בּ
טּ_טּ
כּ‗כּ
לּ_לּ
מּ_מּ
סּ_סּ
תּ_תּ
٩(×̯×)۶
٩(̾●̮̮̃̾•̃̾)۶
@Ram-N
Ram-N / Flowfree_lib.R
Last active April 5, 2018 01:39
Using R and the library lpSoveAPI to find solutions to FlowFree boards. Note that the side has to be specified. There are 3 problems (*.csv) files, included for testing the testing. Create your own csv for other problems.
rm(list=ls())
library(lpSolveAPI)
#utility functions
getColnum <- function(cell, color, edge) {
# return(4*(cell-1)+edge)
celloffset <- (cell-1)*4*num.colors
coloroffset <- (color-1)*4
return(celloffset+coloroffset+edge)
}
@bayesball
bayesball / parse.retrosheet2.pbp.R
Created February 9, 2014 01:50
R function for downloading, upzipping, and appending Retrosheet play-by-play data
parse.retrosheet2.pbp = function(season){
# ADJUSTED FOR MAC -- function will work for WINDOWS and MAC
# download, unzip, append retrosheet data
# assume current directory has a folder download.folder
# download.folder has two subfolders unzipped and zipped
# program cwevent.exe is in unzipped folder (for windows)
download.retrosheet <- function(season){
# get zip file from retrosheet website
download.file(
@bayesball
bayesball / compute.runs.expectancy.R
Last active May 22, 2020 23:21
R function for computing runs values for all plays in a play-by-play Retrosheet file
compute.runs.expectancy <- function(season){
# changed -- plyr function replaced with dplyr
# (increases speed from 114 to 30 sec for 2013 data)
# assume that files "allseason.csv" and "fields.csv"
# are in current working folder
# for example, if season = 1961, all1961.csv should be
# available
# returns play-by-play matrix with new variables
@massenz
massenz / alert
Last active April 2, 2017 11:24
Originally written for Mandrill (screw them), this script can send an email alert, for example, when a remote job fails or some other unexpected event happens: I'm using it on my QNAP TS-212 NAS to alert me about backup jobs failures and other abnormal events.
#!/bin/bash
#
# Failure reporter, logs to a well-known log file failure messages
#
# Created 2014-04-30, M. Massenzio (m.massenzio@gmail.com)
declare -r LOGFILE="/var/log/fail.log"
declare -r ALERT_ICON="/usr/share/icons/ubudao-style/actions/dialog-warning.png"
declare -r ERROR_ICON="/usr/share/icons/ubudao-style/actions/stock_dialog-error.png"
@aammd
aammd / fizzbuzz.md
Last active January 25, 2017 08:25
An example of calculating "FizzBuzz" in R using dplyr and magrittr

Apparently, there is a simple problem called Fizz buzz which is sometimes used to identify competent programmers. A good opportunity to practice some dplyr and magrittr tricks.

library(dplyr)
library(magrittr)
library(knitr)

1:100 %>%
 data.frame %&gt;%
@bayesball
bayesball / batter.matchup.ggplot.R
Last active December 16, 2016 20:23
Finds estimates of true batting averages for all batters against a specific pitcher.
# loading in Retrosheet data for the seasons 1960 through 2013 from my website
load(url("http://bayes.bgsu.edu/baseball/pbp.1960.1979.Rdata"))
load(url("http://bayes.bgsu.edu/baseball/pbp.1980.1999.Rdata"))
load(url("http://bayes.bgsu.edu/baseball/pbp.2000.2013.Rdata"))
batter.matchup.ggplot <- function(Name, graph=TRUE, retroid=FALSE){
# this function assumes the data frames pbp.60.79, pbp.80.99, and pbp.00.13 are
# in the workspace
fit.model <- function(y, n){
require(LearnBayes)
@datalove
datalove / non_generic_function
Last active January 31, 2017 18:18
How to take an R function that isn't generic, make it generic and add method for your class without breaking the standard functionality
identical <- function(x) UseMethod("identical")
identical.my_class <- function(x) do_something(x)
identical.default <- base::identical
@rich-iannone
rich-iannone / us_cities_graph.R
Last active August 29, 2015 14:17
This is a Graphviz diagram that shows major US cities (with population greater than 300,000 people). Uses chromatography.js to provide some color by US state, and, the DiagrammeR R package (https://github.com/rich-iannone/DiagrammeR) to render the graph.
# install.packages("downloader")
# devtools::install_github("rich-iannone/DiagrammeR")
# devtools::install_github("jeroenooms/V8")
library("downloader")
library("DiagrammeR")
library("V8")
# Create a function that uses the 'chromatography' JS library to generate a random
# set of colors from the CIE Lab color space
@yihui
yihui / htmltools-deps.Rmd
Created March 31, 2015 02:33
A minimal example of HTML dependencies
---
title: HTML Dependencies
output: html_document
---
This example explains how HTML dependencies work in **htmltools**. Sometimes an HTML fragment may have additional dependencies to work correctly, such as JavaScript and/or CSS files. In R Markdown documents, you have to let **rmarkdown** know these dependencies, so they can be added to the HTML header when the document is rendered through Pandoc.
Another thing that you need to pay attention is you have to "protect" your HTML output so that Pandoc does not touch it, e.g. when you have four leading spaces, Pandoc may think this line is supposed to be a `<pre>` block whereas you only meant to indent the line for cosmetic purposes. In this case, the function `htmltools::htmlPreserve()` will be _automatically_ applied to your HTML content in R Markdown if the content is generated from `htmltools::tags` or wrapped in `htmltools::HTML()`.
Now we use a random CSS file in the **knitr** package to illustrate how dependencies work. The goal here is to generate a