isValidTimeSeries: Test that Date or POSIXct date-time are unique, sorted and...

View source: R/macroutils2.r

isValidTimeSeriesR Documentation

Test that Date or POSIXct date-time are unique, sorted and regular.

Description

Test that Date or POSIXct date-time are unique, sorted and regular.

Usage

isValidTimeSeries(x, units = "hours", onError = warning)

Arguments

x

A vector of Date, or of POSIXct date-times

units

Passed to as.numeric-difftime. Only used in case irregularities in the time series are detected.

onError

A valid R function, such as warning or stop, or message. Function that will be used to output an error message if the time series is not unique, sorted and regular.

Value

Returns FALSE if a problem was detected, and TRUE otherwise (unless onError is stop, in which case an error is send and the function stops).

Examples


library( "macroutils2" )

timeseries <- seq.POSIXt(
    from = as.POSIXct( "2015-01-01 12:00", tz = "GMT" ), 
    to   = as.POSIXct( "2015-12-31 12:00", tz = "GMT" ), 
    by   = "day"
)   

isValidTimeSeries( x = timeseries ) 
# [1] TRUE



warning2 <- function(w){message("",w)}

#   The time series is not increasing
tryCatch( isValidTimeSeries( x = rev( timeseries ) ), 
    warning = warning2 )

#   The time series is not increasing homogeneously
tryCatch( isValidTimeSeries( x = timeseries[ -2 ] ), 
    warning = warning2 )

#   The time series has duplicated values
timeseries2 <- rep( timeseries, each = 2 ) 
tryCatch( isValidTimeSeries( x = timeseries2 ), 
    warning = warning2 )



#   See also option 'timeSeriesValid' in ?muPar

julienmoeys/macroutils2 documentation built on Feb. 28, 2024, 2:17 a.m.