I have a dataframe with Address, City, State, Zip entities. From there, I'm trying to use the Yahoo APIs to Geocode each address.
I'm basing this off the code in O'Reilly's Data Mashups using R Tutorial. The original example takes a vector of street addresses and uses a hard-coded city. I'm trying to make a dynamic example that supports multiple cities.
The abbreviated version of the code is:
geocodeAddresses<-function(myStreets)
}
appid<-'<put your appid here>'
baseURL<-"http://local.yahooapis.com/MapsService/V1/geocode?appid="
myGeoTable<-data.frame(address=character(),lat=numeric(),long=numeric(),EID=numeric())
for (myStreet in myStreets){
requestUrl<-paste(baseURL, appid, "&street=", URLencode(myStreet$address),"&city=",URLencode(myStreet$city),"&state=",URLencode(myStreet$state),sep="")
xmlResult<-xmlTreeParse(requestUrl,isURL=TRUE,addAttributeNamespaces=TRUE)
geoResult<-xmlResult$doc$children$ResultSet$children$Result
lat<-xmlValue(geoResult[['Latitude']])
long<-xmlValue(geoResult[['Longitude']])
myGeoTable<-rbind(myGeoTable,data.frame(address=myStreet,Y=lat,X=long,EID=NA))
}
}
When I try and reference myStreet$City and myStreet$Address, I receive error
$ operator is invalid for atomic vectors
Other than looping through data frame myStreets, I don't know how I can make the call to the Yahoo API only once for each row and store both the long/lat for each member.