Reputation: 13
I am trying to automate the calculation of some animal energy requirements where I have inputs as days on feed, daily feed intake, etc. My code first reads in the initial data from a CSV, uses it to calculate some starting values outside the loop, runs a loop of that day's energy calculations for the time on feed, stores those results in a data frame, and then write the final data frame to a CSV.
I have data from >300 sheep on individual records basis like this and want to automate reading in the files, and writing the results to separate CSV files within a specific folder. I know this means a loop within a loop, but am trying to figure out how exactly to go about it.
I know I need to read in the files using files.list, like this:
files = list.files("C:/Users/Me/Desktop/Sheepfiles/", pattern = "Sheep+.*csv")
but I want each file as its own data frame run through the model and I need to keep everything separate going in and out.
setwd("C:Users/....../Sheepfiles")
input = read.csv(file = "Sheep131.csv", header = TRUE, sep =",")
#set up initialized values outside loop here
LWt0 = input$LWT[1]
EBW = LWT0*.96*.891
#constants go here
Results = NULL;
timefeed = input$DOF
#now the loop
for (i in timefeed)
{
#differential equations and calculations here
results1 = (c(t, NEG, MEI, OldMEI, HPmaint, EBW, ID, TRT))
names(results1) = c("DOF", "NEG", "MEI", "OldMEI","HPmaint", "EBW", "ID", "TRT")
print((results1))
Results = rbind(Results,results1)
#update variables to new values here
}
write.csv(Results, file = "Results131.csv")
What I want is for them to be able to have files with SheepX in the name, one per sheep, where X is the eartag #, have those read in, calculated, and then automatically output with the results in ResultsX.csv. If it helps, the eartag number is in the original input file under the column "ID". So for Sheep 1:150 I'd have Results1:150 etc
Later on, I'll need to be able to read those result files back in, extract outputs at specific days, and then pull those into a data frame for comparison with observations, but that's the next step after I get all these files run through the model.
Upvotes: 1
Views: 130
Reputation: 3812
You need to loop through your filenames and execute your existing code for each file, so a solution could look like this:
setwd("C:Users/....../Sheepfiles")
files = list.files("C:/Users/Me/Desktop/Sheepfiles/", pattern = "Sheep+.*csv")
for (i in files) {
input = read.csv(file = i,
header = TRUE,
sep = ",")
#set up initialized values outside loop here
LWt0 = input$LWT[1]
EBW = LWT0 * .96 * .891
#constants go here
Results = NULL
timefeed = input$DOF
#now the loop
for (i in timefeed)
{
#differential equations and calculations here
results1 = (c(t, NEG, MEI, OldMEI, HPmaint, EBW, ID, TRT))
names(results1) = c("DOF", "NEG", "MEI", "OldMEI", "HPmaint", "EBW", "ID", "TRT")
print((results1))
Results = rbind(Results, results1)
#update variables to new values here
}
# automatically generate filename for results
result.filename <- gsub("Sheep", "Results", i)
write.csv(Results, file = result.filename)
}
So you basically wrap a for-loop around your code, with your file-names as the counter-variable.
Upvotes: 1