I am importing some files from a folder on my computer, doing some calculations and then putting these calculations into new dataframes. I would like to save each of the new dataframes as csv files (filename_binomial data.csv) in a new folder on my computer. However, I am having difficulty doing so.
Here is my code:
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")
}
I want to save the new files in a folder called "Binomial data" and I want the file names to be the original filename but with an added "_binomial data."
So if the original file was called "John1990.csv", the new file should be called "John1990_binomial data.csv"
datanameis a variable that holds the actual name for the file name. You can usepaste0to combine it with the path and suffix for storing:I'd recommend to not use spaces in paths/file names as it can lead to problems or difficulties e.g. when working on the command line.