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 / 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 / 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{
@abelsonlive
abelsonlive / gist:3687248
Created September 9, 2012 20:55
percents
print(paste("step", i))
print(paste(i/n*100, "%"))
find: ([a-z]) (["])([a-z])
replace: $1 \\$2$3
find: .""
replace: ./""
find: ([a-z])(["]) ([a-z])
replace: $1\\$2 $3
# fill area under a line:
plot(x,y, type="n)
lines(x,y)
polygon( c(min(x), x, max(x)), c( min(y), y, min(y)), density=100 )
@abelsonlive
abelsonlive / gist:3751902
Created September 19, 2012 20:04
google geocoding API, R
geocode.addr <- function(uid_query) {
require("rjson")
require("RCurl")
require("plyr")
uid <- uid_query$uid
query <- uid_query$query
geo.url <- "http://maps.googleapis.com/maps/api/geocode/json?address="
geo.text <- try(getURL(paste(geo.url, URLencode(query), "&sensor=false", sep="")))
@abelsonlive
abelsonlive / gist:3834327
Created October 4, 2012 15:19
PDF to Text, R
# helper function: get number of words in a string, separated by tab, space, return, or point.
nwords <- function(x){
res <- strsplit(as.character(x), "[ \t\n,\\.]+")
res <- lapply(res, length)
unlist(res)
}
# sanitize file name for terminal usage (i.e., escape spaces)
sanitize <- function(str) {
gsub('([#$%&~_\\^\\\\{}\\s\\(\\)])', '\\\\\\1', str, perl = TRUE)