【发布时间】:2016-07-24 08:26:18
【问题描述】:
我正在尝试通过访问网络服务并搜索邮政编码来构建一个包含巴西地址的数据框。 实际上,我能够接收一个结果并将其存储在数据框中,但是当我尝试搜索多个邮政编码(例如在向量中)时,我的数据框只保留最后一个元素。 有人可以帮帮我吗?
请看下面的代码:
###############
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://cep.republicavirtual.com.br/web_cep.php?cep=",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)}
【问题讨论】:
-
什么是 zips
-
使用 rbind 增长对象通常不是一个好主意。更好的方法是定义一个特定大小的空 data.frame(从而分配必要的内存)并随后填充行。
标签: r loops web service append