A few R scripts, for my own reference.
Last active
August 29, 2015 14:00
-
-
Save amandabee/8514c262734b3241da73 to your computer and use it in GitHub Desktop.
A handful of miscellaneous R scripts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ## Load the file into a dataframe | |
| calls.to.911 <- read.csv('/tmp/Calls_for_Service_2013.csv') | |
| calls.to.911 <- read.csv('Calls_for_Service_2013.csv') | |
| ## Try `table` to summarize the data | |
| table(calls.to.911$DispositionText) | |
| ## Make a few new frames out of those tables | |
| disposition.summary <- as.data.frame(table(calls.to.911$DispositionText)) | |
| crime.summary <- as.data.frame(table(calls.to.911$TypeText)) | |
| crime.by.disposition <- as.data.frame(table(calls.to.911$TypeText,calls.to.911$DispositionText)) | |
| ## And write the frames to a CSV | |
| write.csv(crime.summary, file='count_by_crime.csv') | |
| write.csv(disposition.summary, file='count_by_disposition.csv') | |
| write.csv(crime.by.disposition, file='summary.csv') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| library(ggmap) | |
| library(ggplot2) | |
| ## If you don't have ggmap, install it with `install.packages("ggmap")` | |
| setwd("~/mydir") | |
| ## Read the CSV into a data frame | |
| alums <- read.csv('where_weve_gone.csv') | |
| ## geocode() will return 2 vectors, latitude and longitude. Attach those with cbind: | |
| alums <- cbind(alums, geocode(as.character(alums$City))) | |
| ## write that all back to a CSV: | |
| write.csv(alums, file="alums_geo.csv") | |
| ## To work with the data later, you can just read from the geocoded CSV | |
| alums <- read.csv('alums_geo.csv') | |
| ## Draw the world map | |
| library(maps) | |
| library(mapdata) | |
| map("world", col="#f2f2f2", fill=TRUE, bg="#FFFFFF", lwd=0.05) | |
| points(alums$lon, alums$lat) | |
| # Should help: | |
| # http://stackoverflow.com/questions/19262805/r-geom-point-and-ggmap | |
| # http://www.mpi.nl/departments/independent-research-groups/evolutionary-process | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| library(ggmap) | |
| ## Read your csv into a data frame | |
| setwd('Data/PreK') | |
| prek <- read.csv('prek.csv') | |
| ## Create a new column that has a complete address in the form | |
| ## 219 W 40th Street, New York, NY 10018 | |
| ## out of the relevant columns in my CSV (Address, Boro, ZIP) | |
| prek$fulladdy <- paste(prek$Address, prek$Boro, ', NY', prek$ZIP) | |
| ## Geocode that "fulladdy" column and attach it to the data frame. This adds two columns, lon and lat. | |
| prek <- cbind(prek, geocode(prek$fulladdy)) | |
| ## look into the N/As | |
| subset(prek, is.na(prek$lat)) | |
| ## Write it out to a new text file for mapping | |
| write.csv(prek, file="prek.txt") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment