Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View abelsonlive's full-sized avatar
🕳️
[ o o ]

Brian Abelson abelsonlive

🕳️
[ o o ]
View GitHub Profile
@abelsonlive
abelsonlive / scrapeBikeShare.R
Created May 13, 2012 23:41
Scrape NYC BikeShare Data
require("rjson")
#generate urls to scrape
#I determined the lowest and highest unique ids
#by looking at the page that had just the coordinates
root.url <- "http://a841-tfpweb.nyc.gov/bikeshare/get_point_info?point="
id<- seq(11992,12404, by=1)
urls <- paste(root.url, id, sep="")
@abelsonlive
abelsonlive / localMorans.R
Created May 17, 2012 01:00
Add p-values from Local Moran's test to a shapefile with counts.
library("foreign")
library("rgdal")
library("sp")
library("spdep")
library("maptools")
library("SpatialEpi")
library("plyr")
data <- read.dbf("counts.dbf")
shapes <- readShapePoly("counts.shp")
@abelsonlive
abelsonlive / R: New calendarHeat
Created May 17, 2012 06:34
R: modify calendarHeat map to allow custom breaks and RColorBrewer functionality
require("lattice")
require("makeR")
####################################################################
####################################################################
####################################################################
####################################################################
levelplot.forumula <- function (x, data = NULL, allow.multiple = is.null(groups) ||
outer, outer = TRUE, aspect = "fill", panel = if (useRaster) lattice.getOption("panel.levelplot.raster") else lattice.getOption("panel.levelplot"),
@abelsonlive
abelsonlive / R: unix time to date object
Created May 17, 2012 06:50
unix time to date object conversion
data$date <- as.POSIXlt(data$unix_time, origin="1970-01-01", tz="America/New_York")
data$date <- as.Date(data$date, format="%Y-%m-%d")
@abelsonlive
abelsonlive / merge.csv.R
Created May 27, 2012 01:09
merge all csv/xls/etc in a directory by a common field, R
#_______________________________________________________________________________#
#| |#
#| !! keep in mind that each data frame must have only one common field name!! |#
#|_____________________________________________________________________________|#
#create the function for csvs
merge.csv = function(mypath){
filenames=list.files(path=mypath, full.names=TRUE)
datalist = lapply(filenames, function(x){read.csv(file=x,header=T, stringsAsFactors=F)})
@abelsonlive
abelsonlive / genVarNames.R
Created May 27, 2012 02:33
Automatically generate variable names from Pattern, R
#// Say you were building a dataset and wanted to automatically
#// generate variable names by some pattern.
#// For instance, you might want to do this with population counts
#// within 100 census tracts by race
#// IE:
#
# tracts <- paste("c", rep(1:100), sep="")
# race - c("black", "white", "hispanic")
#
#// In this case you would want to generate 300 unique variable names
@abelsonlive
abelsonlive / getInsights.R
Created May 27, 2012 20:09
getInsights - an R API for Google Insights
#see below for query specifications
getInsights <- function(
username = "Insights4R@gmail.com",
password = "googleinsights4r",
text_query = "'hello world'",
date_query = "1/2004 108m",
geo_query = "US",
search_type= "all",
filter_category="none"){
@abelsonlive
abelsonlive / gist:3311043
Created August 10, 2012 04:19 — forked from lucasfais/gist:1207002
Sublime Text 2 - Useful Shortcuts

Sublime Text 2 – Useful Shortcuts (Mac OS X)

General

⌘T go to file
⌘⌃P go to project
⌘R go to methods
⌃G go to line
⌘KB toggle side bar
⌘⇧P command prompt
@abelsonlive
abelsonlive / cartodb2R
Created September 8, 2012 15:45
carto2R.R
path_to_file = "~/Desktop/Vizzuality-cartodb-r-05eb537/CartoDB_1.4.tar.gz"
install.packages(path_to_file, repos=NULL, type="source")
require("CartoDB")
account_name = "parks-datadive"
api_key = "[ON THE BOARD IN 517-F]"
cartodb(account_name, api_key)
cartodb.test()
@abelsonlive
abelsonlive / google_geocode.R
Created September 9, 2012 20:19 — forked from drewconway/google_geocode.R
Grab lat / lon from an address using Google Maps
# Get a lat/lon value for each address
geocode.addr <- function(city, country) {
geo.url <- "http://maps.googleapis.com/maps/api/geocode/json?address="
geo.text <- getURL(paste(geo.url, URLencode(paste(city, country, collapse="+")), "&sensor=false", sep=""))
geo.json <- fromJSON(geo.text)
if(geo.json$status == "OK"){
return(geo.json$results[[1]]$geometry$location)
}
else{