Skip to content

Instantly share code, notes, and snippets.

@andybega
andybega / duke-grade-infl.R
Last active December 17, 2015 02:09
Plot Duke average GPA from 1932 on
library(XML)
library(plyr)
# Get and format html file
duke.html <- readLines("http://www.gradeinflation.com/Duke.html")
duke.doc <- htmlParse(duke.html)
# Get table as data frame
duke <- readHTMLTable(duke.doc, header=F, as.data.frame=F)
duke <- data.frame(duke, stringsAsFactors=F)
@andybega
andybega / plotMissing.R
Created May 24, 2013 15:36
Function to visualize missing values in a data frame or in a data frame representing panel data (or any other 2 dimensions).
plotMissing <- function(data, dim1=NULL, dim2=NULL, na.id=NULL) {
require(ggplot2)
require(grid)
# Function to plot missing values in a data frame, or to plot missing values
# in a data frame representing panel data (i.e. against 2 dimensions)
# Leave dim1 and dim2 NULL to plot missing values in data frame.
if (xor(is.null(dim1), is.null(dim2))) stop("Specify either both or no dims.")
# Set colors for possible data values
myCol <- c("#DCDCDC", "#32CD32", "#CD4F39")
@andybega
andybega / subset.sql
Created June 4, 2013 18:24
My first real SQL query in PostGIS. It subsets the geo-referenced ethnic groups (GREG) data to only groups that are present in Afghanistan (instead of including hundreds of groups for the entire world).
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) );
@andybega
andybega / geonames-city-coord.R
Last active June 8, 2022 17:07
Look up coordinates for city names
library(geonames)
# list of city names (in Afghanistan)
cities <- c("Gereshk", "Lashkar Gah", "Marjah", "Nad-e Ali")
-5L), class = "data.frame")
# conveninence function to look up and format results
GNsearchAF <- function(x) {
res <- GNsearch(name=x, country="AF")
return(res[1, ])
}
@andybega
andybega / count-model-bug.R
Created August 16, 2013 18:01
Protest models from Mike, produces count plots that look really weird.
#
#
# Original code from Mike:
#
#
# mdw attempts at modeling Protests & Demonstrations
# August 13, 2013
library(wicews)
@andybega
andybega / egy.points.R
Created September 25, 2013 20:06
Point data for Egypt ggmap example
egy.points <-
structure(list(Latitude = c(30.0626, 30.0626, 30.0626, 25, 30.5,
30.1219, 29.0744, 31.4044, 29.9737, 30.0626, 31.4, 31.2, 29,
30.6, 28.1, 26.1642, 24.0934, 25.6989, 27, 30.0453, 30.6043,
30.6803, 30.4658, 31.2565, 30.1219, 27.7667, 31.0364, 30.0273,
31.4044, 29.7, 27.2, 26.5, 29.5, 26, 28.501, 27.9333, 30.5, 27.2574,
27, 30.2986, 30.5333, 29.7, 27.2167, 30.7885, 25.0757, 30.9,
30.7, 30.9, 24.0934, 29.9978, 31.1, 30.15, 22.3457, 25.9141,
30.6, 31.35, 26.1642, 28.3121, 26.55, 30.9251, 31.0392, 30.5,
27, 29.3, 26.0495, 31, 26.0549, 26.1247, 30.3, 26.7292, 30.0626,
@andybega
andybega / egy-ggmap-example.R
Created September 25, 2013 21:21
Quick map in R using ggmap
library(ggmap)
library(source.gist)
# Get data frame with coordinates for points
source.gist("6705264")
head(egy.points)
# Get background map
egy.map <- get_map(location=c(lon=30, lat=26), zoom=6, maptype="terrain", filename="~/Desktop/ggmapTemp")
p <- ggmap(egy.map) +
@andybega
andybega / turkey-prot-points.R
Last active December 24, 2015 07:39
GDELT protest data for Turkey from January 2011 to September 2013.
turkey.prot <-
structure(list(source = c("icews", "icews", "icews", "icews",
"icews", "icews", "icews", "icews", "icews", "icews", "icews",
"icews", "icews", "icews", "icews", "icews", "icews", "icews",
"icews", "icews", "icews", "icews", "icews", "icews", "icews",
"icews", "icews", "icews", "icews", "icews", "icews", "icews",
"icews", "icews", "icews", "icews", "icews", "icews", "icews",
"icews", "icews", "icews", "icews", "icews", "icews", "icews",
"icews", "icews", "icews", "icews", "icews", "icews", "icews",
"icews", "icews", "icews", "icews", "icews", "icews", "icews",
@andybega
andybega / turkey-protests.R
Last active December 24, 2015 07:48
Map of protests in Turkey from January 2011 to September 2013, from GDELT event data.
library(ggmap)
library(source.gist)
# Get data frame with coordinates for points
source.gist("6765348")
head(turkey.prot)
# Get background map
tr.map <- get_map(location=c(lon=34, lat=37), zoom=5, maptype="terrain", filename="~/Desktop/ggmapTemp")
p <- ggmap(tr.map) +
@andybega
andybega / turkey-daily-protests.R
Last active May 9, 2016 14:13
GDELT and ICEWS counts of daily protest events in Turkey from 15 May to 15 June 2013.
# Libraries
library(RMySQL)
# Connect to server
conn <- dbConnect(MySQL(), user="ab428", password="",
dbname="event_data", host="")
country <- "Turkey"
start.date <- "2013-05-15"
end.date <- "2013-06-15"