Instantly share code, notes, and snippets.

# dsparks

• Sort options
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?

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)
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)
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) }
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)]) }
Created Feb 9, 2011
remove content inside html tags from string variables
View DropTags.R
 DropTags <- function(x){ gsub("<[^>]*>", " ", x) }
Created Feb 9, 2011
remove leading and trailing spaces from a string
View SpaceTrim.R
 spaceTrim <- function(x){ gsub("(^ +)|( +\$)", "", x) }
Created Feb 9, 2011
create string variable from numeric variable with padding 0s