-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAverageForEachOutfile.R
More file actions
80 lines (56 loc) · 3.74 KB
/
AverageForEachOutfile.R
File metadata and controls
80 lines (56 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#This R script calculates calendar year averages of basic paddock simulation results and writes them to a csv file
#Change the line below to suit your file structure
#Set the Working Directory to the Input files directory
setwd("C:\\Users\\David\\Desktop\\Apsim73\\Regions\\M_W_region\\Out_files")
filesList <- list.files() #Lists all of the files in the the working directory
#Create a data frame called df <-data.frame(var1Name=character(),.. nominate variable names and type)
df <- data.frame(FileName=character(),FirstYear=character(), LastYear=character(),Drainage=double(), Runoff=double(), Soilloss=double(), Canefw=double())
for( j in 1:length(filesList))
{
all_content = readLines(filesList[j]) # this is all of the data in the file
all_data_except_second_row = all_content[-2] # The -2 index just deletes the second row
data = read.csv(textConnection(all_data_except_second_row), header = TRUE, stringsAsFactors = FALSE) # This variable is a data frame (a number of named column vectors of the same length but can have different types like string or integer)
FirstYear = as.numeric(min(substr(data$todaydate,8,11)))
LastYear = as.numeric(max(substr(data$todaydate,8,11)))
numYears = LastYear-FirstYear+1
#YearRange = paste (as.character(LastYear),as.character(FirstYear), collapse="-")
anAveDrainage <- sum(data$Drainage)/numYears
anAveRunoff <- sum(data$Runoff)/numYears
anAveSoilloss <- sum(data$soil_loss)/numYears
canefreshweightMax <- max(data$canefw, na.rm=TRUE)
de <- data.frame( FileName=filesList[j], FirstYear=FirstYear, LastYear=LastYear, Drainage=anAveDrainage, Runoff=anAveRunoff, Soilloss=anAveSoilloss, Canefw=canefreshweightMax )
df = rbind(df,de, stringsAsFactors=FALSE)
print(j)
}
# Write filtered data into a new file.
#Change the line below to suit your file structure
#Saving the Results File
write.csv(df,"C:\\Users\\David\\Desktop\\Apsim73\\Regions\\M_W_region\\AveragesOfOutFiles.csv", row.names = FALSE)
=======
#Change the line below to suit your file structure
#Set the Working Directory to the Input files directory
setwd("C:\\Users\\David\\Desktop\\Apsim73\\Regions\\M_W_region\\Out_files")
filesList <- list.files() #Lists all of the files in the the working directory
#Create a data frame called df <-data.frame(var1Name=character(),.. nominate variable names and type)
df <- data.frame(FileName=character(),FirstYear=character(), LastYear=character(),Drainage=double(), Runoff=double(), Soilloss=double(), Canefw=double())
for( j in 1:length(filesList))
{
all_content = readLines(filesList[j]) # this is all of the data in the file
all_data_except_second_row = all_content[-2] # The -2 index just deletes the second row
data = read.csv(textConnection(all_data_except_second_row), header = TRUE, stringsAsFactors = FALSE) # This variable is a data frame (a number of named column vectors of the same length but can have different types like string or integer)
FirstYear = as.numeric(min(substr(data$todaydate,8,11)))
LastYear = as.numeric(max(substr(data$todaydate,8,11)))
numYears = LastYear-FirstYear+1
#YearRange = paste (as.character(LastYear),as.character(FirstYear), collapse="-")
anAveDrainage <- sum(data$Drainage)/numYears
anAveRunoff <- sum(data$Runoff)/numYears
anAveSoilloss <- sum(data$soil_loss)/numYears
canefreshweightMax <- max(data$canefw, na.rm=TRUE)
de <- data.frame( FileName=filesList[j], FirstYear=FirstYear, LastYear=LastYear, Drainage=anAveDrainage, Runoff=anAveRunoff, Soilloss=anAveSoilloss, Canefw=canefreshweightMax )
df = rbind(df,de, stringsAsFactors=FALSE)
print(j)
}
# Write filtered data into a new file.
#Change the line below to suit your file structure
#Saving the Results File
write.csv(df,"C:\\Users\\David\\Desktop\\Apsim73\\Regions\\M_W_region\\AveragesOfOutFiles.csv", row.names = FALSE)