Skip to content

Instantly share code, notes, and snippets.

View leeper's full-sized avatar

Thomas J. Leeper leeper

View GitHub Profile
@leeper
leeper / read.qualtrics.csv.R
Created April 13, 2015 10:54
Function read data from a Qualtrics-generated CSV file
read.qualtrics.csv <- function(filename, stringsAsFactors = FALSE, ...) {
n <- read.csv(filename, nrows = 1, stringsAsFactors = FALSE)
dat <- read.csv(filename, header = FALSE, skip = 2, stringsAsFactors = stringsAsFactors, ...)
names(dat) <- names(n)
names(dat)[1:10] <- n[1,1:10]
for(i in seq_along(dat)) {
attr(dat[,i], "question") <- n[1,i]
}
dat
}
@snim2
snim2 / .travis.yml
Last active August 31, 2023 20:03
Travis-CI recipe for testing LaTeX projects compiled by a Makefile
install:
- sudo apt-get install texlive-latex-recommended texlive-latex-extra texlive-fonts-recommended
- sudo apt-get install chktex
script:
- make
- chktex -W # Print version information.
- chktex -q -n 6 *.tex chapters.*.tex 2>/dev/null | tee lint.out
# If lint output is non-empty report an error.
- test ! -s lint.out
@menugget
menugget / plot.stream.4.R
Last active February 25, 2019 18:46
Stream plot.
#plot.stream makes a "stream plot" where each y series is plotted
#as stacked filled polygons on alternating sides of a baseline.
#
#Arguments include:
#'x' - a vector of values
#'y' - a matrix of data series (columns) corresponding to x
#'order.method' = c("as.is", "max", "first")
# "as.is" - plot in order of y column
# "max" - plot in order of when each y series reaches maximum value
# "first" - plot in order of when each y series first value > 0
@mrdwab
mrdwab / stratified.R
Last active April 27, 2024 19:57
Stratified random sampling from a `data.frame` in R
stratified <- function(df, group, size, select = NULL,
replace = FALSE, bothSets = FALSE) {
if (is.null(select)) {
df <- df
} else {
if (is.null(names(select))) stop("'select' must be a named list")
if (!all(names(select) %in% names(df)))
stop("Please verify your 'select' argument")
temp <- sapply(names(select),
function(x) df[[x]] %in% select[[x]])
@spion
spion / screenshot.js
Last active May 27, 2022 01:38
Take website screenshots of any resolution using phantomjs
// Usage: phantomjs screenshot.js 1920x1080 site.domain.com
// outputs to site.domain.com-1920x1080.png
// dont add http to the URL
// If the page didnt render in time add a delay argument
// e.g. 3000 for 3 seconds
var page = require('webpage').create();
var args = require('system').args;
var wh = args[1].split('x');
@jbryer
jbryer / parse.codebook.r
Last active March 9, 2023 15:19
Parses a codebook file where lines starting at column zero (far left) represet variable information (e.g. name, description, type) and indented lines (i.e. lines beginning with white space, either tabs or spaces, etc.) represent factor levels and labels.
#' Parse a codebook file with variable and level information.
#'
#' Parses a codebook file where lines starting at column zero (far left) represet
#' variable information (e.g. name, description, type) and indented lines
#' (i.e. lines beginning with white space, either tabs or spaces, etc.) represent factor
#' levels and labels.
#'
#' Note that white space at the beginning and end of each line is stripped before
#' processing that line.
#'
@cboettig
cboettig / segue_custom.R
Created July 7, 2011 21:27
Segue with custom packages
library(segue)
setCredentials(getOption("amazon_key"), getOption("amazon_secret"))
require(mcmcTools)
X <- rnorm(20, 2, 5)
loglik <- function(pars){
sum( dnorm(X, mean=pars[1], sd=pars[2], log=TRUE) )
}
prior <- function(pars){
1/pars[2]^2
}
#!/usr/local/bin/Rscript
library(twitteR)
library(imguR)
sess <- initSession("clark_lab_linux", "password")
rstats <- searchTwitter("#rstats", sess, n=1000)
#filter out junk
rstats <- rstats[sapply(sapply(rstats, screenName), length)>0]
#make graph
posters <- sort(table(sapply(rstats, screenName)), decreasing=TRUE)