Skip to content

Instantly share code, notes, and snippets.

View anhqle's full-sized avatar

Anh Le anhqle

View GitHub Profile
@anhqle
anhqle / animated-choropleth.R
Last active March 28, 2017 03:44
[ggplot2, animation] Create animated choropleth map
rm(list=ls())
toInstall <- c("ggplot2", "maptools", "animation", "rgdal")
lapply(toInstall, library, character.only=TRUE)
# Load and clean World Governance Indicators (WGI) data
# Can be downloaded here https://dl.dropboxusercontent.com/u/18955935/wgi_final.RData
# Most of the cleaning involving matching World Bank naming system with that of the map
load('./data/wgi_cleaned.RData')
# Load world map shape file (Google "TM_WORLD_BORDERS_SIMPL-0.3")
@anhqle
anhqle / correlation-table.R
Last active December 16, 2020 02:17
R code to create pretty correlation table (+ Latex output) Source: somewhere on the Internet
library(xtable)
corstarsl <- function(x){
require(Hmisc)
x <- as.matrix(x)
R <- rcorr(x)$r
p <- rcorr(x)$P
## define notions for significance levels; spacing is important.
mystars <- ifelse(p < .001, "***", ifelse(p < .01, "** ", ifelse(p < .05, "* ", " ")))
# Truly the most ridiculous thing I could think of.
doInstall <- TRUE # Change to FALSE if you don't want packages installed.
toInstall <- c("XML", "png", "devtools", "RCurl")
if(doInstall){install.packages(toInstall, repos = "http://cran.r-project.org")}
lapply(toInstall, library, character.only = TRUE)
# Some helper functions, lineFinder and makeTable
source_gist("818983")
source_gist("818986")
doInstall <- TRUE
toInstall <- c("twitteR", "dismo", "maps", "ggplot2")
if(doInstall){install.packages(toInstall, repos = "http://cran.us.r-project.org")}
lapply(toInstall, library, character.only = TRUE)
<<<<<<< HEAD
# make some change
=======
>>>>>>> 0f79b1430f148bded0527f9c67906de7c0667fce
@anhqle
anhqle / subset.sql
Created November 13, 2013 00:11 — forked from andybega/subset.sql
CREATE TABLE afg_greg AS
SELECT *
FROM greg
WHERE ( ST_contains((SELECT the_geom FROM afg_adm0), geom) OR
ST_overlaps((SELECT the_geom FROM afg_adm0), geom) );
@anhqle
anhqle / csv_to_sql.sh
Created July 16, 2014 15:06
A Shell script that import CSV into MySQL by automatically creating the table structure. This is more convenient than the `LOAD DATA INFILE` command, which requires us to manually create the MySQL database first
#!/bin/sh
MYSQL_ARGS="--defaults-file=/etc/mysql/debian.cnf"
DB="mbctest"
DELIM=";"
CSV="$1"
TABLE="$2"
[ "$CSV" = "" -o "$TABLE" = "" ] && echo "Syntax: $0 csvfile tablename" && exit 1
# verb.coop
SELECT YEAR(e.event_date) AS year
, MONTH(e.event_date) AS month
, cSource.ISOA3Code AS source_country
, cTarget.ISOA3Code AS target_country
, COUNT(*) AS verb_coop
FROM simple_events e
JOIN eventtypes t ON e.eventtype_id = t.eventtype_ID
JOIN countries cSource ON e.source_country_id = cSource.id
JOIN countries cTarget ON e.target_country_id = cTarget.id
@anhqle
anhqle / rmysql_window_install
Last active August 16, 2016 18:09
How to install RMySQL on Windows
## A Guide on Install RMySQL on Windows
I have encountered various error messages during installation, including
* mysql.h not found
* cygwin path warning
Just install a fresh install of MySQL Server (no need for Workbench and Connector, or anything else).
Here are the steps:
1. Download MySQL installer (which contains all products) from [here](http://dev.mysql.com/downloads/windows/installer/)
@anhqle
anhqle / functions.R
Created February 2, 2015 21:52
A bunch of convenience R functions I made for myself
# Install and load packages
f_install_and_load <- function(packs) {
new.packs <- packs[!(packs %in% installed.packages()[ ,"Package"])]
lapply(new.packs, install.packages, repos="http://cran.rstudio.com/", dependencies=TRUE)
lapply(packs, library, character.only=TRUE)
}
# Scale and center a numeric vector
f_center_and_scale <- function(vector, num.sd = 2) {
# num.sd is how many sd to divide by
# Multiple plot function
#
# ggplot objects can be passed in ..., or to plotlist (as a list of ggplot objects)
# - cols: Number of columns in layout
# - layout: A matrix specifying the layout. If present, 'cols' is ignored.
#
# If the layout is something like matrix(c(1,2,3,3), nrow=2, byrow=TRUE),
# then plot 1 will go in the upper left, 2 will go in the upper right, and
# 3 will go all the way across the bottom.
#