嗨,我指的是这个问题-- Downloading multiple files using "download.file" function in R
但我找不到我想要的答案。我想从多个urls下载数据,并使用以下代码:
我正在尝试做一些类似的事情,在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“),:download具有非零退出状态) 2:在文件(文件,"rt")中:无法打开文件'/Users/shashankrai/GitHub/data-science/homeworks/homework1/myic.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)
}发布于 2017-02-07 16:06:14
不要紧。弄明白了。这是一个最终起作用的代码:
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)])
}https://stackoverflow.com/questions/42094106
复制相似问题