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 / 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
@abelsonlive
abelsonlive / gist:3703504
Created September 12, 2012 01:24
read dbf into R (for Cezary)
install.packages("foreign")
library("foreign")
data = read.dbf("path/to/file.dbf")
write.csv(data, "path/to/file.csv", row.names=F)
# 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 / srapeshell.R
Created September 23, 2012 09:19
# best practices for web scraping in R // ldply
# best practices for web scraping in R #
# function should be used with ldply
# eg:
ldply(urls, scrape)
# add a try to ignore broken links/ unresponsive pages
# eg:
@abelsonlive
abelsonlive / r_install.sh
Created October 4, 2012 15:04 — forked from seeRead/r_install.sh
r init file
#/bin/bash
# run chmod +x THIS_GIST.sh
# configure for debian or ubuntu! debian has dependencies that ubuntu's libs will conflict with
# sources:
# http://cran.r-project.org/bin/linux/debian/
# http://cran.r-project.org/bin/linux/ubuntu/README
# http://stackoverflow.com/questions/10255082/installing-r-from-cran-ubuntu-repository-no-public-key-error
clear
@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)
@abelsonlive
abelsonlive / gist:3899832
Created October 16, 2012 15:05
coefplot.R
require("RColorBrewer")
# not run:
# model = lm(y~x1+x2+x3+x4...)
# Construct quantile table for varialbes of interest
# t value: n-p=df.residual=634, alpha=5%,50%. t(1-alpha/2, n-p)
t.05 <- qt(1-0.025, 634, lower.tail = TRUE, log.p=FALSE)
t.5 <- qt(1-0.25, 634, lower.tail = TRUE, log.p=FALSE)
coef.quantil <- data.frame(matrix(0, nrow=length(model$coefficients)-1, ncol=8))
@abelsonlive
abelsonlive / raster_scatter.R
Created October 16, 2012 22:10 — forked from dsparks/raster_scatter.R
Drawing a scatter plot of raster images
# Drawing a scatter plot of raster images
doInstall <- TRUE # Change to FALSE if you don't want packages installed.
toInstall <- c("png", "devtools", "MASS", "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")