Skip to content

Instantly share code, notes, and snippets.

View nutterb's full-sized avatar

Benjamin nutterb

  • Battelle Memorial Institute
  • Kentucky
View GitHub Profile
@nutterb
nutterb / ggSurvGraph.R
Last active August 29, 2015 14:16
Customizable Survival Curves in ggplot2
#* More examples at https://gist.github.com/nutterb/fb19644cc18c4e64d12a
#' @name ggSurvGraph
#' @export ggSurvGraph
#' @import ggplot2
#' @importFrom grid unit
#' @importFrom gridExtra grid.arrange
#' @importFrom plyr arrange
#' @importFrom plyr ddply
#' @importFrom reshape2 melt
#' @importFrom zoo na.locf
@nutterb
nutterb / ggSurvGraph_Examples.R
Created March 10, 2015 13:19
Examples using ggSurvGraph
#* You'll have to source in the function code from the gist
#* https://gist.github.com/nutterb/004ade595ec6932a0c29
library(ggplot2)
install.packages("KMsurv")
data(kidney, package="KMsurv")
fit <- survfit(Surv(time, delta) ~ type, data=kidney)
ggSurvGraph(fit)
ggSurvGraph(fit, conf.bar=FALSE)
library(dplyr)
library(tidyr)
choose_partner <- function(People){
Males <- filter(People, gender == "M")
Females <- filter(People, gender == "F")
if (nrow(Males) <= nrow(Females)){
Males$partner_id <- sample(Females$id, nrow(Males))
People <- full_join(Males, Females,
pi_birthdays <- function(birthdate, n = 1:30, format = "%Y-%m-%d"){
if (!is.Date(birthdate) | !is.POSIXct(birthdate)){
birthdate <- as.Date(birthdate, format = format)
}
data.frame(n_pi = n,
pi_date = birthdate + 365.2425 * n * pi)
}
@nutterb
nutterb / Rectangles.R
Created October 23, 2015 13:00
Speed comparisons for different methods of comparing rectangles
#* A broader experiment on this stack overflow question
#* http://stackoverflow.com/questions/33298196/how-to-avoid-a-double-for-loop-when-accessing-all-combinations-of-colums-of-a-ma/33301080?noredirect=1#comment54403634_33301080
#* I really need to find something better to do with my Friday mornings
#* Change n to make a larger experiment
n <- 1000
#* Change reps to run more cycles in 'microbenchmark'
reps <- 10
@nutterb
nutterb / saveNullDate
Created January 31, 2016 01:36
dateInput save as NULL
library(shiny)
ui <- shinyUI(
fluidPage(
dateInput(inputId = "date",
label = "Signature Date",
value = ''),
actionButton(inputId = "save_signature",
label = "Print Signature"),
verbatimTextOutput("date_value"),
@nutterb
nutterb / directorySearch
Created April 8, 2016 13:27
When I'm developing packages, sometimes I just want to find the files that contain a particular variable. This helps me search all of the files for the existence of a string pattern.
directorySearch <- function(dir, pattern, ...)
{
files <- list.files(dir)
suppressWarnings(
Code <- lapply(file.path(dir, files),
readLines)
)
files[vapply(Code,
@nutterb
nutterb / medley_all_border.R
Last active April 8, 2016 16:27
A LaTeX safe approach to applying borders to all cells using pixiedust
medley_all_border <- function(x, part = c("table"))
{
part <-
match.arg(part,
c("table", "head", "body", "interfoot", "foot"),
several.ok = TRUE)
part <-
if ("table" %in% part)
{
c("head", "body", "interfoot", "foot")
@nutterb
nutterb / installer
Last active September 7, 2016 21:56
Install basic packages for STA 580
source("https://gist.githubusercontent.com/nutterb/23dee9325312d9a71ed5828df1ffd24a/raw/e6d1f2a05b774289246cf28f90902ece4763f92d/packages.R")
@nutterb
nutterb / significance stars
Created October 15, 2016 13:16
Pixiedust with significance stars
#' @name pvalString
#' @export pvalString
#'
#' @title Format P-values for Reports
#' @description Convert numeric p-values to character strings according to
#' pre-defined formatting parameters. Additional formats may be added
#' for required or desired reporting standards.
#'
#' @param p a numeric vector of p-values.
#' @param format A character string indicating the desired format for