簡體   English   中英

R.如何將循環(for)結果附加到數據框中?

[英]R. How to append loop (for) results into a Data Frame?

我正試圖通過訪問網絡服務和搜索郵政編碼來建立一個帶有巴西地址的數據框。 實際上,我能夠收到一個結果並將其存儲在數據幀中,但是當我嘗試搜索多個郵政編碼時(例如在向量中),我的數據幀只保留最后一個元素。 請問有人幫幫我嗎?

請參閱以下代碼:

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

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

請盡量提供最低限度的工作示例。 您的示例包含大量與您的實際問題無關的代碼行。 如果你試圖刪除這些不必要的代碼,你可能會在保存它之前發現正在擦除拉鏈信息的zips <- NULL行。 其次,您正在引用zips_df對象,但這不是在您的代碼中創建的。

回答你的問題:

  • 在開始循環之前,添加一行創建zips_df作為空數據zips_df對象:

     vectorzip <- c("71938360", "70673052", "71020510") j <- length(vectorzip) zips_df <- data.frame() 
  • 刪除擦除zips對象的行( zips <- NULL

  • 更改生長zips_df data.frame的行,將完整數據保存到data.frame對象,而不是臨時“zips”變量:

     zips <- rbind(zips_df, data.frame(t(zips),row.names=NULL)) 

我還建議刪除“視圖”行並使用print檢查data.frame:

print(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 (Ã\u0081guas 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM