r - Binning longitude/latitude labeled data by census block ID -


i have 2 data sets, 1 crime in chicago, labeled longitude , latitude coords , shapefile of census blocks in chicago. possible in r aggregate crimes within census blocks, given these 2 files? purpose able map out crimes census block.

location download of chicago census tract data: https://data.cityofchicago.org/facilities-geographic-boundaries/boundaries-census-blocks-2000/uktd-fzhd

location download of crime data: https://data.cityofchicago.org/public-safety/crimes-2001-to-present/ijzp-q8t2

some code have pruned down project. when through there spatial object census tract information , dataframe containing crime data, including lon/lat coords:

library(rgdal) library(dplyr)  #helper function reduce crime data single year , limit variables of interest yearreduce <- function(rawdata=null,year=null) {         datout <- data.frame(year = numeric(0), community = numeric(0), type = numeric(0), arrest = numeric(0),                              latitude = numeric(0), longitude = numeric(0))         dat <- rawdata[rawdata$year==year,]         datout <- data.frame(year = dat$year, community = as.numeric(dat$community.area), type = dat$primary.type, arrest = dat$arrest,                              latitude = dat$latitude, longitude = dat$longitude)         datout }  #load crime data crimedata <- '~/documents/data/crimes_-_2001_to_present.csv' mydata_crime <- read.csv(crimedata,na.strings = c("", " ", "na"), stringsasfactors=f) mydata_crime$primary.type <- tolower(mydata_crime$primary.type)  #set cwd location of census tract shape file setwd('~/documents/data/boundaries_-_census_blocks_-_2010/') #create spatial vector object , transform projection tract = readogr(".","censusblocktiger2010") %>% sptransform(crs("+proj=longlat +datum=wgs84"))  ### process crime data narrow single year### crime2010 <- yearreduce(mydata_crime,'2010')  # further select specific crime(s). limited testing purposes violent_crimes <- subset(crime2010,                          type == "homicide")  violent_crimes <- violent_crimes[complete.cases(violent_crimes),] #clean data little bit 

thank may able provide.

patrick

#load libraries library(rgdal) library(sp) library(raster) 

first, few improvements code above

#set wd setwd('~/dropbox/rstats/r_blog_home/stack_o/')  #load crime data my_crime <- read.csv(file='spat_aggreg/crimes_2001_to_present.csv',stringsasfactors=f)` my_crime$primary.type <- tolower(my_crime$primary.type)  #select variables of interest , subset year , type of crime #note, yearreduce() not necessary @ all: check r documentation before creating own functions my_crime <- data.frame(year=my_crime$year, community=my_crime$community.area,           type=my_crime$primary.type, arrest=my_crime$arrest,           latitude=my_crime$latitude, longitude=my_crime$longitude) vc <- subset(my_crime, year==2010, type=="homicide")  #keep complete cases vc <- vc[complete.cases(vc), ]  #load census tract data #note: function `shapefile` neater `readogr()` #note: usage of `@` access attribute data tables associated spatial objects in r tract <- shapefile('spat_aggreg/census_blocks_2000/census_blocks.shp') tract <- sptransform(x=tract, crsobj=crs("+proj=longlat +datum=wgs84")) names(tract@data) <- tolower(names(tract@data)) 

and now, answer question...

#convert crime data spatial points object vc <- spatialpointsdataframe(coords=vc[, c("longitude", "latitude")],           data=vc[, c("year", "community", "type", "arrest")],           proj4string=crs("+proj=longlat +datum=wgs84"))  #each row entry represents 1 homicide, so, add count column vc@data$count <- 1  #spatial overlay identify census polygon in each crime point falls #the result `vc_tract` dataframe tract data each point vc_tract <- over(x=vc, y=tract)  #add tract data crimepoints vc@data <- data.frame(vc@data, vc_tract)  #aggregate homicides tract (assuming column "census_tra" unique id census tracts) hom_tract <- aggregate(formula=count~census_tra, data=vc@data, fun=length)  #add number of homicides tracts object m <- match(x=tract@data$census_tra, table=hom_tract$census_tra) tract@data$homs_2010 <- hom_tract$count[m] 

now, census tracts (in spatialpolygondataframe object named tract) contain column named homs_2010 has number of homicides each tract. there, should breeze map it.


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -