Skip to content

Instantly share code, notes, and snippets.

Avatar

Daniel D Reidpath dreidpath

View GitHub Profile
View find_varname.R
#' In a dataframe, find all variable names that match var_pattern
#'
#' @param var_pattern a character string ("pattern" to match)
#' @param dframe a dataframe
#'
#' @return all variable names in the dataframe that match var_pattern
#' @export
#'
#' @examples
#' find_var(iris, "idth")
@dreidpath
dreidpath / DJT-BMI.R
Created Feb 15, 2019
Visual analysis of Donald Trumps likely BMI
View DJT-BMI.R
# A quick visual analysis of Donald Trump's BMI assuming that his reported weight
# in 2018 and 2019 are correct, and his real height lies between his claimed
# height of 6'3" (1.9m) and a more likely height of 6'1" (1.854m)
# https://www.theguardian.com/us-news/2018/jan/17/a-tall-tale-accuracy-of-trumps-medical-report-and-new-height-questioned
# Include the ggplot library
library(ggplot2)
# Function to calculate BMI from height (m) and mass/weight (Kg)
bmicalc <- function(height, mass){
@dreidpath
dreidpath / phred.R
Created Sep 30, 2018
The R functions for the caluclation of the phred quality score, " a measure of the quality of the identification of the nucleobases generated by automated DNA sequencing" https://en.wikipedia.org/wiki/Phred_quality_score
View phred.R
phred <- function(q){
10^(-q/10)
}
inv_phred <- function(p){
10 * log10(p)
}
@dreidpath
dreidpath / SeaIce.R
Created Dec 13, 2017
A replotting of the recently published NOAA data from the Arctic Report Card 2017
View SeaIce.R
# These data appeared in a graph on climate.gov: http://bit.ly/2AFNBJz.
# Paraphrasing from the website:
# The data are for each November since 1979 to 2017.
# The variable 'area' represents the combined November sea ice
# area for the Chukchi and Beaufort Seas, the two sub-basins of
# the Arctic Ocean that touch Alaska’s Arctic (northern) coast.
# Larger values means more sea ice in the combined basin in square kilometres.
# The variable 'temp' is the November temperature (Fahrenheit) at Utqiaġvik,
# also known as Barrow (71°17′26″N 156°47′19″W).
# The data were digitised using Plot Digitizer
@dreidpath
dreidpath / wbplots.R
Created Jul 12, 2017
Quick time series plot of World Bank Life Expectancy and Infant Mortality Rate data downloaded using the wbstats package
View wbplots.R
# This is a quick script to create two time series plots using World Bank data downloaded using the wbstats package
# and plotted using ggplot2.
# The first plot uses Malaysia data only, and is a time series plot showing Life Expectancy at Birth changes
# in the whole population and for males and females separately.
# The second plot uses data for a handful of Southeast Asian countries and Australia and shows changes in the
# Infant Mortality Rate over time.
library(wbstats)
library(tidyverse)
@dreidpath
dreidpath / LE_1870_2016.R
Last active Jan 21, 2017
Changes in country level life expectancy 1870-2016 based on data from Gapminder.org
View LE_1870_2016.R
######################################################################################################################################
# This is a small piece of R-Code to support a recent blog-post of mine on country-level inequalities in life expectancy. #
# If you are so inclined, you can read the blog-post here: http://wp.me/p8fOE1-13 #
# #
# For the purposes of re-use, the code is released under an MIT License (https://choosealicense.com/licenses/mit/) #
# The code is provided "as is" and without warranties. #
# #
####### Notes
@dreidpath
dreidpath / p-divide.R
Created Jun 17, 2016
The probability (P_n) of an event in 1/n-th of the time if the probability of the event over the whole time is P
View p-divide.R
# Imagine that the probability of dying over a whole year is 0.5. Assuming risk is constant,
# what is the probability of dying in any single month? This trivial function (thank you Mark)
# ensures that the monthly-p results in an annual probability of 0.5. I wanted somewhere to
# remember it
p-Divide <- function(P, n){
1-(1-P)^(1/n)
}
@dreidpath
dreidpath / luhnTest.R
Created Jun 1, 2016
Luhn Test: Numbers like creidt cards have a check digit at the end. Given a string of numbers, the Luhn Test wil determine if the last number is the correct check digit
View luhnTest.R
luhnTest <- function(cc){
# cc: the number to test
# Reverse the digits, convert to numeric vector; see: https://en.wikipedia.org/wiki/Luhn_algorithm
# The code originally appeared in http://megasnippets.com/source-codes/r/luhn_test_credit_card_numbers
cc2 <- as.numeric(unlist(strsplit(as.character(cc), "")))[nchar(cc):1]
s1 <- 0
s2 <- 0
@dreidpath
dreidpath / flatland-randomwalk.R
Last active May 23, 2016
A Monte Carlo simulation of random walks in flatland, to see how long it takes an agent (p) to visit every unit in flatland at least once
View flatland-randomwalk.R
# A random-walk in flat-land. Note that the world is a circle.
maxworld <- 1000 # Flatland is maxworld units long
p <- integer(maxworld/2) # place p on the unit halfway between 0 and maxworld
dist <- c() # dist: the distribution of "times it took to complete each iter(ation) of the randmo walk"
for(loop in 1:1000){ # Run the siumulation of random walks 1000 times
world <- rep(0, maxworld) # Set up the world, as maxworld-long unvisited units
keep_going <- T # keep_going determined whether a random walk simulation continues
@dreidpath
dreidpath / xmas-tree.R
Created Apr 2, 2016
R code to generate a random christmas tree
View xmas-tree.R
# This appeared on R-blogger, a post by "wingfeet" from 23 December 2013: http://www.r-bloggers.com/merry-christmas-2/
# I stumbled acroos it and didn't want to lose it so I placed it here
part <- list(x0=0,y0=0,x1=0,y1=1,
branch1=NULL,branch2=NULL,extend=NULL,
lwd=1,depth=0,col='springgreen')
par(mfrow=c(1,1),mar=c(5, 4, 4, 2) + 0.1)
segplot <- function(tree) {
if (is.null(tree)) return()
segments(tree$x0,tree$y0,tree$x1,tree$y1,