Skip to content

Instantly share code, notes, and snippets.

Avatar

Benjamin nutterb

  • Battelle Memorial Institute
  • Kentucky
View GitHub Profile
@nutterb
nutterb / convert_base
Created Mar 14, 2018
Convert between number systems. Still incomplete
View convert_base
convert_to_decimal_single <- function(x, from = 2){
coll <- checkmate::makeAssertCollection()
checkmate::assert_integerish(x,
add = coll)
checkmate::assert_integerish(from,
len = 1,
lower = 1,
upper = 36
@nutterb
nutterb / total_atomic_mass.R
Created Feb 28, 2018
Calculate total atomic mass of a compound.
View total_atomic_mass.R
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,
View risk.R
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))
View numeric summary table
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,
View tidy.microbenchmark
tidy.microbenchmark <- function(x, unit, ...){
summary(x, unit = unit)
}
@nutterb
nutterb / massert.R
Created Jul 12, 2017
Comparing code efficiency of multiple assertions in checkmate
View massert.R
# 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 / query_varchar_max
Created May 30, 2017
Execute queries to SQL Server that involve VARCHAR(MAX) variable types.
View query_varchar_max
#' @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 / limerick.txt
Created Mar 15, 2017
I want to download this file
View limerick.txt
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)
View dust_inline_example.Rmd
---
title: "Untitled"
output: html_document
---
```{r}
library(pixiedust)
fit <- lm(mpg ~ qsec * factor(gear) + factor(am), data = mtcars)
```
@nutterb
nutterb / Additional Work by Frank
Last active Jan 6, 2017
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#…
View Additional Work by Frank
# 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