Skip to content

Instantly share code, notes, and snippets.

View tjmahr's full-sized avatar
🍍
sampling

TJ Mahr tjmahr

🍍
sampling
View GitHub Profile
@tjmahr
tjmahr / cat.R
Created June 10, 2014 18:09
a cat saying happy birthday
library("devtools")
install_github("sckott/cowsay")
library("cowsay")
say("HAPPY BIRTHDAY", "cat")
## -----
## HAPPY BIRTHDAY
## ------
## \
## \
@tjmahr
tjmahr / leading_zero.R
Created March 5, 2015 14:19
leading zeros
sprintf("%03.f", seq_len(20))
# [1] "001" "002" "003" "004" "005" "006" "007" "008" "009" "010"
#[11] "011" "012" "013" "014" "015" "016" "017" "018" "019" "020"
@tjmahr
tjmahr / utils_print.R
Last active August 29, 2015 14:16
pretty printing
# Code for formatting numbers (for my knitr docs)
library("magrittr")
library("dplyr", warn.conflicts = FALSE)
library("broom")
library("stringr")
# Fixed width integers (like for track or image numbers in filenames)
sprintf("%03.f", seq_len(20))
# [1] "001" "002" "003" "004" "005" "006" "007" "008" "009" "010"
@tjmahr
tjmahr / backup_tbl.R
Last active August 29, 2015 14:23
backup_tbl
library("dplyr")
library("readr")
# Download a tbl from a db connection and write to a csv
backup_tbl <- function(tbl_name, src, output_dir) {
# Try to download the tbl, defaulting to an empty data-frame
try_tbl <- failwith(data_frame(), tbl)
df <- collect(try_tbl(src, tbl_name))
output_file <- file.path(output_dir, paste0(tbl_name, ".csv"))
@tjmahr
tjmahr / date.R
Last active May 26, 2017 01:06
xml example
library("magrittr")
library("stringr")
library("lubridate")
library("xml2")
get_date <- function(xml_blob) {
# Convert XML clock info into an R list
xml_list <- xml_blob %>%
str_replace_all("\\\\n", "") %>%
read_xml %>%
@tjmahr
tjmahr / scrapefork.R
Last active August 29, 2015 14:28
Getting Pitchfork's Eighties Also-Rans
# Download all the 'See also: Artist: "Title"' entries from
# Pitchfork's Top 200 songs of the 80's
# http://pitchfork.com/features/staff-lists/9700-the-200-best-songs-of-the-1980s/
library("rvest")
library("stringr")
library("dplyr", warn.conflicts = FALSE)
library("curl")
# Given url, get see-also paragraphs and text matching "See also*"
scrape_see_also_nodes <- function(url) {

Good idea or bad idea for YAML metadata?

---
title: "My report"
author: "Tristan Mahr"
output: html_document
params:
  knitr_chunks: !r 
 knitr::opts_chunk$set(comment = "#&gt;", 
@tjmahr
tjmahr / anxiety.R
Last active November 23, 2015 20:44
# Function factory
make_adder <- function(x) {
function() x + 1
}
# Create list of functions with Map
funs <- Map(make_adder, 1:10)
unlist(Map(function(f) f(), funs))
# [1] 2 3 4 5 6 7 8 9 10 11
@tjmahr
tjmahr / pvalue_stars.R
Created November 28, 2015 01:39
pvalue_stars
get_p_stars <- function(ps) {
symnum(ps, na = FALSE, cutpoints = c(0, 0.001, 0.01, 0.05, 0.1, 1),
symbols = c("***", "**", "*", ".", " "), legend = FALSE)
}
#> get_p_stars(c(.00001, .001, .002, .01, .02, .05, .06, .1, .2))
#> [1] *** *** ** ** * * . .
@tjmahr
tjmahr / days_between_visits.R
Created January 13, 2016 20:52
days between lab visits
library("readr")
library("dplyr")
library("tidyr")
library("lubridate")
# Load the visit history, parse the timestamp as a Date
mdr <- read_csv("C:/Users/trist/Downloads/longitudinal_missing.csv") %>%
mutate(Date = mdy_hm(DateTime) %>% as.Date)
# for each study/subject, find difference between latest and earliest date