aurelius_37809
aurelius_37809

Reputation: 195

How to loop through urls in a column using download.file()

I have this df from which I need to download all file urls:

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

Of course I do not want to type download.file(url='https://this.is.url_number.tar.gz', destfile='files.tar.gz', method='curl') 40 times for each url. How can I loop over all url's in column_1 using download.file()?

Upvotes: 2

Views: 1122

Answers (2)

Ronak Shah
Ronak Shah

Reputation: 389275

You can use Map -

Map(download.file, df$column_1, sprintf('file%d.tar.gz', seq(nrow(df))))

where sprintf is used to create filenames to save the file.

Upvotes: 0

akrun
akrun

Reputation: 887891

Here is one way in a forloop

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')
}

Upvotes: 4

Related Questions