Skip to content

Instantly share code, notes, and snippets.

View leeper's full-sized avatar

Thomas J. Leeper leeper

View GitHub Profile
rm(list = ls())
library(tidyverse)
library(estimatr)
N <- 1000
dat <-
data.frame(
Y = rnorm(N),
@bgall
bgall / r_na_examples
Last active July 6, 2017 18:32
Demonstrates the idiosyncratic behavior of NA values in R
# The below demonstrates the madness of R's treatment of NA values.
# Some examples taken from https://stackoverflow.com/questions/25100974/na-matches-na-but-is-not-equal-to-na-why/25101796
# Logical examples
NA %in% NA
# [1] TRUE
NA == NA
@leeper
leeper / ttttable.R
Last active May 19, 2021 22:38
Grammar of Tables?
# ttable: a grammar of tables
# https://gist.github.com/leeper/f9cfbe6bd185763762e126a4d8d7c286
# aggregate/summarize
# arrange
# annotation (metadata features)
# theme
@artemklevtsov
artemklevtsov / .gitlab-ci.yml
Last active January 12, 2024 04:14
Testing R package with GitLab CI (with code coverage)
variables:
CODECOV_TOKEN: "CODECOV_TOKEN_STRING"
_R_CHECK_CRAN_INCOMING_: "false"
_R_CHECK_FORCE_SUGGESTS_: "true"
APT_PKGS: "libcurl4-openssl-dev libssh2-1-dev libssl-dev libxml2-dev zlib1g-dev git"
before_script:
- apt-get update
- apt-get install -y --no-install-recommends ${APT_PKGS}
- apt-get install -y --no-install-recommends qpdf pandoc pandoc-citeproc
@jennybc
jennybc / polygraphing-films.R
Created April 9, 2016 16:46
Get polygraphing's film data into R
## http://polygraph.cool/films/
## https://github.com/matthewfdaniels/scripts
x <- "https://raw.githubusercontent.com/matthewfdaniels/scripts/master/data/character_list5.csv"
characters <- read.csv(x, na.strings = c("NULL", "?"),
fileEncoding = "ISO-8859-1", stringsAsFactors = FALSE)
## some ages are clearly (negative) birth years ... oops
characters$age[!is.na(characters$age) & characters$age < 0] <- NA
characters$age[!is.na(characters$age) & characters$age > 105] <- NA
library(inline)
library(stringi)
#' Replace a substring of a string
#'
#' @param str string the source string that you want to replace characters in
#' (a character vector of length 1)
#' @param repl another string (this will be copied into \code{str})
#' @param pos position of the first character to be replaced
#' @param len number of characters to replace (if the string is shorter,
@hrbrmstr
hrbrmstr / newpkg.sh
Created December 27, 2015 17:56
R package skeleton maker
#!/bin/bash
# AUTHOR: Bob Rudis <bob@rudis.net> (@hrbrmstr)
# LICENSE: MIT + file zLICENSE
TODAY=`date +%Y-%m-%d`
TODAY_MD=`date +%B\ %d,\ %Y`
YEAR=`date +%Y`
PACKAGENAME=$1
@leeper
leeper / KVF.R
Last active August 29, 2015 14:26
A key-value alternative to UNF
# The UNF algorithm provides a clearly defined specification for creating a data hash.
# The algorithm has many strengths, including the fact that it is file format-independent,
# controls for floating point and rounding errors, and is independent of the column-
# organization of a dataset (i.e., variable order is irrelevant).
#
# A weakness, however, is that UNF is row-order sensitive. This is useful in terms of
# data subsetting (i.e., a subset of a dataset produces a different UNF than the full
# dataset), but it produces a weakness when two identical datasets are arranged in
# different row orders. Here's a simple example:
@hrbrmstr
hrbrmstr / ghelp.R
Last active August 29, 2015 14:23
github search in rstudio viewer pane - source it and try it with ghelp("vapply")
ghelp <- function(topic, in_cran=TRUE) {
require(htmltools) # for getting HTML to the viewer
require(rvest) # for scraping & munging HTML
# github search URL base
base_ext_url <- "https://github.com/search?utf8=%%E2%%9C%%93&q=%s+extension%%3AR"
ext_url <- sprintf(base_ext_url, topic)
# if searching with user:cran (the default) add that to the URL
@andrewheiss
andrewheiss / limesurvey_remote_control.R
Created June 5, 2015 01:34
Access LimeSurvey RemoteControl API with R
library(httr)
library(jsonlite)
library(base64enc)
#----------------------------------------
# Functions to access RemoteControl API
#----------------------------------------
get_session_key <- function(username, password) {
body.json = list(method = "get_session_key",
id = 1,