我正在从计算机上的文件夹导入一些文件,进行一些计算,然后将这些计算放入新的数据帧中。我想将每个新数据帧保存为 csv 文件(filename_binomial data.csv)在我的计算机上的新文件夹中。然而,我很难做到这一点。
这是我的代码:
library(dplyr)
library(data.table)
parentdir<-"C:/Users/s2087070/Desktop/PhD/IAH Systematic Review"# root path to main folder
datadir<- paste(parentdir,"/New Folder",sep="") # directory containing original data files
#set working directory
setwd(datadir)
#list the files in the directory
files<-list.files()
##Loop through the files
for(file in files){
#dataname <-files[file]
dataname<-strsplit(file,split='[.]')[[1]][1]
#Load data
data<-read.csv(file)
data1<-data[,1:3]
data1[,"Uninfected"]<-data1$num.tot-data1$num.inf
new<-reframe(data1, Score = c(rep(1, num.inf), rep(0, Uninfected)), .by = inoc.size)
new1<-data.frame(new)
write.csv(new1, "C:/Users/s2087070/Desktop/PhD/IAH Systematic Review/Binomial
data/dataname_binomial data.csv")
}
我想将新文件保存在名为“二项式数据”的文件夹中,并且我希望文件名是原始文件名,但添加了“_二项式数据”。
因此,如果原始文件名为“John1990.csv”,则新文件应名为“John1990_binomial data.csv”
dataname
是一个保存文件名实际名称的变量。可以使用paste0
与路径和后缀组合起来存储:
write.csv(new1, paste0("C:/Users/s2087070/Desktop/PhD/IAH Systematic Review/Binomial data/", dataname, "_binomial data.csv"))
我建议不要在路径/文件名中使用空格,因为它可能会导致问题或困难,例如在命令行上工作时。