Skip to content

Instantly share code, notes, and snippets.

View vsbuffalo's full-sized avatar

Vince Buffalo vsbuffalo

View GitHub Profile
My message:
This was tweeted recently by an R user, and after trying to figure out why this is, I am stumped:
> a <- 5; b <- 1; c <- 4
> f <- function (n) for (i in 1:n) d <- 1/{a*{b+c}}
> g <- function (n) for (i in 1:n) d <- 1/(a*(b+c))
> system.time(f(1000000))
user system elapsed
3.92 0.00 3.94
> system.time(g(1000000))
user system elapsed
@vsbuffalo
vsbuffalo / trace_calls.R
Created August 24, 2010 21:59
trace_calls.R
tracked <- new.env()
makeCounted <- function(fun, loud=FALSE) {
assign(fun, TRUE, envir=tracked)
calls <- 0
countedFun <- function(...) {
calls <<- .Primitive('+')(1, calls)
if (loud)
cat('call made to:', fun, '\n')
.Primitive(fun)(...)
@vsbuffalo
vsbuffalo / makeURL.R
Created November 20, 2010 23:32
An abstraction to make URLs from named R lists, rather than doing in-place string concatenation
a <- list(id=242, 'session_id'='ah876fh3', main='page.html')
makeURL <-
# Given a named list of key/values, concatenate to make a URL. The
# value of the entry with key 'main' is treated as the HTML page.
function(pieces) {
m <- which(names(pieces) == 'main')
keys.values <- paste(names(pieces[-m]), pieces[-m], collapse='&', sep='=')
return(paste(pieces[[m]], keys.values, sep='?'))
}
@vsbuffalo
vsbuffalo / local_munging.R
Created November 30, 2010 17:21
This is how I prefer to do data munging in R
d <- local({
s <- apply(obj$qual.freqs[, -1], 1, binsample)
tmp <- t(s)
nr <- nrow(tmp)
dim(tmp) <- c(ncol(tmp)*nrow(tmp), 1)
tmp <- as.data.frame(cbind(1:nr, tmp))
colnames(tmp) <- c('position', 'quality')
return(tmp)
})
@vsbuffalo
vsbuffalo / Makefile
Created December 8, 2010 22:43
R package Makefile template
# R package Makefile template
# Vince Buffalo <vsbuffaloAAAAA@gmail.com> (with poly-A tail removed)
#
# Replace 'package' with the name of your package (which should be a
# directory). My layout for package development is:
# package_build
# - TODO
# - Readme.md (for Github)
# - package (actual R package directory)
# - Makefile (this)
@vsbuffalo
vsbuffalo / integer_types.R
Created February 6, 2011 09:24
Give R hints when it's working with integers
# If you have a long vector of integers to compare against an integer literal,
# specify that the numerical literal is an integer rather than letting
# R coerce the entire vector to doubles.
x <- as.integer(floor(rnorm(1000000, 0, 1000)))
system.time(x < 0)
# user system elapsed
# 0.014 0.000 0.014
system.time(x < 0L)
@vsbuffalo
vsbuffalo / .profile
Created May 4, 2011 20:16
My Bash Profile
## Vince Buffalo's .profile
## Some fun first:
curl --connect-timeout 1 -Is slashdot.org | egrep '^X-(F|B)' | sed 's/^X-//'
## Set environmental variables
export PATH=/opt/local/bin:/opt/local/sbin:/usr/local/bin/:$PATH
export CLICOLOR=1
export EDITOR="emacsclient"
export LSCOLORS="gxfxcxdxbxegedabagacad"
@vsbuffalo
vsbuffalo / .screenrc
Created May 18, 2011 00:56
My screenrc
shell -$SHELL
startup_message off
defscrollback 30000
autodetach on
hardstatus on
hardstatus alwayslastline
altscreen on
screen -t irc 0 /opt/local/bin/irssi
@vsbuffalo
vsbuffalo / list-ess-vars.el
Created May 21, 2011 00:23
List all ESS variables in the current buffer.
;; From Michael Hannon and Vince Buffalo
(require 'cl)
(defun flatten (list) ; From `misc-fns.el'.
"Flatten LIST, returning a list with the atoms in LIST at any level.
Also works for a consp whose cdr is non-nil.
Borrowed from: http://www.emacswiki.org/emacs/lib-requires.
@vsbuffalo
vsbuffalo / pasilla-test.R
Created March 7, 2012 18:25
A comparison of edgeR (with common and tagwise dispersion estimation) with DESeq
## analysis.R -- looking at how similar the results of edgeR and DESeq are
## vsbuffaloAAAAA@gmail.com sans poly-A tail
library(pasilla)
library(DESeq)
library(edgeR)
library(ggplot2)
library(limma)