Skip to content

Instantly share code, notes, and snippets.

View christophergandrud's full-sized avatar

Christopher Gandrud christophergandrud

View GitHub Profile
knitr::opts_knit$set(stop_on_error = 2L)
#' Generate a random password with R
#'
#' @param n_char integer with the number of characters for the resulting
#' password.
#' @param n_special integer with the number of special (non-letter or number)
#' characters in the password.
#' @param special_chars character vector with the special characters that can be
#' included in the password.
#'
#' @examples
@christophergandrud
christophergandrud / monadic_spatial_weights.R
Last active August 24, 2016 13:17
Find monadic spatial weights for continuous numeric data
#' Find monadic spatial weights for continuous numeric data in a time series data set
#' df a data frame containing the unit ID variable and time variables as well as
#' 'location' and dependent variables.
#' id_var a character string identifying the unit ID variable in \code{df}.
#' time_var a character string identifying the time variable in \code{df}.
#' location_var a character string identifying the location of the units in
#' \code{df}. This is used to create the weighting matrix. Note that the function
#' finds the relative distance between the units by subtracting their 'location'.
#' y_var a character string identifying the dependent variable in \code{df}. Note that
#' an independent variable could also be supplied.
@christophergandrud
christophergandrud / oecd_membership_dummy.R
Last active August 22, 2016 12:51
Download OECD membership start dates and use it to create a country-year OECD membership dummy variable
# ---------------------------------------------------------------------------- #
# Convert an HTML table of OECD member start dates to year-membership dummies
# Christopher Gandrud
# MIT License
# ---------------------------------------------------------------------------- #
# Load required packages
library(rio)
library(rvest)
library(countrycode)
@christophergandrud
christophergandrud / spell_features.R
Last active May 5, 2016 07:20
Find key features of a spell including and ordered spell ID and each spell's duration
#' Find key features of a spell including and ordered spell ID and each spell's
#' duration
#'
#' @param x a time ordered vector with values identifying a spell. It is
#' assumed that when a value in this vector changes that the spell has ended.
#' @param id logical specifying whether or not to return the spell ID
#' @param duration logical specifying whether or not to return the spell
#' duration.
#'
#' @examples
@christophergandrud
christophergandrud / missing_map.R
Last active March 30, 2016 09:50
Simple example of creating a missingness map with country-year data
# ---------------------------------------------------------------------------- #
# Create a missingness map to examine patterns of missing values across variables
# Christopher Gandrud
# MIT LICENSE
# ---------------------------------------------------------------------------- #
# Load required packages
library(WDI)
library(dplyr)
library(Amelia)
#' Convert a vector into a matrix with two columns
#'
#' @param x a vector
equal_columns <- function(x) {
round2 = function(x, digits) {
posneg = sign(x)
z = abs(x)*10 ^ digits
z = z + 0.5
z = trunc(z)
@christophergandrud
christophergandrud / plot_me.R
Last active December 1, 2015 15:32
Plot marginal effects from two-way interactions in linear regressions
#' Plot marginal effects from two-way interactions in linear regressions
#'
#' @param obj fitted model object from \code{lm}
#' @param term1 the first constitutive term's variable name. Note b1 and b2 must
#' be entered in the order in which they are entered into the \code{lm} model.
#' @param term2 character string of the second constitutive variable's name.
#' @param fitted numeric vector of fitted values of \code{term2} to plot for.
#'
#' @return a \code{gg} class ggplot2 object
#'
@christophergandrud
christophergandrud / range01.R
Last active August 29, 2015 14:23
Rescale a vector to be within 0 and 1
#' Rescale a vector to be within 0 and 1
#'
#' @param x a numeric vector to rescale
#' @param na.rm logical. Whether or not to remove NA (missing) values
range01 <- function(x, na.rm = T){
(x - min(x, na.rm = na.rm)) /
(max(x, na.rm = na.rm) - min(x, na.rm = na.rm))
}
@christophergandrud
christophergandrud / set_valid_wd.R
Created June 2, 2015 13:48
Sets valid working directory from a vector of possible directories. Useful if you run a script on multiple computers
#' Sets valid working directory from vector of possible directories
#'
#' @param character vector of possible working directores
set_valid_wd <- function(possible) {
for (i in possible) {
if (file.exists(i)) {
setwd(i)
message(sprintf('Working directory set as: %s', i))
}