首页 > 解决方案 > 如何使用 download.file() 遍历列中的 url

问题描述

我有这个df我需要下载所有文件的网址:

library(RCurl)

view(df)

    Date       column_1                                                             
    <chr>      <chr>                                                                  
 1  5/1/21     https://this.is.url_one.tar.gz
 2  5/2/12     https://this.is.url_two.tar.gz
 3  7/3/19     https://this.is.url_three.tar.gz
 4  8/3/13     https://this.is.url_four.tar.gz
 5  10/1/17    https://this.is.url_five.tar.gz
 6  12/12/10   https://this.is.url_six.tar.gz
 7  9/9/16     https://this.is.url_seven.tar.gz
 8  4/27/20    https://this.is.url_eight.tar.gz
 9  7/20/15    https://this.is.url_nine.tar.gz
10  8/30/19    https://this.is.url_ten.tar.gz
# … with 30 more rows

当然,我不想download.file(url='https://this.is.url_number.tar.gz', destfile='files.tar.gz', method='curl')为每个 url 输入 40 次。如何循环使用 column_1 中的所有 url download.file()

标签: rloopsfiledownload

解决方案


for这是循环中的一种方式

for(i in seq_len(nrow(df))) {
    download.file(url = df$column_1[i], 
    destfile = paste0('files', df$column_ID_number[i], 
         '.tar.gz'), method = 'curl')
}

推荐阅读