Skip to content
Prev 379605 / 398503 Next

converting zipcodes to latitude/longitude

Hi Nicola,
Getting the blank rows will be a bit more difficult and I don't see
why they should be in the final data frame, so:

townzip<-read.table(text="waltham, Massachusetts 02451
Columbia, SC 29209

Wheat Ridge , Colorado 80033
Charlottesville, Virginia 22902
Fairbanks, AK 99709
Montpelier, VT 05602
Dobbs Ferry, New York 10522

Henderson , Kentucky 42420",
sep="\t",stringsAsFactors=FALSE)
zip_split<-function(x) {
 commasplit<-unlist(strsplit(x,","))
 state<-trimws(gsub("[[:digit:]]","",commasplit[2]))
 zip<-trimws(gsub("[[:alpha:]]","",commasplit[2]))
 return(c(commasplit[1],state,zip))
}
townzipsplit<-as.data.frame(t(sapply(townzip$V1,zip_split)))
rownames(townzipsplit)<-NULL
names(townzipsplit)<-c("town","state","zip")
townzipsplit$latlon<-NA
# I don't know the name of the zipcode column in the "zipcode" data frame
newzipdf<-merge(townzipsplit,zipcodedf,by.x="zip",by.y="zip")

Jim

On Tue, May 14, 2019 at 5:57 AM Nicola Ruggiero
<nicola.ruggiero.unt at gmail.com> wrote: