Skip to content

Instantly share code, notes, and snippets.

View trcook's full-sized avatar

Tom Cook trcook

View GitHub Profile
# Ways to execute a shell script in Ruby
# Example Script - Joseph Pecoraro
cmd = "echo 'hi'" # Sample string that can be used
# 1. Kernel#` - commonly called backticks - `cmd`
# This is like many other languages, including bash, PHP, and Perl
# Returns the result of the shell command
# Docs: http://ruby-doc.org/core/classes/Kernel.html#M001111
@trcook
trcook / Asset.r
Created April 3, 2013 13:58
knitr child documents
<<child-demo, child='methods.rnw', eval=TRUE>>=
@
###
#### Put this in the child document to identify the parent:
@trcook
trcook / Asset.R
Created April 3, 2013 13:58
expand factor into dichotomous variables
require(reshape2)
dcast(pds2,codeyear~lparty,length,value_var="codeyear")->tmp
# the codeyear variable is the row identifier. use this to remerge back into the dataset
@trcook
trcook / liklihood.r
Created April 3, 2013 13:58
likelihood functions
# The liklihood of a Poission Distribution
exp(-theta*length(data))*theta^sum(data))/prod(factorial(data)
(factorial(n)/(factorial(k)factorial((k-n))))p^k * (1-p)^(n-k)
@trcook
trcook / Asset.rb
Created April 3, 2013 13:58
regex hash
my_input=File.open("my_input.txt","r") # load some file as type: File
fixb=Hash.new
fixb.merge! "\t" => "&",
"\n"=>"\\\\"+"\\\\"+"\n",
"\""=>""
my_output=my_input.read.to_s
fixb.each_pair do |key, val|
my_output.gsub!(key,val)
@trcook
trcook / Asset.R
Created April 3, 2013 13:58
R colors for graphics
# Display pretty color pallete:
RColorBrewer::display.brewer.pal(5,"Set1")
# Get the hex values for the color palette
RColorBrewer::brewer.pal(5,"Set1")
# set these to names that you can use later. Ex:
red<-"#E41A1C"
blue<-"#377EB8"
@trcook
trcook / Asset.R
Created April 3, 2013 13:58
Find vars in object with grep
mysearch <- function (OBJ,searchterm="year") {
p<-ls(OBJ)[grep(searchterm,ls(OBJ))]
return(p)
}
@trcook
trcook / Asset.R
Created April 3, 2013 13:58
Get class for all vars in data.frame
sapply(names(ds2),function(x){
eval(substitute(class(ds2$x),env=list(x=x)))
})
@trcook
trcook / Asset.R
Created April 3, 2013 13:58
Convert Object Name to String
myfun <- function(x) deparse(substitute(x))
# so myfun(myQOG) will give output "myQOG"
@trcook
trcook / Asset.R
Created April 3, 2013 13:58
String to Object Names with eval parse
paste("ds2$",names(ds2)[1],sep="")->x
eval(parse(text=(x))) # will output the contents of the object name constructed in x, in this case, it will output the individual names of all cases in a dataset.