Skip to content
Snippets Groups Projects
in_range.R 2.18 KiB
Newer Older
  • Learn to ignore specific revisions
  • pbac's avatar
    pbac committed
    # Do this in a separate file to see the generated help:
    #library(devtools)
    #document()
    #load_all(as.package("../../onlineforecast"))
    #?in_range
    
    
    #' Returns a logical vector of boolean values where TRUE indicates if timestamp is within the
    #' specified period.
    
    pbac's avatar
    pbac committed
    #'
    
    #' Returns a logical vector of boolean values where TRUE indicates if timestamp is within the
    #' specified period spanned by tstart and tend. 
    
    pbac's avatar
    pbac committed
    #'
    
    #' Note the convention of time stamp in the end of the time intervals causes the time point
    #' which equals \code{tstart} not to be included. See last example.
    
    pbac's avatar
    pbac committed
    #'
    #' The times can be given as character or POSIX, per default in tz='GMT'.
    #' 
    #' @title Selects a period
    #' @param tstart The start of the period.
    #' @param time The timestamps as POSIX.
    #' @param tend The end of the period. If not given then the period will have no end. 
    
    #' @param timezone The timezone of the timestamps, time.
    
    pbac's avatar
    pbac committed
    #' @return A logical vector indicating the selected period with TRUE
    #' @name in_range
    #' @examples
    #'
    #' # Take a subset
    
    pbac's avatar
    pbac committed
    #' D <- subset(Dbuilding, c("2010-12-15", "2011-01-01"))
    
    pbac's avatar
    pbac committed
    #'
    #' # Just a logical returning TRUE in a specified period
    #' in_range("2010-12-20", D$t, "2010-12-22")
    #'
    #' # Set which period to evaluate when optimizing parameters, like in rls_optim()
    #' # (the points with scoreperiod == false are not included in the score evaluation)
    #' D$scoreperiod <- in_range("2010-12-20", D$t)
    #' D$scoreperiod
    #'
    #' # Further, excluding a small period by
    #' D$scoreperiod[in_range("2010-12-26", D$t, "2010-12-27")] <- FALSE
    #' D$scoreperiod
    #'
    #' # Note the convention of time stamp in the end of the time intervals
    
    #' # causes the point with t = 2010-12-26 00:00:00 not to be included
    #' # since it's covering to "2010-12-25 23:00:00" to "2010-12-26 00:00:00"
    
    pbac's avatar
    pbac committed
    #' D$t[in_range("2010-12-26", D$t, "2010-12-27")]
    #'
    #'
    #' @export
    
    
    in_range <- function(tstart, time, tend=NA, timezone=NA) {
    
    pbac's avatar
    pbac committed
        if (class(tstart)[1] == "character") 
    
            tstart <- ct(tstart)
    
    pbac's avatar
    pbac committed
        if (is.na(tend))
            tend <- time[length(time)]
        if (class(tend)[1] == "character") 
    
            tend <- ct(tend)
    
        if (is.na(timezone)){
            timezone <- attr(D$t, "tzone")
        }
    
        ct(tstart, tz = timezone) < time & time <= ct(tend, tz = timezone)
    
    pbac's avatar
    pbac committed
    }