按url加载csv文件在两个文件后出现故障,原因不明
我正在尝试从www2.census.gov加载一些按url加载csv文件在两个文件后出现故障,原因不明,r,csv,for-loop,url,try-catch,R,Csv,For Loop,Url,Try Catch,我正在尝试从www2.census.gov加载一些.csv文件,如下所示: # Create the numbers to replace in the download link: download_number <- c("01", "02", "04", "05", "06", "07", "08", "09", "
.csv
文件,如下所示:
# Create the numbers to replace in the download link:
download_number <- c("01", "02", "04", "05", "06", "07", "08", "09", "10")
dn <- seq(from = 11, to = 56, by = 1)
download_number <- append(download_number, dn)
# Load the url's
county_controls_2010_urllist <- list()
for (i in 1:length(download_number)) {
url_holder[[i]] <- glue::glue("https://www2.census.gov/programs-surveys/popest/datasets/2000-2010/intercensal/county/co-est00int-alldata-{download_number[[i]]}.csv")
tryCatch({
county_controls_2010_list[[i]] <- read.csv(url_holder) # https://www.programmingr.com/examples/read-csv-web/
if (i==1000) stop("stop")
}, error=function(e){cat("ERROR :",conditionMessage(e), "\n")})
}
ERROR : invalid 'description' argument
ERROR : invalid 'description' argument
...
ERROR : invalid 'description' argument
ERROR : invalid 'description' argument
我已经检查了第二个和第三个文件的链接位置,但它们看起来完全相同
https://www2.census.gov/programs-surveys/popest/datasets/2000-2010/intercensal/county/co-est00int-alldata-02.csv
https://www2.census.gov/programs-surveys/popest/datasets/2000-2010/intercensal/county/co-est00int-alldata-04.csv
有人能帮我找出哪里出了问题吗?试试以下方法:
file_url <- sprintf("https://www2.census.gov/programs-surveys/popest/datasets/2000-2010/intercensal/county/co-est00int-alldata-%s.csv", download_number)
purrr::map_df(file_url, function(x) tryCatch(read.csv(x),
error = function(e) message('skipping ', x),
warning = function(w) {})) -> result
result
文件\u url结果
结果
非常感谢!知道我的代码为什么不起作用吗?顺便说一下,您的答案中有一个拼写错误:purrr:
而不是purrr::
,但我无法修复它,因为它至少有六个字符。谢谢..更正。我不太清楚为什么你的代码不起作用。您是否尝试重新启动R并重试?