Skip to content

Instantly share code, notes, and snippets.

@gourab5139014
Last active October 6, 2015 10:42
Show Gist options
  • Save gourab5139014/d4f7f9d27317dec964b4 to your computer and use it in GitHub Desktop.
Save gourab5139014/d4f7f9d27317dec964b4 to your computer and use it in GitHub Desktop.
The extractLongLatData() uses the ggmap API in R to extract longitude and latitude data for a set of pincodes supplied in a csv file
extractLongLatData <- function(file)
{
library(ggmaps) #In case you dont have the package, use this command - install.packages(ggmaps, dependencies = TRUE)
data <- read.csv(file)
v <- as.list(data["pincode"]) #assuming the attribute containing the pincodes in the supplied CSV file is named "pincode"
pc <- sapply(v,as.character)
pc <- sapply(pc,function(x) { paste(x,"India", sep = " ")}) #change the argument in paste() from "India" to whichever country the pincodes belong to
longlatpc <- geocode(pc, source = "google")
library(xlsx) #In case you dont have the package, use this command - install.packages(xlsx, dependencies = TRUE)
write.xlsx2(longlatpc,'longlatpincode.xlsx')
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment