Mic*_*ael 4 r function save stata
我正在尝试将文件从一个文件夹移动到另一个文件夹.我有一个名为"data"的数据框,其中包含"from"位置,"to"位置和文件名"myfile".
library(foreign)
movefile <- function(from, to, myfile){
readfile <- paste(from, myfile, sep = "/")
temp <- read.dta(readfile)
copyto <- paste(to, myfile, sep = "/")
write.dta(temp, copyto)
}
Run Code Online (Sandbox Code Playgroud)
当我使用以下代码行调用该函数时:
movefile(data$from, data$to, data$myfile)
Run Code Online (Sandbox Code Playgroud)
它只复制第一个文件.当我尝试通过在函数中打印各种术语来诊断问题时(例如,添加print(copyto)作为函数的最后一行),它会打印数据中列出的每个文件,表明该函数正在为每一行运行数据,但它实际上并不复制第一个以外的文件.我怎么能纠正这个?
除非你真的需要将文件读入内存的data.frame使用read.dta,我会建议使用file.copy,这将使用计算机文件系统复制文件.
original.files <- do.call('file.path', data[c('from','myfile')])
new.files <- do.call('file.path', data[c('to','myfile')])
# overwrite will overwrite, so make sure you mean to do this
file.copy(from = original.files, to = new.files, overwrite = TRUE)
Run Code Online (Sandbox Code Playgroud)