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 / 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 / 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
@tjmahr
tjmahr / compute_bias.R
Last active January 21, 2016 15:31
find the most viewed image in a time window
compute_bias <- function(looks, window, t_col = "Time", aoi_col = "GazeByImageAOI", sample_rate = NULL) {
# Create filtering expression by plugging in the name of the "Time" column
interp <- lazyeval::interp
pred <- interp(~ between(x, min(w), max(w)), x = as.name(t_col), w = window)
# Keep just looks in range defined by window
subtrial <- looks %>% filter_(pred)
# Make sure that there is only set of looks in the data
window_times <- subtrial[[t_col]]
sample_n_groups <- function(df, n, group_keys = as.character(groups(df))) {
# Remember original grouping
group_keys_orig <- as.character(groups(df))
stopifnot(length(group_keys) != 0)
df_sub <- df %>%
# Keep just the grouping columns
group_by_(.dots = group_keys) %>%
select(one_of(group_keys)) %>%
distinct %>%