R. 如何将循环 (for) 结果附加到数据帧中? [英] R. How to append loop (for) results into a Data Frame?

查看:20
本文介绍了R. 如何将循环 (for) 结果附加到数据帧中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过访问网络服务并搜索邮政编码来构建一个包含巴西地址的数据框.实际上,我能够接收一个结果并将其存储在数据框中,但是当我尝试搜索多个邮政编码(例如在向量中)时,我的数据框仅保留最后一个元素.有人可以帮我吗?

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?

查看下面的代码:

###############
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)}

推荐答案

您想要:

a) 定义 zips_df
b) 在循环外定义 zips_df.
c) 不在循环内将 zips_df 设置为 null :)

a) define zips_df
b) define zips_df outside of the loop.
c) not set the zips_df to null inside the loop :)

###############
library(httr)
library(RCurl)
library(XML)
library(dplyr)
###############

# ZIPs I want to search for:
vectorzip <- c("71938360", "70673052", "71020510")
j <- length(vectorzip)
zips_df <- data.frame()

i<-1
# 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_df <- rbind(zips_df, data.frame(t(zips),row.names=NULL))
}

  View(zips_df)

你明白了:

> zips_df
  resultado.text     resultado_txt.text uf.text cidade.text         bairro.text tipo_logradouro.text  logradouro.text
1              1 sucesso - cep completo      DF  Taguatinga Sul (Ãguas Claras)                  Rua               09
2              1 sucesso - cep completo      DF    Cruzeiro      Setor Sudoeste               Quadra      300 Bloco O
3              1 sucesso - cep completo      DF      Guará            Guará I               Quadra QI 11 Conjunto U

这篇关于R. 如何将循环 (for) 结果附加到数据帧中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆