简体   繁体   English

将文件路径的数据帧复制到 R 中的新文件夹中

[英]copying a dataframe of file paths into a new folder in R

I have a dataframe containing a bunch of file paths.我有一个包含一堆文件路径的数据框。 I am trying to copy the files with these paths into a new folder.我正在尝试将具有这些路径的文件复制到一个新文件夹中。 My attempt is file.copy(data1,"E:\\\\Contracts\\\\MainContracts") , where data1 is my dataframe and "E:\\Contracts\\MainContracts" is the path to the destination folder.我的尝试是file.copy(data1,"E:\\\\Contracts\\\\MainContracts") ,其中 data1 是我的数据框,“E:\\Contracts\\MainContracts”是目标文件夹的路径。 'from' path too long When I run this I get "'from' path too long". 'from' 路径太长当我运行这个时,我得到“'from' 路径太长”。 Does anyone know what I am doing wrong?有谁知道我做错了什么?

Here is a small example that will first write 3 files to your desktop in a folder called 'demo' and then copy all the files into another director on your desktop 'demo-copy'.这是一个小例子,它首先将 3 个文件写入桌面“demo”文件夹中,然后将所有文件复制到桌面“demo-copy”上的另一个目录中。 We need to make sure we have either the full path names of the files to move or set the working directory to the folder they are in if not.我们需要确保我们有要移动的文件的完整路径名,或者如果没有,则将工作目录设置为它们所在的文件夹。 Also file.copy takes a character vector in the 'from' argument so we need to pass column of file paths from the dataframe by indexing with the $ .此外file.copy在 'from' 参数中采用字符向量,因此我们需要通过使用$进行索引来传递数据帧中的文件路径列。

See if this example gives you an idea of the workflow:看看这个例子是否让你了解工作流程:

library(purrr)
#create example data
data_to_save <- list(mtcars, iris, ToothGrowth)
filenames <- paste0('~/Desktop/demo/', c('mtcars', 'iris', 'ToothGrowth'), '.csv')
dir.create('~/Desktop/demo')
purrr::walk2(data_to_save, filenames, ~write.csv(.x, .y))

#copy all files to new folder
df_of_filenames <- data.frame(filename = list.files('~/Desktop/demo', pattern = '.csv', full.names = T))
dir.create('~/Desktop/demo-copy')
file.copy(df_of_filenames$filename, '~/Desktop/demo-copy/')

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM