如何使用 download.file() 遍历列中的 url
How to loop through urls in a column using download.file()
我有这个 df
我需要从中下载所有文件 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
当然我不想为每个 url 输入 40 次 download.file(url='https://this.is.url_number.tar.gz', destfile='files.tar.gz', method='curl')
。如何使用 download.file()
遍历 column_1 中的所有 url?
这是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')
}
您可以使用 Map
-
Map(download.file, df$column_1, sprintf('file%d.tar.gz', seq(nrow(df))))
其中 sprintf
用于创建文件名以保存文件。
我有这个 df
我需要从中下载所有文件 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
当然我不想为每个 url 输入 40 次 download.file(url='https://this.is.url_number.tar.gz', destfile='files.tar.gz', method='curl')
。如何使用 download.file()
遍历 column_1 中的所有 url?
这是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')
}
您可以使用 Map
-
Map(download.file, df$column_1, sprintf('file%d.tar.gz', seq(nrow(df))))
其中 sprintf
用于创建文件名以保存文件。