Skip to content

Instantly share code, notes, and snippets.

# one connect/rcon *only*
exportForSas <- function(rcon, directory = NULL, ...) {
if(is.null(directory)) {
directory <- getwd()
}
curtime <- format(Sys.time(), '%Y%m%d%H%M%S')
cb <- assembleCodebook(rcon, expand_check = TRUE)
# used for looking up field_label
tmpcb <- unique(cb[,c('field_name','field_label','form_name')])
library(Hmisc)
options(digits=3)
set.seed(173)
sex <- factor(sample(c("m","f"), 500, rep=TRUE))
age <- rnorm(500, 50, 5)
treatment <- factor(sample(c("Drug","Placebo"), 500, rep=TRUE))
symp <- c('Headache','Stomach Ache','Hangnail','Muscle Ache','Depressed')
symptom1 <- sample(symp, 500,TRUE)
symptom2 <- sample(symp, 500,TRUE)
symptom3 <- sample(symp, 500,TRUE)
@couthcommander
couthcommander / weighted-scoring.R
Created September 28, 2017 15:06
how to value upcoming projected fantasy football stats
x <- matrix(NA, 200, 20)
pmean <- c(20, 10, 8, 5, 8, 6)
psd <- c(5, 2, 2, 2, 2, 3)
for(i in seq(nrow(x))) {
pos <- (i - 1) %% 6 + 1
x[i,1] <- i
x[i,2] <- pos
x[i,3] <- ifelse(i <= 100, (i - 1) %/% 10 + 1, 0)
x[i,4:20] <- sample(c(0, rnorm(16, pmean[pos], psd[pos])))
}
@couthcommander
couthcommander / rosters.R
Created December 1, 2016 23:54
look up NCAA football teams
library(XML)
fetchData <- function(id) {
url <- sprintf("http://www.espn.com/ncf/teams/roster?teamId=%s", id)
x <- htmlTreeParse(url, useInternalNodes=TRUE)
y <- xpathSApply(x, "//td", xmlValue)
z <- data.frame(matrix(y[-c(1:8)], ncol=7, byrow=TRUE), stringsAsFactors=FALSE)
names(z) <- y[2:8]
z[,'TM'] <- y[1]
z
@couthcommander
couthcommander / mlb2016-remaining.txt
Last active June 30, 2016 22:34
a quick look at the remaining schedule; H is wRC+; P is SIERA
N meanH medH meanP medP pastN pastmeanH pastmedH pastmeanP pastmedP dHit dPit
NYN 84 92.83333 97.0 4.008214 4.030 78 89.39744 90 4.096154 4.170 -3.435897436 -0.087939560
WAS 82 91.23171 92.0 4.141098 4.180 80 91.22500 92 4.068250 4.160 -0.006707317 0.072847561
MIA 83 89.72289 92.0 4.030602 4.030 79 92.39241 97 4.003038 4.030 2.669513497 0.027564435
CHN 82 95.64634 93.0 4.274390 4.305 80 92.87500 97 4.207500 4.275 -2.771341463 0.066890244
MIL 83 98.83133 104.0 4.252651 4.290 79 93.12658 92 4.059367 4.030 -5.704743023 0.193283514
SFN 81 94.76543 97.0 4.034198 4.180 81 93.41975 92 4.130370 4.180 -1.345679012 -0.096172840
PHI 82 92.04878 95.0 4.050244 4.160 80 94.96250 98 4.078375 4.160 2.913719512 -0.028131098
LAN 81 96.72840 98.0 4.135432 4.180 81 95.18519 97 4.166914 4.180 -1.543209877 -0.031481481
SLN 84 93.10714 91.5 4.242024 4.260 78 95.4359
nprimes <- function(n) {
res <- numeric(n)
cnt <- 1
x <- 2
while(cnt <= n) {
good <- TRUE
for(i in seq(2, sqrt(x))) {
if(x != i && x %% i == 0) {
good <- FALSE
break
library(shiny)
shinyServer(function(input, output) {
output$vorpPlot <- reactivePlot(function() {
id <- input$obs
nsim <- 1000
gender <- if(id == 51) 1
else if(id == 52) 0
else stats[id, 'gender']
spots <- seq(4+gender*2)