I´m trying to build a dataframe with some brazilian address by accessing a web service and searching for a zip code. Actually, I´m able to receive one single result and store it in a dataframe, but when I try to search for multiple zip codes (e.g in a vector), my dataframe is only keep the last element. Could anybody help me please?
See the code below:
###############
library(httr)
library(RCurl)
library(XML)
library(dplyr)
###############
# ZIPs I want to search for:
vectorzip <- c("71938360", "70673052", "71020510")
j <- length(vectorzip)
# loop:
for(i in 1:j) {
# Save the URL of the xml file in a variable:
xml.url <- getURL(paste("http://ift.tt/1Ybvqj7",vectorzip[i], sep = ""), encoding = "ISO-8859-1")
xml.url
# Use the xmlTreeParse-function to parse xml file directly from the web:
xmlfile <- xmlTreeParse(xml.url)
xmlfile
# the xml file is now saved as an object you can easily work with in R:
class(xmlfile)
# Use the xmlRoot-function to access the top node:
xmltop = xmlRoot(xmlfile)
# have a look at the XML-code of the first subnodes:
print(xmltop)
# To extract the XML-values from the document, use xmlSApply:
zips <- xmlSApply(xmlfile, function(x) xmlSApply(x, xmlValue))
zips
# Finally, get the data in a data-frame and have a look at the first rows and columns:
zips <- NULL
zips <- rbind(zips_df, data.frame(t(zips),row.names=NULL))
View(zips_df)}
Aucun commentaire:
Enregistrer un commentaire