Skip to content

Instantly share code, notes, and snippets.

@justgrimes
justgrimes / gist:2348459
Created April 10, 2012 05:16
publiclibraries.com scraper
# publiclibraries.com scraper
import BeautifulSoup
import urllib2
l = [("http://www.publiclibraries.com/alabama.htm","AL"),
("http://www.publiclibraries.com/alaska.htm","AK"),
("http://www.publiclibraries.com/arizona.htm","AZ"),
("http://www.publiclibraries.com/arkansas.htm","AR"),
("http://www.publiclibraries.com/california.htm","CA"),
@justgrimes
justgrimes / latlongmatch.py
Created April 16, 2012 17:52
Match two sets of latitude/longitude points by distance
#script that attempts to match compare lat,long points two csv files, match by distance
#justin grimes (@justgrimes) 04/16/12
import math
import csv
import sys
import re
# takes two lat/long points and returns distance, modified code from -> http://www.johndcook.com/python_longitude_latitude.html
def distance_on_unit_sphere(lat1, long1, lat2, long2):
@justgrimes
justgrimes / latlong_distance_sql_snippet
Created April 17, 2012 17:01
sql snippet for calculating distance for latitude, longtitude
SELECT latitude,longitude, (3963.191 * ACOS(
(SIN(PI()* 40.7383040 /180)*SIN(PI()*latitude/180)) +
(COS(PI()* 40.7383040 /180)*cos(PI()*latitude/180)*COS(PI() * longitude/180-PI()* -73.99319 /180))
)) AS distance
FROM TABLE
WHERE 1=1
AND 3963.191 * ACOS( (SIN(PI()* 40.7383040 /180)*SIN(PI() * latitude/180)) +
(COS(PI()* 40.7383040 /180)*cos(PI()*latitude/180)*COS(PI() * longitude/180-PI()* -73.99319 /180))
) <= 1.5
ORDER BY 3963.191 * ACOS(
@justgrimes
justgrimes / gist:3308085
Created August 9, 2012 21:07
text mining in r snippet
require(tm)
a <- Corpus(DirSource("C:/Users/jgrimes/Desktop/text/"), readerControl = list(language="lat"))
#summary(a)
a <- tm_map(a, function(x) iconv(enc2utf8(x), sub = "byte"))
a <- tm_map(a, removePunctuation)
a <- tm_map(a, removeNumbers)
a <- tm_map(a, stripWhitespace)
a <- tm_map(a, tolower)
a <- tm_map(a, removeWords, stopwords("english"))
a <- tm_map(a, stemDocument, language = "english")
@justgrimes
justgrimes / imls_pls_small_multiple.r
Created January 13, 2014 19:34
Downloads FY 2011 Public Library Survey data files from Institute of Museum and Library Services website and constructs a small multiple map of NCES locale codes.
# Load packages
library(maps) # To draw map
library(maptools)
# download data files from IMLS website
temp <- tempfile()
download.file("http://www.imls.gov/assets/1/AssetManager/pupld11b_csv.zip",temp)
pupld11b <- read.csv(unz(temp, "pupld11b.csv")) #file with library systems (admin buildings)
puout11b <- read.csv(unz(temp, "puout11b.csv")) #file with library outlets (all buildings)
unlink(temp)
@justgrimes
justgrimes / gist:07079a03c66f2ad054d3
Created December 10, 2014 16:24
Introduction to R - SDC Annual Meeting 2014
#SDC Annual Meeting 2014 Introduction to R, R Studio, and Statistical Computing
# Load downloaded data from PLS 2012 survey, CSV format (comma seperated value)
pupld12a_csv <- read.csv("pupld12a_csv/pupld12a.csv", header = TRUE, sep=",", quote = "\"")
puout12a_csv <- read.csv("pupld12a_csv/puout12a.csv", header = TRUE, sep=",", quote = "\"")
pusum12a_csv <- read.csv("pupld12a_csv/pusum12a.csv", header = TRUE, sep=",", quote = "\"")
# Load downloaded data from PLS 2012 survey, Excel format (xls)
#install.packages("gdata")
library(gdata) #library to load xls data; if loading xlsx files use XLConnect library
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@justgrimes
justgrimes / SonifyPLS.R
Last active August 29, 2015 14:20
sonify public library survey data using R and audiolyzR
# Script to load public public library data from the most recent
# FY 2012 Public Library Survey (PLS) from Institute of Museum and Library Services
# Data documentation -> http://www.imls.gov/assets/1/AssetManager/fy2012_pls_data_file_documentation.pdf
# More info about data & survey -> http://www.imls.gov/research/public_libraries_in_the_united_states_survey.aspx
install.packages("audiolyzR")
library("audiolyzR")
# Load PLS 2012 directly from the web
@justgrimes
justgrimes / pls12nbmcai14.r
Last active August 29, 2015 14:21
Public library broadband data - Loads, cleans, and merges Public Library Survey data with National Broadband Map Community Anchor Institution data
# Script to load public public library data from FY 2012 Public Library Survey (PLS) from Institute of Museum and Library Services
# and merge it with most recent broadband data from National Broadband Map Community Anchor Institution data
#
# FOR REFERENCE
# CAIID - Unique federal ID's associated with the CAICAT
# CAICAT -these are the unique federal ID's associated with the CAICAT
# MAXADDOWN - Maximum Advertised Download Speed subscribed to at this CAI
# MAXADUP - Maximum Advertised Upload Speed subscribed to at this CAI
# BBSERVICE - Does institution subscribe to broadband service at location (ex. Y=Yes; N=No; U=Unknown),
@justgrimes
justgrimes / plsmap.r
Created May 22, 2015 17:57
pls mapping
#PLS Dot Density Map
library(maps)
library(mapproj)
library(maptools)
library(geosphere)
library(splancs)
library(foreign)
# Map just the points