我试图通过访问Web服务和搜索邮政编码来建立一个巴西地址的数据框。实际上,我可以接收一个单独的结果并将其存储在一个数据框中,但是当我尝试搜索多个邮政编码(例如向量中)时,我的数据框只保留最后一个元素。 有人可以帮我吗?R.如何将循环(for)结果追加到数据框中?
请参见下面的代码:
###############
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 < - NULL行为zips_df定义的位置? –
用rbind生长一个对象通常不是一个好主意。更好的方法是定义一个特定大小的空数据框(从而分配必要的内存),然后填充行。 – RHertel