Skip to content

Instantly share code, notes, and snippets.

View nassimhaddad's full-sized avatar

Nassim Haddad nassimhaddad

View GitHub Profile
@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 / 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
with open(fname,'r') as fin:
A = fin.readlines()
@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
@nassimhaddad
nassimhaddad / download_ftp.R
Created May 28, 2014 05:24
Download files from ftp
address<- 'ftp://user:[email protected]'
items <- strsplit(getURL(address, .opts=curlOptions(ftplistonly=TRUE)), "\n")[[1]]
filename <- items[3]
bin = getBinaryURL(paste0(address, "/",filename))
writeBin(bin, filename)
@nassimhaddad
nassimhaddad / qgraph_viz.R
Last active June 15, 2017 17:39
Good looking network or graph visualization, using qgraph. For network or node metrics, use igraph.
# notes: it's all about fine-tuning the details:
# good color palette (-> google 'color palette')
# white borders and font
# gray edges (even better when weighted)
#
library(qgraph)
adj=matrix(sample(0:1,10^2,TRUE,prob=c(0.8,0.2)),nrow=10,ncol=10)
groups <- list(group1 = 1:4,
group2 = 5:7,
group3 = 8:10)
@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 / 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)))
cut_equal <- function(x, n = 3){
q <- quantile(x, prob= seq(0,1,length.out = n+1 ))
cut(x,
breaks = as.numeric(q),
include.lowest=TRUE, labels = 1:n)
}
@nassimhaddad
nassimhaddad / launcher.R
Created November 26, 2013 07:38
launch and kill R process from R
# getting process id so we can kill it
pid <- system( "Rscript sample.R 500 > test2.txt &
echo $!", intern = TRUE)
system(paste(sprintf("kill %s", pid)))