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
#' An R function for creating simple D3 javascript directed network graphs. | |
#' | |
#' d3SimpleNetwork creates simple D3 javascript network graphs. | |
#' | |
#' @param data a data frame object with three columns. The first two are the names of the linked units. The third records an edge value. (Currently the third column doesn't affect the graph.) | |
#' @param Source character string naming the network source variable in the data frame. If \code{Source = NULL} then the first column of the data frame is treated as the source. | |
#' @param Target character string naming the network target variable in the data frame. If \code{Target = NULL} then the second column of the data frame is treated as the target. | |
#' @param height numeric height for the network graph's frame area. | |
#' @param width numeric width for the network graph's frame area. | |
#' @param file a character string of the file name to save the resulting graph. If a file name is given a standalone webpage is created, i.e. with a header and footer. If \code{file = NULL} then |
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)) |
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
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
# @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
# 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
## 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
// Use Gists to store code you would like to remember later on | |
console.log(window); // log the "window" object to the console |