Database Reference
In-Depth Information
Making Events of Our Foreclosures
Our geoTable is similar in structure to an EventData object but we need to use the
as.EventData function to complete the conversion.
> addressEvents<-as.EventData(geoTable,projection=NA)
Error in as.EventData(geoTable, projection = NA) :
Cannot coerce into EventData.
One or more columns contains factors where they are not allowed.
Columns that cannot contain factors: EID, X, Y.
Oh snap! The dreaded “factor feature” in R strikes again. When a data frame column contains
factors , its elements are represented using indices that refer to levels, distinct values within
that column. The as.EventData method is expecting columns of type numeric, not factor.
Never transform from factors to numeric like this:
as.numeric(myTable$factorColumn) #don't do this!
This is an extremely common mistake—this will merely return numeric indices used to refer
to the levels—but you want the levels themselves:
> geoTable$X<-as.numeric(levels(geoTable$X))[geoTable$X] #do this
> geoTable$Y<-as.numeric(levels(geoTable$Y))[geoTable$Y]
With numeric lat/longs in hand, we can quantitatively explore this geographic data. What is
the northernmost (highest latitude) of our foreclosures?
> geoTable[geoTable$Y==max(geoTable$Y),]
address Y X EID
1151 100 County Line Rd. 40.13754 -75.0145 1151
The EventData casting should work now that our columns are numeric.
> addressEvents<-as.EventData(geoTable,projection=NA)
> addPoints(addressEvents,col="red",cex=.5)
The EventData is rendered as tiny circles using addPoints (see Figure 1-3 ) .
Search WWH ::




Custom Search