2017-02-07 68 views
0

您好我指的是這裏的問題 - Downloading multiple files using "download.file" function in Rfor循環和下載有多個URL

但我無法找到我一直在尋找的答案。我想從多個網址下載數據,我使用下面的代碼:

我試圖做類似的事情和我在下面河相對較新的是我的代碼:

temp <- tempfile(pattern = "my", fileext = ".txt") #my is a vector in YYMM form 

masterfile = as.data.frame(NULL) 

for(i in 1:length(my)) { 

    download.file(url = paste0("http://www2.census.gov/econ/bps/Metro/ma", "my[i]", "c.txt"), destfile = paste0("/Users/shashankrai/GitHub/data-science/homeworks/homework1/","my[i]","c.txt"), mode = wb) 

temp <- read.table(paste0("/Users/shashankrai/GitHub/data-science/homeworks/homework1/","my[i]","c.txt"), sep = ",", skip = 3)[, c(1,3,5)] 

masterfile <- rbind(masterfile, temp) 

} 

它拋出了以下錯誤:

curl: (3) [globbing] bad range in column 44

Error in file(file, "rt") : cannot open the connection

In addition: Warning messages:

1: In download.file(url = paste0(" http://www2.census.gov/econ/bps/Metro/ma ", : download had nonzero exit status

2: In file(file, "rt") : cannot open file '/Users/shashankrai/GitHub/data-science/homeworks/homework1/my[i]c.txt': No such file or directory

你能告訴我什麼,我做錯了什麼?

我也試過這樣:

temp <- tempfile(pattern = "my", fileext = ".txt") 

masterfile = as.data.frame(NULL) 

for(i in 1:length(my)) { 

    download.file(url = paste0("url", "my[i]", "c.txt"), destfile = my[i], mode = wb) 

    temp <- read.table(my[i], sep = ",", skip = 3)[, c(1,3,5)] 

    masterfile <- rbind(masterfile, temp) 

} 
+1

''my [i]'''應該是我的[i]' – MichaelChirico

+0

謝謝邁克爾。是的,我注意到了。 :) –

回答

0

沒關係。弄清楚了。這是一個終於有效的代碼:

masterfile = as.data.frame(NULL) 

for(i in 1:length(my)) { 

    temp <- tempfile(pattern = "my", fileext = ".txt") 

    download.file(url = paste0("http://www2.census.gov/econ/bps/Metro/ma",  my[i], "c.txt"), destfile = temp[i], mode = wb) 

    masterfile <- rbind(masterfile, read.table(file = temp[i], sep = ",", skip = 3)[, c(1,3,5)]) 

}