I'm testing the influence of the time period on the end result of my calculations. To do this, I'm using a for loop, that is detailled below. I'm sure there is a way to use apply in such a way as to remove the need for a loop, but I can't work it out. Some assistance would be more than welcome.
## result vector
HLClist<-vector()
for(i in 1:length(TimeSpan[,1])){
StartTime=TimeSpan[i,]
EndTime=TimeSpan[i,]+TimeInterval
Xbis<-Choixintervalle(X,StartTime,EndTime)
Xtierce <- resampleDF(Xbis, Ts)
HLC<-CalcAverage(Xtierce$Ph,Xtierce$Ti,Xtierce$Te)
HLC<-HLC[length(HLC)]
HLClist<-append(HLClist,HLC)
}
Where
TimeSpan is a list that contains all the startimes (format : double), defined as folows:
InitTime <-as.POSIXct("16/02/2014 0:00", format="%d/%m/%Y %H:%M") FinalTime <-as.POSIXct("16/03/2014 0:00", format="%d/%m/%Y %H:%M") TimeInterval <-144*3600 SEndTime <- FinalTime - TimeInterval TimeSpan<-data.frame(seq(InitTime, SEndTime, by=3600))
TimeInterval is the number of seconds between Start and endtime (format : double)
- X is the dataframe containing all my data:
> X
t Ph Elec Sol Ti Te DHW
1 16/02/2014 0:00 612 612 0.0 22.70 4.600000 0.0000
2 16/02/2014 0:05 612 612 0.0 22.70 4.600000 0.0000
3 16/02/2014 0:10 516 516 0.0 22.79 4.600000 0.0000
4 16/02/2014 0:15 480 480 0.0 22.70 4.600000 0.0000
5 16/02/2014 0:20 540 540 0.0 22.70 4.600000 0.0000
6 16/02/2014 0:25 528 528 0.0 22.60 4.600000 0.0000
7 16/02/2014 0:30 492 492 0.0 22.60 4.600000 0.0000
8 16/02/2014 0:35 528 528 0.0 22.50 4.600000 0.0000
9 16/02/2014 0:40 492 492 0.0 22.49 4.600000 0.0000
10 16/02/2014 0:45 456 456 0.0 22.43 4.600000 0.0000
Choixintervalle is the following function:
Choixintervalle <-function(X,startTime=NA,endTime=NA) { ## set the start time if not specified if(is.na(startTime)){startTime <- as.POSIXct(X[1,1], format="%d/%m/%Y %H:%M")} else{startTime<-as.POSIXct(startTime, format="%d/%m/%Y %H:%M")} ## set the end time if not specified if(is.na(endTime)){ endTime <- as.POSIXct(X[nrow(X),1], format="%d/%m/%Y %H:%M")} else{endTime<-as.POSIXct(endTime, format="%d/%m/%Y %H:%M")} X<-X[(as.POSIXct(X$t,format="%d/%m/%Y %H:%M"))>startTime,] X<-X[(as.POSIXct(X$t,format="%d/%m/%Y %H:%M"))<endTime,] return(X) }
ResempleEDF is the folowing function
resampleDF <- function(X,Ts,startTime = NA, endTime =NA,timeName="t",includeNA=TRUE,quantizeTime=TRUE,meanNaRm=FALSE) { ## Split into periods of length Ts, and take the mean of each period X[,timeName] <-as.POSIXct(X[,timeName], format="%d/%m/%Y %H:%M")-startTime iSplit <- as.integer(X[,timeName]) %/% Ts ## Do the resampling Xres <- aggregate(X, list(iSplit), mean, na.rm=meanNaRm) ## Remove the "Group" column Xres <- Xres[,-1] ## Convert time to POSIXct Corr<- as.integer(Xres[1,timeName]) Xres[,timeName] <- startTime + as.integer(Xres[,timeName])-Corr return(Xres) }
-CalcAverage is the following function:
CalcAverage <- function(Q, Ti,Te) { Solution = vector(length(Q), mode="double" ) for (i in 1:length(Q)){ Temp<-sum(Ti[1:i]-Te[1:i]) Heat<-sum(Q[1:i]) #création du vecteur R Solution[i]<-Heat/Temp } return(Solution) }