Skip to content

Instantly share code, notes, and snippets.

View nassimhaddad's full-sized avatar

Nassim Haddad nassimhaddad

View GitHub Profile
@nassimhaddad
nassimhaddad / footnote_ggplot2.r
Created April 1, 2014 15:08
add footnote to ggplot chart
# source: http://bigdata-analyst.com/best-way-to-add-a-footnote-to-a-plot-created-with-ggplot2.html
library(gridExtra)
g <- arrangeGrob(p, sub = textGrob("Footnote", x = 0, hjust = -0.1, vjust=0.1, gp = gpar(fontface = "italic", fontsize = 18)))
@nassimhaddad
nassimhaddad / s3example.R
Last active August 29, 2015 14:00
some very minimal examples of object oriented programming with R
myfun <- function(){
out <- list()
out$x <- LETTERS
out$y <- "hello world"
class(out) <- "vtable"
out
}
print.vtable <- function(x){
cat("object of class vtable")
print(str(x$x))
@nassimhaddad
nassimhaddad / download_ftp.R
Created May 28, 2014 05:24
Download files from ftp
address<- 'ftp://user:pw@sftp2.server.com'
items <- strsplit(getURL(address, .opts=curlOptions(ftplistonly=TRUE)), "\n")[[1]]
filename <- items[3]
bin = getBinaryURL(paste0(address, "/",filename))
writeBin(bin, filename)
@nassimhaddad
nassimhaddad / 0_reuse_code.js
Created May 28, 2014 05:38
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
with open(fname,'r') as fin:
A = fin.readlines()
@nassimhaddad
nassimhaddad / importantFunctions.R
Created June 16, 2014 06:19
date conversion in R
as.Date(string, "%Y")
# for a list of options, see table in
# http://blog.mollietaylor.com/2013/08/date-formats-in-r.html
@nassimhaddad
nassimhaddad / get_best_match.R
Created June 16, 2014 11:38
get best match in a fuzzy lookup
input <- c("kitten", "dog")
candidates <- c("sitting", "sittingAround", "doggy")
get_best_match <- function(input, candidates, repeatInput = FALSE){
distM <- adist(as.character(input), as.character(candidates))
if (repeatInput){
out <- data.frame(input,
bestMatch = candidates[apply(distM, 1, which.min)],
distance = apply(distM, 1, min))
} else{
@nassimhaddad
nassimhaddad / addConsol.py
Created June 17, 2014 09:54
launch ipython console alongside notebook
%qtconsole
@nassimhaddad
nassimhaddad / interactiveScatter.R
Last active August 29, 2015 14:02
interactive scatterplot in r with tooltips
interactiveScatter <- function(xvar, yvar, data,
tooltip = c(xvar, yvar), title = "",
lmline = TRUE){
require(rCharts)
x <- xvar
y <- yvar
tooltipString <- paste0("#!function(item){return ",
@nassimhaddad
nassimhaddad / perfCompare.R
Created June 29, 2014 07:49
compare performance of models
# code copied from:
# http://heuristically.wordpress.com/2009/12/23/compare-performance-machine-learning-classifiers-r/
# load the ROCR package which draws the ROC curves
require(ROCR)
# create an ROCR prediction object from rpart() probabilities
x.rp.prob.rocr <- prediction(x.rp.prob[,2], BreastCancer[ind == 2,'Class'])
# prepare an ROCR performance object for ROC curve (tpr=true positive rate, fpr=false positive rate)
x.rp.perf <- performance(x.rp.prob.rocr, "tpr","fpr")