Skip to content

Instantly share code, notes, and snippets.

View nassimhaddad's full-sized avatar

Nassim Haddad nassimhaddad

View GitHub Profile
@nassimhaddad
nassimhaddad / df_to_html.R
Created February 27, 2013 17:25
Various html output tricks
library(xtable)
print_df <- function(df){
print(xtable(df), type = "html", include.rownames = FALSE)
}
@nassimhaddad
nassimhaddad / README.md
Last active December 13, 2015 23:49
pie chart in ggplot2

Here I'll drop ggplot2 tricks ...

Table of Contents:

  • piechart.R: getting the order of the colors right is quite a task
  • themes.R: ggthemes is really useful
@nassimhaddad
nassimhaddad / README.md
Last active December 13, 2015 23:48
Super fast group by, code from: http://stackoverflow.com/questions/3685492/r-speeding-up-group-by-operations. Especially, the `id()` function could prove very useful in building indices.
@nassimhaddad
nassimhaddad / insertRow.R
Created February 16, 2013 19:10
insert a row to a data frame at a wanted position
insert_row <- function(existingDF, newrow, r){
r <- as.numeric(r)
if (length(newrow) != ncol(existingDF)){
stop("Length of row doesn't match.")
}
if (r == 1){
existingDF <- rbind(newrow,existingDF)
} else{
@nassimhaddad
nassimhaddad / create_shema.R
Last active December 13, 2015 19:38
Simple relational database shema creator with R. The resulting database (in SQLite) can easily be used using the Firefox add-in "SQLite Manager" (which supports foreign keys). Other options (not tested): http://www.sqlmaestro.com/products/sqlite/phpgenerator/ http://www.sqlitemanager.org/ https://code.google.com/p/phpliteadmin/ http://sqlitestud…
library(yaml)
# loads the database shema description
db_list <- yaml.load_file("sample3.yaml")
# create the database
con <- dbConnect(SQLite(), "new_db.sqlite")
# create the tables one by one
for (i in 1:length(db_list)){
data.files <- dir('data')
for (data.file in data.files)
{
filename <- file.path('data', data.file)
variable.name <- sub('\\.csv$', '', data.file, ignore.case = TRUE, perl = TRUE)
assign(variable.name, read.csv(filename, header = TRUE, sep = ','))
}
@nassimhaddad
nassimhaddad / CCustomCumsum.R
Created February 11, 2013 19:54
custom "filter" = cumulative sum with multiplier, written with inline C. It is incredibly fast
# looping through vector ####
library(inline)
sign <- signature(x="numeric", n="integer", d="numeric")
code <- "
for (int i=1; i < *n; i++) {
x[i] = x[i-1]*d[0] + x[i];
}"
c_fn <- cfunction(sign,
@nassimhaddad
nassimhaddad / r2html_into_variable.R
Created February 7, 2013 12:01
generate html code with R
library(R2HTML)
# setup a temporaty file to store the code
fileName <- 'temp.html'
.HTML.file = file.path(getwd(), fileName)
# make a title
HTML(as.title("Title of my report"), append = FALSE)
# add space
HTMLhr()
@nassimhaddad
nassimhaddad / gist:4708636
Created February 4, 2013 18:41
reorder factor levels. change the order of factors without changing their value
df$g <- factor(df$g, levels = letters[4:1])
@nassimhaddad
nassimhaddad / regex.R
Last active December 12, 2015 02:48
Tricks with regular expressions
# tricks with regular expressions ####
# insert character after a given pattern: use \\1
sub("([[:digit:]]{4})", "\\15", "12346789")