简体   繁体   English

如何使用 dataframe 中另一列的数据命名 R 中的文件下载?

[英]How do I name file downloads in R using data from another column in dataframe?

I have a large dataset of unique file IDs and links to download the files.我有一个包含唯一文件 ID 和下载文件链接的大型数据集。 It looks like this:它看起来像这样:

file_id <- c("id:fghjs12:ws8c7/syx", "id:f7gnsfu:7a6#*s", "id:dug:shxgcvu:6sh")
link <- c("https://www.dynare.org/wp-repo/dynarewp028.pdf", "https://www.dynare.org/wp-repo/dynarewp029.pdf", "https://www.dynare.org/wp-repo/dynarewp020.pdf")
df <- data.frame(file_id, link, stringsAsFactors = FALSE)

I want to download each file using the name of the handle.我想使用句柄的名称下载每个文件。 Some of the links are broken.一些链接已损坏。 So I have the following loop to do the task but it's not working..所以我有以下循环来完成任务,但它不工作..

download_documents <- function(url, file_id) {
   tryCatch(
     {download.file(url, paste0('~/Desktop/Dataset/files/', file_id))}, 
      error = function(e) {NA},
      warning = function(w) {NA})
}
Map(download_documents, df$link, df$file_id)

Does anyone know what I'm doing wrong or have a better solution?有谁知道我做错了什么或有更好的解决方案? Thanks in advance for your help!在此先感谢您的帮助!

You can turn the file_id to valid names using make.names .您可以使用make.namesfile_id转换为有效名称。

Map(download_documents, df$link, make.names(df$file_id))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 R 中,如何检查一个 dataframe 中的列名是否存在于另一个 dataframe 中,然后在该列中插入一个值? - In R, how do I check to see if a column name in one dataframe is present in another dataframe and then insert a value into that column? R 如何从一个 dataframe 到另一个 dataframe 对应的列名进行除法 - R how to do division from one dataframe to another dataframe corresponding column name 如何从一列中提取一部分数据并使用 R 将其粘贴到另一列中? - how do I extract a part of data from a column and and paste it n another column using R? 如何使用R中数据框中的列来索引另一个数据框? - How do I use a column from a dataframe in R to index another dataframe? 在R中:如何从字符串加上列名称再加上分类变量来创建数据框名称? - In R: How do I create a dataframe name from a string plus a column name plus categorical variable? 如何使用向量重命名R数据框中的列以指示OLD列名? - How do I rename a column in a R dataframe using a vector to indicate the OLD column name? 如何阻止 r 使用第一行数据作为列名? - How do I stop r from using the first row of data as the column name? 在 R 中,如何在变量后命名 Dataframe 列? - In R How Do I Name a Dataframe Column After a Variable? R:使用另一个数据框中的列名,条件和值在一个数据框中创建一个新列 - R: Create a new column in a dataframe, using column name, condition and value from another dataframe 如何通过在R中调用列名来合并来自另一个数据帧的数据 - How to combine data from another dataframe by calling column names in r
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM