决策树建模以及超漂亮的绘图
library(e1071)
modfit <- train(Species~.,method="rpart",data=training)
library(rattle)
fancyRpartPlot(modfit$finalModel)
有经纬度计算两点之间的距离
library(geosphere)
dist=distHaversine(matrix(c(pickup_longitude, pickup_latitude), ncol = 2),
matrix(c(dropoff_longitude, dropoff_latitude), ncol = 2))
把经纬度数据变成zip code/neighborhood name
You can now do this directly within R itself thanks to the rather awesomeggmappackage.
Like others mention, you'll be reverse geocoding using the google maps API (and therefore limited to 2,500 queries daily), but it's as simple as:
library("ggmap")
# generate a single example address
lonlat_sample <- as.numeric(geocode("the hollyood bowl"))
lonlat_sample # note the order is longitude, latitiude
res <- revgeocode(lonlat_sample, output="more")
# can then access zip and neighborhood where populated
res$postal_code
res$neighborhood
或
#Geocode:
getGeoCode <- function(gcStr) {
library("RJSONIO") #Load Library
gcStr <- gsub(' ','%20',gcStr) #Encode URL Parameters
#Open Connection
connectStr <- paste('http://maps.google.com/maps/api/geocode/json?sensor=false&address=',gcStr, sep="")
con <- url(connectStr)
data.json <- fromJSON(paste(readLines(con), collapse=""))
close(con)
#Flatten the received JSON
data.json <- unlist(data.json)
if(data.json["status"]=="OK") {
lat <- data.json["results.geometry.location.lat"]
lng <- data.json["results.geometry.location.lng"]
gcodes <- c(lat, lng)
names(gcodes) <- c("Lat", "Lng")
return (gcodes)
}
}
geoCodes <- getGeoCode("Palo Alto,California")
> geoCodes
Lat Lng
"37.4418834" "-122.1430195"
#Reverse Geocode:
reverseGeoCode <- function(latlng) {
latlngStr <- gsub(' ','%20', paste(latlng, collapse=","))#Collapse and Encode URL Parameters
library("RJSONIO") #Load Library
#Open Connection
connectStr <- paste('http://maps.google.com/maps/api/geocode/json?sensor=false&latlng=',latlngStr, sep="")
con <- url(connectStr)
data.json <- fromJSON(paste(readLines(con), collapse=""))
close(con)
#Flatten the received JSON
data.json <- unlist(data.json)
if(data.json["status"]=="OK")
address <- data.json["results.formatted_address"]
return (address)
}
address <- reverseGeoCode(c(37.4418834, -122.1430195))
> address
results.formatted_address
"668 Coleridge Ave, Palo Alto, CA 94301, USA"