您好我在这里指的是问题 - Downloading multiple files using "download.file" function in R
但我找不到我想要的答案。我想从多个网址下载数据,我使用以下代码:
我正在尝试做类似的事情而且在R中相对较新。以下是我的代码:
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)
}
它引发了以下错误:
卷曲:(3)第44栏中[全局]坏范围
文件错误(文件,“rt”):无法打开连接
另外:警告信息:
1:在download.file中(url = paste0(“http://www2.census.gov/econ/bps/Metro/ma”,:下载了 非零退出状态
2:在文件(文件,“rt”)中:无法打开文件 '/Users/shashankrai/GitHub/data-science/homeworks/homework1/my[i]c.txt': 没有这样的文件或目录
你能告诉我我做错了吗?
我也试过这个:
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)
}
答案 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)])
}