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 / dummy_variables.R
Created December 30, 2016 12:43
Options for Creating Dummy Variables
library(magrittr)
dummy_ifelse <- function(x)
{
if (!is.factor(x)) stop("x is not a factor")
lev <- levels(x)
lapply(lev[-1],
function(l) ifelse(x %in% l, 1, 0)) %>%
@nutterb
nutterb / Additional Work by Frank
Last active January 6, 2017 18:35
An example of a case where a `for` loop isn't any slower than `lapply`. In fact, it is consistently faster than a typical `lapply`, though I can get very close to the execution time of the `for` loop if I use `<<-` in the `lapply`. Take a look at http://stackoverflow.com/questions/41471757/update-pairs-of-columns-based-on-pattern-in-their-names#…
# Adding lapply versions to http://stackoverflow.com/a/41511889/1017276
library(magrittr)
library(data.table)
library(microbenchmark)
set.seed(pi)
nc = 1e3
nr = 1e2
df_m0 = sample(c(1:10, NA_integer_), nc*nr, replace = TRUE) %>% matrix(nr, nc) %>% data.frame
df_r = sample(c(1:10), nc*nr, replace = TRUE) %>% matrix(nr, nc) %>% data.frame
@nutterb
nutterb / dust_inline_example.Rmd
Created January 20, 2017 17:18
dust_inline example
---
title: "Untitled"
output: html_document
---
```{r}
library(pixiedust)
fit <- lm(mpg ~ qsec * factor(gear) + factor(am), data = mtcars)
```
@nutterb
nutterb / limerick.txt
Created March 15, 2017 20:48
I want to download this file
A silly young man from Clyde
In a funeral procession was spied;
When asked, "Who is dead?"
He giggled and said,
"I don't know; I just came for the ride."
(source: https://www.brownielocks.com/Limericks.html)
@nutterb
nutterb / query_varchar_max
Created May 30, 2017 12:11
Execute queries to SQL Server that involve VARCHAR(MAX) variable types.
#' @name query_varchar_max
#' @title Query a VARCHAR(MAX) Variable from SQL Server
#'
#' @importFrom RODBCext sqlExecute
#'
#' @description The RODBC driver to SQL Server (SQL Server Native Client 11.0)
#' reports the lenght of a VARCHAR(MAX) variable to be zero. This presents
#' difficulties in extracting long text values from the database. Often, the
#' ODBC will assume a length of 255 characters and truncate the text to that
#' many characters. The approach taken here searches the VARCHAR(MAX) variables
@nutterb
nutterb / massert.R
Created July 12, 2017 11:32
Comparing code efficiency of multiple assertions in checkmate
# Suppose we have arguments for power and sample size of a two-sample t-test
# (Similar to power.t.test, but vectorized. See https://github.com/nutterb/StudyPlanning/blob/devel/R/test_t2.R
# delta: Difference of means. (-Inf, Inf)
# delta0: Difference under the null hypothesis. (-Inf, Inf)
# se: Standard error (0, Inf)
# alpha: significance level (0, 1)
# power: power of the test (0, 1)
### STANDARD ARGUMENT CHECKS ###
# 19 lines of code (259 characters)
@nutterb
nutterb / tidy.microbenchmark
Created September 12, 2017 19:33
tidy.microbenchmark
tidy.microbenchmark <- function(x, unit, ...){
summary(x, unit = unit)
}
quick_summary <- function(df, vars, group = NULL){
group_is_null <- is.null(group)
coll <- checkmate::makeAssertCollection()
checkmate::assert_data_frame(x = df,
add = coll)
checkmate::assert_character(x = vars,
risk_attack_succeed_sim <- function(attack, defend){
while(attack >= 2 && defend > 0){
n_attack <- min(c(3, attack - 1))
n_defend <- min(c(2, defend))
roll_attack <- sample(1:6, n_attack, replace = TRUE)
roll_defend <- sample(1:6, n_defend, replace = TRUE)
n_compare <- min(c(n_attack, n_defend))
@nutterb
nutterb / total_atomic_mass.R
Created February 28, 2018 17:44
Calculate total atomic mass of a compound.
atomic_mass <- c( H = 1.008, He = 4.0026, Li = 6.94, Be = 9.0122,
B = 10.81, C = 12.011, N = 14.007, O = 15.999,
F = 18.998, Ne = 20.180, Na = 22.990, Mg = 24.305,
Al = 26.982, Si = 28.085, P = 30.974, S = 32.06,
Cl = 35.45, Ar = 39.948, K = 39.098, Ca = 40.078,
Sc = 44.956, Ti = 47.867, V = 50.942, Cr = 51.996,
Mn = 54.938, Fe = 55.845, Co = 58.933, Ni = 58.693,
Cu = 63.546, Zn = 65.38, Ga = 69.732, Ge = 72.630,
As = 74.922, Se = 78.971, Br = 79.904, Kr = 83.798,
Rb = 85.468, Sr = 87.62, Y = 88.906, Zr = 91.224,