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
# 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) | |
} |
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
// Use Gists to store code you would like to remember later on | |
console.log(window); // log the "window" object to the console |
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
## 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 | |
} |
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
# 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']] |
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
# @author: Michael J Bommarito II | |
# @date: Feb 20, 2011 | |
# @email: michael.bommarito@gmail.com | |
# @packages: gridExtra, ggplot2 | |
library(gridExtra) | |
library(ggplot2) | |
setwd('/data/workspace/blog/cn220/') |
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
ks.default <- function(rows) seq(2, max(3, rows %/% 4)) | |
many_kmeans <- function(x, ks = ks.default(nrow(x)), ...) { | |
ldply(seq_along(ks), function(i) { | |
cl <- kmeans(x, centers = ks[i], ...) | |
data.frame(obs = seq_len(nrow(x)), i = i, k = ks[i], cluster = cl$cluster) | |
}) | |
} | |
all_hclust <- function(x, ks = ks.default(nrow(x)), point.dist = "euclidean", cluster.dist = "ward") { |
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
ks.default <- function(rows) seq(2, max(3, rows %/% 4)) | |
many_kmeans <- function(x, ks = ks.default(nrow(x)), ...) { | |
ldply(seq_along(ks), function(i) { | |
cl <- kmeans(x, centers = ks[i], ...) | |
data.frame(obs = seq_len(nrow(x)), i = i, k = ks[i], cluster = cl$cluster) | |
}) | |
} | |
all_hclust <- function(x, ks = ks.default(nrow(x)), point.dist = "euclidean", cluster.dist = "ward") { |
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
library(sqldf) | |
sqldf("SELECT | |
day | |
, avg(temp) as avg_temp | |
FROM beaver2 | |
GROUP BY | |
day;") | |
# day avg_temp |
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
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)) |
OlderNewer