This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
knitr::opts_knit$set(stop_on_error = 2L) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#' 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#' 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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ---------------------------------------------------------------------------- # | |
# 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#' 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ---------------------------------------------------------------------------- # | |
# 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#' 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#' 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 | |
#' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#' 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)) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#' 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)) | |
} |