Skip to content

Instantly share code, notes, and snippets.

View leeper's full-sized avatar

Thomas J. Leeper leeper

View GitHub Profile
@leeper
leeper / lookfor.R
Last active August 29, 2015 14:00
Search for names, labels, and levels in R
# This is an effort to emulate Stata's `lookfor` in R
lookfor <- function(what, ls_opts = list(), ...){
s <- do.call("ls", ls_opts)
d <- lapply(s, lookin, what = what, ...)
# return value should be a list with the string matching the search, along with details of its position
# big challenge is doing this recursively because, e.g., lists of lists of dataframes would be really difficult to search
class(d) <- 'lookfor'
@leeper
leeper / federalregister.R
Last active August 29, 2015 14:01
Initial ideas for Federal Register API client
# https://www.federalregister.gov/developers/api/v1
# NAMESPACE
# importFrom(RJSONIO, fromJSON)
# importFrom(RCurl, curlPerform, basicTextGatherer)
# S3method(print,fedreg_document)
# S3method(print,fedreg_agency)
fr_search <- function(version='v1', ...) {
@leeper
leeper / readwrite.R
Last active August 29, 2015 14:01
Outline of a more convenient i/o function for R
# call package easyio
library('foreign')
library('tools')
# library('haven') # for spss (sav, por), stata (dta), sas
#library('memisc') # for `spss.portable.file` and `spss.system.file`
#library('hmisc') # for `spss.get`
#library('openxlsx') # for `read.xlsx`
#library('data.table') # for fread
@leeper
leeper / TESS.r
Created May 15, 2014 22:18
TESS Data Archive Scraper
require('XML')
tess_studies_list <- function(){
x <- xmlParse('http://www.tessexperiments.org/previousstudies.html')
}
get_tess_study <- function(id, file){
download.file()
}
@leeper
leeper / by.R
Last active August 29, 2015 14:05
Creating a simple by pipe operator
# function definition
`%by%` <- function(a,b){
s <- substitute(a)
l <- as.list(s)
p <- parse(text=s)
if(length(p)>2) {
l <- l[3:length(l)]
do.call("by", c(list(data=eval.parent(p[2]),
INDICES=b,
FUN=eval.parent(p[1])),
@leeper
leeper / ReversePolish.R
Last active August 29, 2015 14:15
Reverse Polish
rp <- function() {
stack <- numeric()
push <- function(values) stack <<- c(stack, values)
pop <- function() {
a <- stack[length(stack)]
stack <<- stack[-length(stack)]
return(a)
}
reverse <- function() {
r <- readline()
@leeper
leeper / gettext_workflow.txt
Last active August 29, 2015 14:17
Package message translations
1. Install GNU gettext (on Windows, this can be obtained from the Rtools site: http://www.stats.ox.ac.uk/pub/Rtools/goodies/gettext-tools.zip)
2. Run `update_pkg_po()` (https://stat.ethz.ch/R-manual/R-devel/library/tools/html/update_pkg_po.html)
a. Create `/po` if it does not exist
b. Call `xgettext2pot()` to create/update `po/R-pkgname.pot` file
c. All `R-lang.po` files in `/po` are updated from `po/R-pkgname.pot` using `msgmerge`
d. `checkPoFiles()` is called on updated files
e. If check is successful, messages are compiled using `msgfmt` system call and installed in `/inst/po`.
f. In a UTF-8 locale, a ‘translation’ ‘R-en@quot.po’ is created with UTF-8 directional quotes, compiled and installed under ‘inst/po’.
g. If `po/pkg.pot` exists:
i. `/src` is examined to create `/po/pkg.pot`
#' Parse a codebook file with variable and level information.
#'
#' Parses a codebook file where lines starting at column zero (far left) represet
#' variable information (e.g. name, description, type) and indented lines
#' (i.e. lines beginning with white space, either tabs or spaces, etc.) represent factor
#' levels and labels.
#'
#' Note that white space at the beginning and end of each line is stripped before
#' processing that line.
#'
@leeper
leeper / code.R
Last active August 29, 2015 14:17
Reading fixed width format data
read.fwf2 <- function (file, widths, header = FALSE, sep = "\t", skip = 0,
row.names, col.names, n = -1, ...)
{
doone <- function(x) {
x <- substring(x, first, last)
x[!nzchar(x)] <- NA_character_
paste0(x, collapse = sep)
}
if (is.list(widths)) {
recordlength <- length(widths)
@leeper
leeper / aggregate_test.R
Last active August 29, 2015 14:20
Aggregating a data.frame
z <- function(data, FUN = mean, ...) aggregate(. ~ 1, data = data, FUN = FUN, ...)
y <- function(data, FUN = mean, ...) data.frame(t(apply(data, 2, FUN = FUN, ...)))
x <- function(data, FUN = mean, ...) data.frame(t(sapply(data, mean, ...)))
d1 <- data.frame(matrix(1e3, ncol=10))
d2 <- data.frame(matrix(1e6, ncol=10))
d3 <- data.frame(matrix(1e6, ncol=100))
# MEAN