0

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) 
    }
    
  • 1
    Do note: [*apply* functions are loops](https://stackoverflow.com/questions/28983292/is-the-apply-family-really-not-vectorized). – Parfait Jul 13 '18 at 14:44
  • 1
    *but I can't work it out* ... is not very helpful for us. Please describe the errors or undesired results you receive. Also, please include all objects so we can reproduce your process including *TimeSpan* and *TimeInterval*. Make sure your code is compilable and runnable from an empty R environment. – Parfait Jul 13 '18 at 14:47
  • I added the code I used to create TimeSpan and TimeInterval. The problem with this code is that it takes 10 minutes to resolve, and I was hoping that the use of the apply function would speed it up. – K. De Sloover Jul 13 '18 at 15:25
  • This is a very important point to make in your text: that this code works as intended but performance is slow. And actually, this should be posted to [Code Review](https://codereview.stackexchange.com/) as StackOverflow deals with problem/error code. – Parfait Jul 13 '18 at 15:28
  • Thank you, I posted my question to Code Review, as suggested. – K. De Sloover Jul 16 '18 at 07:19

0 Answers0