Skip to content

Instantly share code, notes, and snippets.

@hadley
hadley / reproducible.md
Created January 6, 2010 17:33
How to write a reproducible example

How to write a reproducible example.

You are most likely to get good help with your R problem if you provide a reproducible example. A reproducible example allows someone else to recreate your problem by just copying and pasting R code.

There are four things you need to include to make your example reproducible: required packages, data, code, and a description of your R environment.

  • Packages should be loaded at the top of the script, so it's easy to see which ones the example needs.

  • The easiest way to include data in an email is to use dput() to generate

@Sharpie
Sharpie / grainAnalysis.R
Created January 31, 2010 17:44
R code related to graphical analysis of a sieved soil sample.
# Load Data
grainData <- read.csv('grainSize.csv', check.names=F, na.strings='--' )
# Calculate Derived Sample Values
grainData[['Phi Diameter']] <- -log2( grainData[['Grain Diameter']] )
totalWeight <- sum( grainData[['Sample Weight']] )
grainData[["Percent Retained"]] <- grainData[['Sample Weight']] / totalWeight
grainData[["Cumulative Percent"]] <- cumsum( grainData[["Percent Retained"]] )
grainData[['Percent Finer']] <- 1 - grainData[['Cumulative Percent']]
@stephenturner
stephenturner / Rprofile.R
Created August 12, 2011 03:02
Rprofile.R
# To source this file into an environment to avoid cluttering the global workspace, put this in Rprofile:
# my.env <- new.env(); sys.source("C:/PathTo/THIS_FILE.r", my.env); attach(my.env)
#-----------------------------------------------------------------------
# Load packages, set options and cwd, set up database connection
#-----------------------------------------------------------------------
## Load packages
# require(ggplot2) #plotting
# require(plyr) #data manipulation
@benmarwick
benmarwick / grainAnalysis.R
Created December 28, 2011 00:16 — forked from Sharpie/grainAnalysis.R
R code related to graphical analysis of a sieved soil sample.
# Load Data
grainData <- read.csv('grainSize.csv', check.names=F, na.strings='--' )
# Calculate Derived Sample Values
grainData[['Phi Diameter']] <- -log2( grainData[['Grain Diameter']] )
totalWeight <- sum( grainData[['Sample Weight']] )
grainData[["Percent Retained"]] <- grainData[['Sample Weight']] / totalWeight
grainData[["Cumulative Percent"]] <- cumsum( grainData[["Percent Retained"]] )
grainData[['Percent Finer']] <- 1 - grainData[['Cumulative Percent']]
@mrdwab
mrdwab / readtps.R
Created March 17, 2012 16:51
read.tps function for R
read.tps = function(data) {
# Reads the .tps file format produced by TPSDIG
# (http://life.bio.sunysb.edu/morph/ into a single data frame
# USAGE: R> read.tps("filename.tps")
a = readLines(data) # so we can do some searching and indexing
LM = grep("LM", a) # find the line numbers for LM
ID.ind = grep("ID", a) # find the line numbers for ID
# and the ID values, SCALE values, and image names
ID = gsub("(ID=)(.*)", "\\2", grep("ID", a, value=T))
SCALE = gsub("(SCALE=)(.*)", "\\2", grep("SCALE", a, value=T))
@GerHobbelt
GerHobbelt / .gitignore
Created July 8, 2012 14:41
d3.js: force layout; click to group/bundle nodes
# Editor backup files
*.bak
*~
@stevenworthington
stevenworthington / ipak.R
Created July 25, 2012 19:44
Install and load multiple R packages at once
# ipak function: install and load multiple R packages.
# check to see if packages are installed. Install them if they are not, then load them into the R session.
ipak <- function(pkg){
new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])]
if (length(new.pkg))
install.packages(new.pkg, dependencies = TRUE)
sapply(pkg, require, character.only = TRUE)
}
@stephenturner
stephenturner / explore-correlations.r
Created August 27, 2012 22:06
Exploring correlations with R using cor.prob and chart.Correlation
## Correlation matrix with p-values. See http://goo.gl/nahmV for documentation of this function
cor.prob <- function (X, dfr = nrow(X) - 2) {
R <- cor(X, use="pairwise.complete.obs")
above <- row(R) < col(R)
r2 <- R[above]^2
Fstat <- r2 * dfr/(1 - r2)
R[above] <- 1 - pf(Fstat, 1, dfr)
R[row(R) == col(R)] <- NA
R
}
@rmflight
rmflight / tableFigureFuncs.r
Created October 9, 2012 13:54
knitr table and figure functions for Rmd
pasteLabel <- function(preText, inObj, objName, insLink=TRUE){
objNum <- inObj[objName]
useText <- paste(preText, objNum, sep=" ")
if (insLink){
useText <- paste("[", useText, "](#", objName, ")", sep="")
}
useText
}
@rjw57
rjw57 / bayes-arch.txt
Created October 30, 2012 19:11
Bayesian Archaeology paper referecnes
A Bayesian approach to seriation problems in archaeology
http://www.sciencedirect.com/science/article/pii/S0167947303000343
A Bayesian Approach to Ageing Perinatal Skeletal Material from Archaeological Sites: Implications for the Evidence for Infanticide in Roman-Britain
http://www.sciencedirect.com/science/article/pii/S0305440301907769#
Bayesian interpretation of radiocarbon results (thesis)
http://etheses.nottingham.ac.uk/1035/1/359686.pdf
Spatial and Multivariate Analysis, Random Sampling Error, and Analytical Noise: Empirical Bayesian Methods at Teotihuacan, Mexico