Skip to content

Instantly share code, notes, and snippets.

dsparks

Block or report user

Report or block dsparks

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@dsparks
dsparks / Test.md
Created Feb 14, 2014
A test, which I can delete.
View Test.md

This is a text. but changed this feel free to delete this.

  • WHy
  • Don't
  • I Try
  • A Bulleted list?

I added this.

@dsparks
dsparks / rationalFractionApproximator.R
Created Oct 7, 2014
Approximate any decimal with a rational fraction
View rationalFractionApproximator.R
rationalFractionApproximator <- function(dec, maxDenom = 1000){
denomSeq <- 1:maxDenom
impliedNumerator <- dec * denomSeq
roundNumerator <- round(impliedNumerator)
absError <- abs(roundNumerator / denomSeq - dec)
minMinimand <- which.min(absError * denomSeq)
minDenom <- denomSeq[minMinimand]
minNumer <- roundNumerator[minMinimand]
line2 <- paste0(minNumer, " / ", minDenom, " = ", minNumer / minDenom)
@dsparks
dsparks / Completion Projector.R
Created Feb 9, 2011
to project the time at which a loop will be finished
View Completion Projector.R
Projector <- function(st, loopover, iteration){
Currently <- Sys.time()
Elapsed <- (Currently - st) * length(loopover)/which(iteration == loopover)
units(Elapsed) <- "mins"
return(st + Elapsed * 60)
}
StartTime <- Sys.time()
for(oo in c(1:50)){
print(oo)
@dsparks
dsparks / GroupRank.R
Created Feb 9, 2011
rank within a group, then paste back together in original order
View GroupRank.R
GroupRank <- function(x, group){ unsplit(lapply(split(-x, group), rank), group) }
@dsparks
dsparks / Reorderer.R
Created Feb 9, 2011
convert string to factor, ordered by another variable
View Reorderer.R
Reorderer <- function(tofactor, inorder){ factor(tofactor, levels = tofactor[order(inorder)]) }
@dsparks
dsparks / DropTags.R
Created Feb 9, 2011
remove content inside html tags from string variables
View DropTags.R
DropTags <- function(x){ gsub("<[^>]*>", " ", x) }
@dsparks
dsparks / SpaceTrim.R
Created Feb 9, 2011
remove leading and trailing spaces from a string
View SpaceTrim.R
spaceTrim <- function(x){ gsub("(^ +)|( +$)", "", x) }
@dsparks
dsparks / Pad0.R
Created Feb 9, 2011
create string variable from numeric variable with padding 0s
View Pad0.R
Pad0 <- function(x, mx=NULL, fill=0) {
## pad numeric vars to strings of specified size
lx <- nchar(as.character(x))
mx.calc <- max(lx, na.rm=TRUE)
if (!is.null(mx)) {
if (mx<mx.calc) {
stop("number of maxchar is too small")
}
} else {
mx <- mx.calc
@dsparks
dsparks / CorrelationEllipses.R
Created Feb 9, 2011
correlation plots with ellipses
View CorrelationEllipses.R
# Correlation ellipses
doInstall <- TRUE # Change to FALSE if you don't want packages installed.
toInstall <- c("ellipse")
if(doInstall){install.packages(toInstall, repos = "http://cran.r-project.org")}
lapply(toInstall, library, character.only = TRUE)
# Function to plot colored correlation ellipses
correlationEllipses <- function(cor){
require(ellipse)
View nhl_skaters.R
# Illustrative use of plyr
# LOAD LIBRARIES REQUIRED
library(plyr);
library(XML)
# FIGURE OUT PATTERN OF URL FOR EACH SEASON
url.b1 = 'http://ca.sports.yahoo.com/nhl/stats/byposition?pos=C,RW,LW,D';
You can’t perform that action at this time.