Skip to content

Instantly share code, notes, and snippets.

View mmparker's full-sized avatar

Matt Parker mmparker

View GitHub Profile
@mmparker
mmparker / named_color_vector.r
Last active May 14, 2021 10:44
An example of using a named color vector to set colors in ggplot2
# The incantation
options(stringsAsFactors = FALSE)
library(ggplot2)
# Some sample data
# A test that can result in a negative or positive only
testres2 <- data.frame(result = c("Negative", "Positive"),
@mmparker
mmparker / md_to_html.bat
Created September 4, 2013 21:09
This is a script I use for converting directories full of Markdown files into directories full of HTML files, with a directory of links appended to each file.
REM This file converts all of the Markdown files to HTML.
REM Main files
for %%i in (main/*.markdown) do pandoc -f markdown -t html5 main/%%~ni.markdown links.markdown > html/main/%%~ni.html
REM Report pages
for %%i in (report_pages/*.markdown) do pandoc -f markdown -t html5 report_pages/%%~ni.markdown links.markdown > html/report_pages/%%~ni.html
@mmparker
mmparker / list_to_multicol_html.r
Last active December 24, 2015 14:39
Printing a list to a multicolumn HTML table with HTML inside the cells - two different methods I tried.
<head>
<style>
td {
text-align: left;
border: 1px solid #808080;
}
h4 {
background-color: #1F78B4;
color: #FFFFFF;
@mmparker
mmparker / calc_age.r
Last active August 13, 2021 18:58
Calculate years of age at a given date
# Calculate age at a given reference date
# Create an interval between the date of birth and the enrollment date;
# intervals are specific to the two dates. Periods give the actual length
# of time between those dates, so convert to period and extract the year.
calc_age <- function(birthDate, refDate = Sys.Date(), unit = "year") {
require(lubridate)
@mmparker
mmparker / show_old_vers.txt
Created January 28, 2014 00:38
How to view an old version of a Git-tracked file in Vim
git show commit-id:filename | vim -
@mmparker
mmparker / calc_qtr_end.r
Created January 30, 2014 16:50
Calculate the last day of a quarter, given year and quarter
# Calculate the date of the last day of a given quarter by pasting
# together its first day, adding three months, and subtracting a day.
# Very elegance
# Such vectorized
calc_qtr_end <- function(year, qtr) {
require(lubridate) # Easiest way to add a month to a date
(as.Date(paste(year, qtr * 3, "01", sep = "-")) %m+% months(1)) - 1
@mmparker
mmparker / printable_table.css
Created February 20, 2014 19:20
CSS for printing an HTML table, one row per page (it's a tall row)
@media print {
tr{
page-break-after: always;
display: block;
}
}
@mmparker
mmparker / applytorows.r
Created April 18, 2014 00:03
Ways to use a non-vectorized function on every row of a data.frame
# Sample data
X <- data.frame(
x = c(1, 2, 3),
y = c(4, 5, 6),
etc = c("a", "b", "c")
)
# Arbitrary stand-in for function that can't be vectorized (no pmax)
max.fun <- function(a, b) { max(c(a, b)) }
@mmparker
mmparker / date_diffs.r
Created May 22, 2014 21:48
Successive pairwise date diffs
library(zoo)
# Some sequence of Dates
x <- as.Date("2014-01-01") + c(0, 1, 2, 5, 10)
data.frame(x,
basediff = c(NA, diff(x)),
# Dummy list
z <- list(list('a' = 1, 'b' = 2, 'c' = 3), list('a' = 4, 'b' = 5, 'c' = 6))
# If you just want to stack all of the values, not keeping any of the list name data
# but ensuring they're all of one type
data.frame(values = as.numeric(unlist(z)))