Skip to content

Instantly share code, notes, and snippets.

View gu-mi's full-sized avatar

Gu Mi gu-mi

  • Sanofi
  • Cambridge, MA
View GitHub Profile
## Add an alpha value to a colour
add.alpha <- function(col, alpha=1){
if(missing(col))
stop("Please provide a vector of colours.")
apply(sapply(col, col2rgb)/255, 2,
function(x)
rgb(x[1], x[2], x[3], alpha=alpha))
}
@gu-mi
gu-mi / update_R_pkgs.R
Last active December 15, 2015 20:48
On a Mac OS, re-install R packages installed for R 2.15.X, when upgrading to R 3.0.0
# http://randyzwitch.com/automated-re-install-of-packages-for-r-3-0/
# if using RStudio, make sure to unload all packages that are pre-loaded except for utils:
# we have to use the install.packages() function in utils for the re-installation
# Get currently installed packages
package_df <- as.data.frame(installed.packages("/Library/Frameworks/R.framework/Versions/2.15/Resources/library"))
package_list <- as.character(package_df$Package)
package_list # get a package list installed on R 2.15.X
length(package_list)
@gu-mi
gu-mi / global.R
Created March 21, 2013 06:14 — forked from pssguy/global.R
# load required libraries
library(shiny)
library(plyr)
library(ggplot2)
library(googleVis)
library(reshape2)
####creation of example data on local directory for uploading####
@gu-mi
gu-mi / installation of Cufflinks
Last active December 14, 2015 07:59
Instructions for installing Cufflinks and related tools in Mac OS X
library(sqldf)
sqldf("SELECT
day
, avg(temp) as avg_temp
FROM beaver2
GROUP BY
day;")
# day avg_temp
library(randomForest)
# download Titanic Survivors data
data <- read.table("http://math.ucdenver.edu/RTutorial/titanic.txt", h=T, sep="\t")
# make survived into a yes/no
data$Survived <- as.factor(ifelse(data$Survived==1, "yes", "no"))
# split into a training and test set
idx <- runif(nrow(data)) <= .75
data.train <- data[idx,]
library(stringr)
names(iris)
#[1] "Sepal.Length" "Sepal.Width" "Petal.Length" "Petal.Width" "Species"
names(iris) <- str_replace_all(names(iris), "[.]", "_")
names(iris)
#[1] "Sepal_Length" "Sepal_Width" "Petal_Length" "Petal_Width" "Species"
s <- c("Go to Heaven for the climate, Hell for the company.")
str_extract_all(s, "[H][a-z]+ ")
@gu-mi
gu-mi / nested-loops.cpp
Last active December 11, 2015 06:09
Using nested loops to iterate elements in a 2-dim array of integers
#include <iostream>
using namespace std;
int main()
{
const int MAX_ROWS = 3;
const int MAX_COLS = 4;
// 2-D array of integers
int MyInts[MAX_ROWS][MAX_COLS] = { {34, -1, 879, 22},
@gu-mi
gu-mi / Spirograph.R
Created December 30, 2012 23:22
This R snippet is retrieved from http://menugget.blogspot.com/2012/12/spirograph-with-r.html on Dec. 30, 2012
#spirographR() will produce either a hypotrochoid or an epitrochoid.
#'A' is a circle of radius 'A.RADIUS'
#'B' is a circle of radius 'B.RADIUS' travelling around 'A'
#'C' is a point relative to the center of 'B' which rotates with the turning of 'B'.
#'BC' is the distance from the center of 'B' to 'C'
#'A.REV' is the number of revolutions that 'B' should travel around 'A'
#'N.PER.A.REV' is the number of radial increments to be calculated per revolution
#'A.CEN' is the position of the center of 'A'
#
@gu-mi
gu-mi / voronoi_raster.R
Created November 28, 2012 19:14 — forked from dsparks/voronoi_raster.R
Image Manipulation, Part 3
doInstall <- TRUE # Change to FALSE if you don't want packages installed.
toInstall <- c("ReadImages", "reshape", "ggplot2")
if(doInstall){install.packages(toInstall, repos = "http://cran.r-project.org")}
lapply(toInstall, library, character.only = TRUE)
# Image URL:
allImageURLs <- c("http://media.charlesleifer.com/blog/photos/thumbnails/akira_940x700.jpg",
"http://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg/402px-Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg",
"http://upload.wikimedia.org/wikipedia/commons/thumb/e/e9/Official_portrait_of_Barack_Obama.jpg/441px-Official_portrait_of_Barack_Obama.jpg",
"http://cache.boston.com/universal/site_graphics/blogs/bigpicture/obama_11_05/obama22_16604051.jpg",